How can I create or auto generate Magento coupon codes programmatically.
First, you need to create a sales rule or load an existing sales rule and then use “salesrule/coupon_massgenerator” model to create the coupons.
$generator = Mage::getModel('salesrule/coupon_massgenerator'); $data = array( 'max_probability' => .25, 'max_attempts' => 10, 'uses_per_customer' => 1, 'uses_per_coupon' => 1, 'qty' => 2, //number of coupons to generate 'length' => 10, //length of coupon string 'to_date' => '2017-9-11', //ending date of generated promo 'format' => Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC, 'rule_id' => 1//the id of the rule you will use as a template ); $generator->validateData($data); $generator->setData($data); $generator->generatePool();