Free Magento extension for Coupons Import

Tweet about this on TwitterShare on LinkedInShare on Google+Share on FacebookEmail this to someone

Magento arrives with some really good functionality that allows store owners to create automatically generated coupons. Later those coupons can be sent to individual customers in marketing campaigns. Sometimes this strategy will not be insufficient in order to respond to certain business situations because the business case could require that the individual coupons be generated in third party system and later imported in Magento.

This weekend I’ve decided to do some programming for fun. I’ve developed a Magento extension that allows store owners to import coupons in Magento via Advanced Import/Profiles. So far the extension looks more like a proof of concept because it imports coupons that have very simple actions.

The coupons imported by my extension can be used in case you want have coupons for certain SKUs.

I’ve stuck to this basic functionality because describing coupon actions and conditions could lead to complicated data structure and right now I don’t have a clear idea how this data can be neatly described in CSV file. I believe that I would have a chance to develop more a flexible import once I get some feedback from people who’ve tried to utilise my extension.

How to use?

  1. Install the extension – download link Extendix_CouponImport
  2. Create the CSV import file (I describe below what columns the csv file should contain)
  3. Create an Advanced Dataflow Profile (I describe below what configuration the import profile should contain)

1. Installation:

For the time being, extension is available on GitHub and can be installed directly or by modman.
Repository: Extendix_CouponImport
(If you are also interested how to generate your own modman files you can take a look at Magento: Generate modman files)

2. Coupons import csv file format:

Below I describe what the CSV import columns should look like:

  • name – Text for coupon name
  • description – Text for coupon description
  • is_active – 0 inactive / 1 active
  • website_ids – Text containing website id example values:
    • 1 – for website with id = 1
    • 1,2 – for websites with id = 1 or 2
    • all – create coupon for all available websites
  • customer_group_ids – Text containing customer group ids example values:
    • 1 – for customer group with id = 1
    • 1,2 – for customer group with id = 1 or 2
    • all – create coupon for all available customer groups
  • coupon_code – Text for coupon code that customers will submit in the cart
  • sku – product sku/s for which a coupon can be applied. It could be a string with comma-separated skus. Example values:
    • wbk002c – means that the coupon can be applied for products with sku wbk002c
    • wbk002c,shw003 – means that the coupon can be applied for products with skus wbk002c,shw003
  • uses_per_coupon – Number that describes how many times the coupon could be used in total
  • uses_per_customer – Number that describes how many times the coupon can be used per customer
  • from_date – Date from which the coupon is valid. Example:
    • 4-12-2015 – the date format is day-month-year
    • If you leave it empty, the coupon will not have a valid start date
  • to_date – Date until which the coupon is valid. Example:
    • 23-1-2016 – the date format is day-month-year
    • If you leave it empty, the coupon will not have a valid end date
  • discount_amount – Number that describes what discount will be given. Example:
    • 1.33 – depending on the simple_action field this number could be fixed or percentage of the product price
  • simple_action – Text that describes the type of discount that can be given. Example values:
    • by_percent – Product price discount as percentage
    • by_fixed – Fixed amount discount
    • cart_fixed – Fixed amount discount for the whole cart
    • buy_x_get_y – Buy X and get Y free (discount amount is Y)

It’s possible for other columns to be added to the imported csv file. I haven’t tested this because I wanted to focus on very simple business case for the current implementation. If you are interested, you could try adding the following columns in the csv import file:

  • sort_order – Priority – (number)
  • discount_qty – Maximum Qty Discount is Applied To – (number)
  • discount_step – Discount Qty Step (Buy X) – (number)
  • apply_to_shipping – Apply to Shipping Amount – 0/1 (No/Yes)
  • simple_free_shipping – Free Shipping – 0/1 (No/Yes)
  • stop_rules_processing – Stop Further Rules Processing – 0/1 (No/Yes) By default this values is 1

You can also take a look at this sample csv import file if you want to give a try an import:

3. Advanced Dataflow Import Profile Configuration:

In order to create an Advanced Dataflow Import Profile, please follow these steps:

  • In Magento admin go to System -> Import/Export -> Dataflow Advanced Profiles
  • Click on the “Add New Profile” button
  • Populate the “Profile Name” and “Action XML” text fields:
    • Profile Name – write the name you want
    • Action XML – paste this template:

I won’t delve into details about the advanced profile configuration but it’s important to mention that you can control import filename and import folder path if you change the path and filename values in the action node.

Also set the number_of_records value to 100 because this means that we will try to import 100 coupons per batch import step. For example, if this value is 1 then the import would be very slow.

Once you’ve configured everything, run your import profile!

About the current implementation:

I’ve decoupled the import logic from Magento Dataflow. This means that other developers could import coupons easily by calling Extendix_CouponImport_Model_Import_SalesRule::createCouponSpecificSkus() in their own modules.

I was considering using Mage::getSingleton() in the Extendix_CouponImport_Model_Import_SalesRule constructor when injected class dependencies but I decided to stick to Mage::getModel(). I decided to use Mage::getSingleton() when I create Extendix_CouponImport_Model_Import_SalesRule in order to avoid querying Magento DB for Website Ids and Customer Group Ids for each coupon import.

So far there are no unit tests but if I decide to develop this extension further, with many new features, then I will create some.

Future development:

I would like to receive some feedback about different business cases where coupons import could be implemented in order to understand how to handle complex coupon conditions and actions data. I could try to develop my own data format that represents coupon conditions and actions in csv file but I prefer to get more feedback from developers and merchants on the format they like for their shops.

I am also considering improving the import speed. Right now it’s importing coupons one at a time but this could be prove problematic for huge coupon imports on shops with high concurrency. I think that there is a way to import hundreds of coupons in a single transaction.

I am also considering a functionality that would allow updating specific attributes of already existing coupons. A good use case could be updating discount_amount.

If you have any suggestions or feedback, then I’d be happy to hear from you!

Your thoughts?

Tweet about this on TwitterShare on LinkedInShare on Google+Share on FacebookEmail this to someone

Tsvetan Stoychev

Tsvetan aka. Cecko is the founder of Cecko's Lab. He is Magento addicted since Magento CE and has worked on over 30 Magento projects. At the moment he is in charge to take care about the money flow of the company, to keep constant communication with the clients and to keep the people in the office busy.

More Posts

Follow Me: