Question

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.

Was it helpful?

Solution

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:

$generator = Mage::getModel('salesrule/coupon_massgenerator');

You need to set some options:

$data = array(
    'max_probability'   => .25,
    'max_attempts'      => 10,
    'uses_per_customer' => 1,
    'uses_per_coupon'   => 1,
    'qty'               => 5, //number of coupons to generate
    'length'            => 14, //length of coupon string
    'to_date'           => '2013-12-31', //ending date of generated promo
    /**
     * Possible values include:
     * Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC
     * Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHABETICAL
     * Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_NUMERIC
     */
    'format'          => Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC,
    'rule_id'         => 1234 //the id of the rule you will use as a template
);

Validate that the $data is correct:

$generator->validateData($data);

And then save those options to the generator:

$generator->setData($data);

And now, GENERATE!

$generator->generatePool();

They're populated into salesrule_coupon:

enter image description here

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:

enter image description here

If you need to get them programmatically:

$salesRule = Mage::getModel('salesrule/rule')->load($data['rule_id']);
$collection = Mage::getResourceModel('salesrule/coupon_collection')
            ->addRuleToFilter($salesRule)
            ->addGeneratedCouponsFilter();

You can get a count of how many were generated successfully if you need:

$generator->getGeneratedCount();

OTHER TIPS

the options for data array can be,

$generator = Mage::getModel('salesrule/coupon_massgenerator');
$data = array(
    'uses_per_customer' => 1,
    'uses_per_coupon'   => 1,
    'qty'               => 1,  //number of coupons to generate
    'length'            => 16, //length of coupon string
    'to_date'           => date('Y-m-d', strtotime("+1 month",  time())), //ending date of generated promo
    'prefix'            => 'rev',
    'suffix'            => 'tf',
    'dash'              => 5,
    /**
     * Possible values include:
     * Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC
     * Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHABETICAL
     * Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_NUMERIC
     */
    '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();

$salesRule = Mage::getModel('salesrule/rule')->load($data['rule_id']);
$collection = Mage::getResourceModel('salesrule/coupon_collection')
            ->addRuleToFilter($salesRule)
            ->addGeneratedCouponsFilter();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top