4

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.

13
  • I'm not sure what you're asking. You want to generate a bunch of random codes programmatically? or you want to know how to make a random string? Commented Sep 12, 2013 at 14:18
  • i want generate a bunch of random codes programmatically
    – rocky
    Commented Sep 12, 2013 at 14:21
  • From where would you like to do this, is this an external script? And what code do you have so far except the 2 lines above? Commented Sep 12, 2013 at 14:23
  • 1
    Give this a try: marius-strajeru.blogspot.com/2010/04/…
    – Marius
    Commented Sep 12, 2013 at 14:24
  • @SanderMangel: i have a page, when customer access to it, they enter their email and system will generate a coupon code for them.
    – rocky
    Commented Sep 12, 2013 at 14:29

2 Answers 2

12

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();
3
  • thanks, very detail although it seem not correct with my question, but it's a way
    – rocky
    Commented Sep 12, 2013 at 15:44
  • 1
    To just generate (one) coupon make the "qty" field 1. Use the collection as prescribed and issue "getLastItem"
    – philwinkle
    Commented Sep 12, 2013 at 19:21
  • 1
    And to get the last coupon code you can call getLastItem()->getData('code') Commented Jun 28, 2016 at 10:50
2

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.