Let me preface this by stating that the company I work for is very unusual in how it uses coupons. I won’t go into details, but I don’t know of many other online merchants that have the same requirements for coupons as we do.
I’m breaking this post up into two posts. The first post will deal with using direct database inserts into the salesrule table to load coupons. The second will deal with using a custom script or module that makes use of Mage code to populate the salesrule/rule objects and persist them to the database usng $model->save() or $collection->save();
Before I continue, if I had a lawyer they’d probably make me add something like the following:
- WARNING: Any use of the following is at your own risk. It is not recommended or supported by Varien (or me for that matter). This post is provided for informational purposes only.
There, now that is out of the way we can move on to some content. Coupons (sales promotions/shopping cart rules hereafter referred to simply as coupons) in Magento are a curious beast. For some reason, Varien has provided no built-in way to import/export promotions. They’ve left this for us developers to figure out. I’m a developer. I like to know how things work “under the hood” so that’s perfectly ok with me.
Looking through the code to see where Magento stores the underlying data for a sales rule we find that the data to support a coupon is stored in two tables. The first table is the salesrule table. This table holds the definition of the coupon and how it functions. The second table is the salesrule_label table. This table holds the text that is displayed in the cart next to the discount amount. The reasoning for this separate table is that Magento allows you to have different labels for each store (not really sure how this would be useful, nevertheless, it’s there). For the purposes of this post, we are only going to focus on the salesrule table as the salesrule_label table should be fairly simple to handle on your own (again, see the legal mumbo-jumbo above).
For the most part, the fields in the salesrule table directly relate to fields on the edit screen in the admin:
These 4 fields should be fairly obvious.
- rule_id – Autogenerated id number
- customer_group_ids – comma delimited list of customer group ids
- website_ids – the website id this rule applies to
These 2 are special cases. They are serialized arrays for the condition rules and the action rules.
The difference between conditions and actions is a little misleading. Conditions can be used to exclude certain transactions from the coupon being used (it’s not limited to exclusions, but it’s also not necessarily needed for a coupon to be effective). Actions are what you use to define the criteria used to apply the coupon to an order.
If you’re like me, I know you are dying for a sample. So without further ado. The below script will insert a coupon with code CPN1234 that will set the price of a product with SKU number SKU-1234 for $14.99. Most of it should be fairly explanatory (if not, let me know in the comments and I’ll update the post with an explanation).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85