I would like to create unique coupon codes automatically.
I have tried the following – when using setCouponCode() to generate coupon code, i must set static code. Example:
$code = "abc";
setCouponCode($code);
I would like this to be a unique random string instead.
Best Answer
Magento has this facility in-built since 1.7CE.
The class
Mage_SalesRule_Model_Coupon_Massgenerator
. To make use of it on your own you can instantiate the class:You need to set some options:
Validate that the $data is correct:
And then save those options to the generator:
And now, GENERATE!
They're populated into
salesrule_coupon
:It works reasonably well - you'll have to fetch back out which coupons were generated though. To do that, you may look under "Manage Coupon Codes" tab of the shopping cart rules:
If you need to get them programmatically:
You can get a count of how many were generated successfully if you need: