Based on other stackoverflow posts, I was able to use the following code to programmatically create individual shopping cart rule coupons in Magento.
How can I programmatically call the "Auto generate coupon" function to create 100 unique coupons for each price rule that I make? Thanks!
$coupon = Mage::getModel('salesrule/rule'); $coupon->setName($_coupon['name']) ->setDescription('this is a description') ->setFromDate(date('Ym-d')) ->setCouponType(2) ->setCouponCode($_coupon['code']) ->setUsesPerCoupon(1000) ->setUsesPerCustomer(100) ->setCustomerGroupIds(array(1)) //an array of customer groupids ->setIsActive(1) //serialized conditions. the following examples are empty ->setConditionsSerialized('a:6:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}') ->setActionsSerialized('a:6:{s:4:"type";s:40:"salesrule/rule_condition_product_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}') ->setStopRulesProcessing(0) ->setIsAdvanced(1) ->setProductIds('') ->setSortOrder(0) ->setSimpleAction('by_percent') ->setDiscountAmount(100) ->setDiscountQty(null) ->setDiscountStep('0') ->setSimpleFreeShipping('0') ->setApplyToShipping('0') ->setIsRss(0) ->setWebsiteIds(array(1)); $coupon->save();
For example, this one price rule may contain a complete list of auto-generated coupon codes (htgf-7774, htgf-2345, etc.) using the function available when manually creating price rules in the admin panel.
EDIT:
I came closer using the following code. Still don't know how to specifically assign a generation template
->setName('Name') ->setDescription('this is a description') ->setFromDate('2013-03-06') ->setToDate(NULL) ->setUsesPerCustomer('100') ->setIsActive('1') ->setConditionsSerialized('a:6:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}') ->setActionsSerialized('a:6:{s:4:"type";s:40:"salesrule/rule_condition_product_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}') ->setStopRulesProcessing('0') ->setIsAdvanced('1') ->setProductIds(NULL) ->setSortOrder('0') ->setSimpleAction('by_percent') ->setDiscountAmount('100.0000') ->setDiscountQty(NULL) ->setDiscountStep('0') ->setSimpleFreeShipping('0') ->setApplyToShipping('0') ->setTimesUsed('1') ->setIsRss('0') ->setCouponType('2') ->setUseAutoGeneration('1') ->setUsesPerCoupon('1000') ->setCustomerGroupIds(array('1',)) ->setWebsiteIds(array('1',)) ->setCouponCode(NULL)
source share