Magento 1 – programmatically create promotion coupon codes
In this tutorial I show you how to programmatically create coupon codes for a promotion code rule. This was needed, because your shop offers a promotion code for all new newsletter subscribers. For this I created an observer for newsletter_subscriber_save_commit_after.
Magento 1 – programmatically create promotion coupon codes
Creating unique promotion codes is very easy. First step is to create a new shopping cart price rule. It is important to set the following settings:
This means, that this rule can be activated by a specific coupon. We do not set a specific coupon code that is valid for all users. We want to create individual coupons, that are only valid once. So we check “Use Auto Generation”. In action tab we define the value for a individual coupon.
The rule is: 10 Dollar for next order. So we set discount amount and define that this is a fixed amount discount for whole cart. We do not set “Stop Further Rules Processing”, so a user may also use another coupon. If you only want to allow this coupon with no other, set this to Yes.
The last set is to create coupon codes. You can set various settings in the shown form and click generate. Based on this rules codes are generated and shown in the bottom grid. This can be done if you want to manually give customers coupons. You won’t do that if you run a automatic service. So based on this form, we create our code that programmatically generate coupon codes on the fly.
Generate some coupon codes
As mentioned before, we create codes after a user has submitted for a newsletter. In the relevant observer method we call generateCode Function to get a coupon code and then send a mail with this code to the user. Here is how we create a coupon code for the previously generated rule:
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 | function generateCode($rule_id, $amount) { // Get the rule in question $rule = Mage::getModel('salesrule/rule')->load($rule_id); // Define a coupon code generator model instance // Look at Mage_SalesRule_Model_Coupon_Massgenerator for options $generator = Mage::getModel('salesrule/coupon_massgenerator'); $parameters = array( 'count'=>$amount, 'format'=>'alphanumeric', 'dash_every_x_characters'=>'', 'prefix'=>'', 'suffix'=>'', 'length'=>8 ); if( !empty($parameters['format']) ){ switch( strtolower($parameters['format']) ){ case 'alphanumeric': case 'alphanum': $generator->setFormat( Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC ); break; case 'alphabetical': case 'alpha': $generator->setFormat( Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHABETICAL ); break; case 'numeric': case 'num': $generator->setFormat( Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_NUMERIC ); break; } } $generator->setDash( !empty($parameters['dash_every_x_characters'])? (int) $parameters['dash_every_x_characters'] : 0); $generator->setLength( !empty($parameters['length'])? (int) $parameters['length'] : 6); $generator->setPrefix( !empty($parameters['prefix'])? $parameters['prefix'] : ''); $generator->setSuffix( !empty($parameters['suffix'])? $parameters['suffix'] : ''); // Set the generator, and coupon type so it's able to generate $rule->setCouponCodeGenerator($generator); $rule->setCouponType( Mage_SalesRule_Model_Rule::COUPON_TYPE_AUTO ); // Get as many coupons as you required $count = !empty($parameters['count'])? (int) $parameters['count'] : 1; $codes = array(); for( $i = 0; $i < $count; $i++ ){ $coupon = $rule->acquireCoupon(); $coupon->setUsageLimit(1); $coupon->setTimesUsed(0); $coupon->setType(1); $coupon->save(); $code = $coupon->getCode(); $codes[] = $code; } return $codes; } |
As you see, it is very easy to do that. You can set the same settings as defined in backend form. You can individually create coupon codes with different lengths and format. Magento is very flexible and it creates always unique coupon codes.
Conclusion
It is quite easy to programmatically create coupon codes. I showed you a method to create it on the fly. If you have a trigger like newsletter subscriptions, you can create individual coupon codes for each user that can only used once.
Another great article found on your blog. Great Work!
However, I have one query that in Magento 2 we can create coupon code directly from its admin panel as shown here: https://magenticians.com/how-to-create-coupon-code-magento-2/. So is there any need to create coupon code in Magento 2 programmatically?