I have a quick question on magento coupon code generator. Please see the below method in core/Mage/SalesRule/Model/Rule.php:

 * Acquire coupon instance
 * @param bool $saveNewlyCreated Whether or not to save newly created coupon
 * @param int $saveAttemptCount Number of attempts to save newly created coupon
 * @return Mage_SalesRule_Model_Coupon|null
public function acquireCoupon($saveNewlyCreated = true, $saveAttemptCount = 10)
    if ($this->getCouponType() == self::COUPON_TYPE_NO_COUPON) {
        return null;
    if ($this->getCouponType() == self::COUPON_TYPE_SPECIFIC) {
        return $this->getPrimaryCoupon();
    /** @var Mage_SalesRule_Model_Coupon $coupon */
    $coupon = Mage::getModel('salesrule/coupon');
        ->setUsageLimit($this->getUsesPerCoupon() ? $this->getUsesPerCoupon() : null)
        ->setUsagePerCustomer($this->getUsesPerCustomer() ? $this->getUsesPerCustomer() : null)

    $couponCode = self::getCouponCodeGenerator()->generateCode();

    $ok = false;
    if (!$saveNewlyCreated) {
        $ok = true;
    } else if ($this->getId()) {
        for ($attemptNum = 0; $attemptNum < $saveAttemptCount; $attemptNum++) {
            try {
            } catch (Exception $e) {
                if ($e instanceof Mage_Core_Exception || $coupon->getId()) {
                    throw $e;
                    $couponCode .
                    self::getCouponCodeGenerator()->getDelimiter() .
                    sprintf('%04u', rand(0, 9999))
            $ok = true;
    if (!$ok) {
        Mage::throwException(Mage::helper('salesrule')->__('Can\'t acquire coupon.'));

    return $coupon;

I can see it has 10 attempts by default. However if I generate a code which exists in the database, I get an exception immediately:

a:5:{i:0;s:41:"Coupon with the same code already exists.";i:1;s:3513:"#0 /var/www/html/magento1/app/code/core/Mage/Core/Model/Resource/Db/Abstract.php(676)

Is there any way to prevent this without touching the core code? And in case the coupon code exists - should there not be another try as there are 10 attempts?

  • That's an interesting question. Do you use that method in a custom module ? Because that acquireCoupon method is not used anywhere in Magento CE codebase Oct 25, 2016 at 14:18
  • Hello, yes I am using acquireCoupon method in my custom extension. So far it does look like there is no way around it. Oct 25, 2016 at 15:55
  • Are you on Enterprise ? Oct 25, 2016 at 17:39

1 Answer 1


I think you should not use that acquireCoupon method. Indeed, this method is not used anywhere in Magento CE core code but it's used in EE and I'm afraid it might be an old / unremoved / buggy method. I would be very interested to see why it's here.

If you want to use the mass coupon generator, here is how you should do it:

// Load your salesrule
$rule = Mage::getModel('salesrule/rule')->load($ruleId);
// Get the coupon mass generator
$generator = $rule->getCouponMassGenerator();
// Set the generator data
$data = array(
    'qty' => 1, //number of coupons to generate
    'length' => 10, //length of coupon string
    'prefix' => "test_",
    'format' => Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC,
    'rule_id' => $ruleId //the id of the rule you will use as a template
// Validate the data
if ($generator->validateData($data)) {
    $generatedCount = $generator->getGeneratedCount();

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.