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

I have this method to generate coupon code but how can I get a coupon code.

1 Answer 1

5

To get the last generated coupon code you will have to modify your collection like this:

$couponCode = Mage::getResourceModel('salesrule/coupon_collection')
                ->addRuleToFilter($salesRule)
                ->addGeneratedCouponsFilter()
                ->getLastItem()
                ->getData('code');

The two lines I've added get the last item and retrieve the corresponding code.

3
  • actually these two things, 1.generation 2.retreive are happening at two different times .will it be correct for high traffic site. because we are getting last generated coupon code . but if a request comes between the gap will it fail. normally will it happen or am I thinking wrong
    – user41437
    Jun 28, 2016 at 10:40
  • @HaimanoHellomano what do you mean ? Jun 28, 2016 at 10:40
  • @HaimanoHellomano your idea is totally right. This will fail if a request happens in between. However Magento does not provide any other way to do that. What you will have to do is to retrieve the code right after the generation (using my code), store it somewhere and do whatever you want with it after. Jun 28, 2016 at 10:47

Your Answer

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