photo credit: rose3694 via photopin cc

Bulk import coupon codes in Magento

When migrating from another shop or when you have a client who hands you a list of coupon codes, you want to be able to bulk import coupon codes in Magento. Now, there are some existing solutions out there, and I stumbled into this one from Marius Strajeru. This script does generate bulk coupon codes, but with some adjustments I changed the script to a CSV importer for coupon codes.

This example imports a CSV file with coupon codes exported from a ViArt shop:

Just place it with a corresponding CSV-file in the root of your Magento installation and call the URL from your browser. It would be nicer to have this done as an extension, but as a quick fix it will do.


Visitors give this article an average rating of 3.3 out of 5.

How would you rate this article?

30 thoughts on “Bulk import coupon codes in Magento”

  1. Luis says:

    Thank you very much!! it works like charm!! you save me a lot of time =)

  2. Lar says:

    Hi there, can you tell me or better give me a sample CSV file to use with this script?



  3. You saved my life, it works great! Thanks.

  4. By the way, here’s the sample CSV. 1st line is the csv header.

    1,AABB00001,Cupom 1,100
    1,AABB00002,Cupom 2,100
    1,AABB00003,Cupom 3,100
    1,AABB00004,Cupom 4,100

  5. caseybecking says:

    Where should I place this file and how do I execute it?

  6. Nazimul Hossain says:

    Thank you so much it’s really works in magento 1.7 also….

  7. Ankit says:

    It shows me this Error?

    Not imported (not active):

    Please explain!

    1. Because in “is_active” cell you put value=0.Just check your “coupons.csv” file and enter value=1 in “is_active” cell.


  8. joe says:

    Nice! works fine thanks

  9. jd says:

    How to use this code?

  10. Andy says:

    you saved my life! THANK YOU!

  11. richard says:

    I understand this is quite an old post but i’m struggling to edit this to allow me to set an end date. Any ideas? Great code though thanks! Has helped a lot!

    1. Giel Berkers says:

      Have you tried $rule->setFromDate() and $rule->setToDate() already? With this you can set a date in YYYY-MM-DD format.

      1. richard says:

        Yes sorry I should have included the edits.

        I have $rule->setToDate($toDate). And have also included $toDate in my CSV file, and also in the function createCoupon line.

        When I run the import with the date in YYYY-MM-DD it results in a date populated with 01/01/1970. I have ready this means it is reading this as empty.

        Thank you so much for the quick response! to be honest I wasn’t expecting one because of the date of the post!

        1. Giel Berkers says:

          When the coupon is saved, is there a value stored in the database (salesrule-table)? Or is it NULL? What happens when you do a var_dump($rule->debug()) before saving the rule? What’s the value of from_date and to_date then?

  12. Crystal says:

    How can I apply this to a particular product, sku?

    1. Crystal says:

      Nevermind figured it out. This is the code I used. This codes works pretty much the same for conditions.

      $actions = Mage::getModel(‘salesrule/rule_condition_product’)

      1. keyrex says:

        Im on magento 1.9 and would love to restrict the codes to specific products – i can’t get your code edit to work. Any ideas?


        1. Giel Berkers says:

          The example provided by Crystal should work as intended. All it does is add a condition to your coupon. Didn’t you make any typo’s in the attribute, operator or SKU? Is the rule showing up at all after creating the coupon code?

  13. Koray Küpe says:

    Will it create a new rule or add codes to current rule?

    1. Giel Berkers says:

      Hi, this example creates a new rule, but just as with any other Magento model you can load an existing rule by id, modify it and save it.

  14. mau says:

    Thank you for sharing, although I found it’s easier to manage-edit to just use the auto generation feature in Magento http://www.crimsonwing.com/ecommerce/how-to-manage-multiple-coupon-codes-per-shopping-cart-price-rule-in-magento/

  15. odanicola says:

    Hi, how could i import multiple coupon in a rule?

    1. Giel Berkers says:

      I’m not sure if I understand your question correctly, but wouldn’t a foreach()-loop do the trick?

      1. vineela says:

        hi, may I know where can I use foreach() loop to import multiple coupon codes under one rule?
        thanks in advance.

  16. Perry says:

    I need to create and import 10 000+ coupons for 7 rules. I think it’ll be quite hard and time-consuming. I found this extension to help me https://amasty.com/generate-and-import-coupons.html. Do you have any experience with it? Or do you have any other suggestions?
    Thanks in advance

    1. Giel Berkers says:

      I know there are a lot of extensions to do a lot of stuff. Mostly we tend to use extensions when we want to give our clients the power to do stuff like this themselve. If it’s for a one-timer or very specific task sometimes a shell script is better suited for the task.

    2. vinnu says:

      mine is also same requirement can u suggest me if you got the solution.
      thanks in advance.

  17. vinnu says:

    hi thanks for nice code
    how do we import multiple coupon codes for a rule. does only foreach work? please suggest me.
    Thanks in advance.

Leave a Reply