title: Programmatically create auto generated coupon codes in Magento?
body: <p>Based on other stackoverflow posts, I've been able to use the following code to programmatically generate individual shopping cart price rule coupons in Magento. </p>
  
  <p>How can I programmatically call the "Auto Generate Coupon" feature to create 100 unique coupons for each price rule I make? Thanks!</p>
  
  <pre><code>$coupon = Mage::getModel('salesrule/rule');
  $coupon-&gt;setName($_coupon['name'])
         -&gt;setDescription('this is a description')
         -&gt;setFromDate(date('Y-m-d'))
         -&gt;setCouponType(2)
         -&gt;setCouponCode($_coupon['code'])
         -&gt;setUsesPerCoupon(1000)
         -&gt;setUsesPerCustomer(100)
         -&gt;setCustomerGroupIds(array(1)) //an array of customer groupids
         -&gt;setIsActive(1)
         //serialized conditions.  the following examples are empty
         -&gt;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";}')
         -&gt;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";}')
         -&gt;setStopRulesProcessing(0)
         -&gt;setIsAdvanced(1)
         -&gt;setProductIds('')
         -&gt;setSortOrder(0)
         -&gt;setSimpleAction('by_percent')
         -&gt;setDiscountAmount(100)
         -&gt;setDiscountQty(null)
         -&gt;setDiscountStep('0')
         -&gt;setSimpleFreeShipping('0')
         -&gt;setApplyToShipping('0')
         -&gt;setIsRss(0)
         -&gt;setWebsiteIds(array(1));      
  $coupon-&gt;save();
  </code></pre>
  
  <p>For instance, this one price rule might have a whole list of Auto-Generated Coupon Codes (htgf-7774, htgf-2345, etc) using the function that is available when manually creating price rules in the admin panel.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I've gotten closer, using the following code. Still don't know how to specifically assign the auto generation pattern</p>
  
  <pre><code>-&gt;setName('Name')
  -&gt;setDescription('this is a description')
  -&gt;setFromDate('2013-03-06')
  -&gt;setToDate(NULL)
  -&gt;setUsesPerCustomer('100')
  -&gt;setIsActive('1')
  -&gt;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";}')
  -&gt;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";}')
  -&gt;setStopRulesProcessing('0')
  -&gt;setIsAdvanced('1')
  -&gt;setProductIds(NULL)
  -&gt;setSortOrder('0')
  -&gt;setSimpleAction('by_percent')
  -&gt;setDiscountAmount('100.0000')
  -&gt;setDiscountQty(NULL)
  -&gt;setDiscountStep('0')
  -&gt;setSimpleFreeShipping('0')
  -&gt;setApplyToShipping('0')
  -&gt;setTimesUsed('1')
  -&gt;setIsRss('0')
  -&gt;setCouponType('2')
  -&gt;setUseAutoGeneration('1')
  -&gt;setUsesPerCoupon('1000')
  -&gt;setCustomerGroupIds(array('1',))
  -&gt;setWebsiteIds(array('1',))
  -&gt;setCouponCode(NULL)
  </code></pre>