0

I use this method to generate coupons with a specific SalesRule template that is existing.

/**
     * @param null|Mage_SalesRule_Model_Rule $rule
     * @param int $amount
     * @throws Mage_Core_Model_Store_Exception
     */
    public function createCoupons($rule = null, $amount = 5)
    {
        if ($rule == null && !is_null($this->salesRuleObject)) {
            $rule = $this->getRule();
        }

        /** @var Mage_SalesRule_Model_Coupon_Massgenerator $generator */
        $generator = Mage::getModel('salesrule/coupon_massgenerator');
        $data = [
            'max_probability' => .25,
            'max_attempts' => 5,
            'uses_per_customer' => 1,
            'uses_per_coupon' => 1,
            'qty' => $amount,
            'length' => 8,
            'format' => Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC,
            'dash_every_x_characters' => 4,
            'prefix' => "PH-",
            'rule_id' => $rule->getId()
        ];

        if ($generator->validateData($data)) {
            $generator->setDash($data['dash_every_x_characters'])
                ->setPrefix($data['prefix'])
                ->setData($data)
                ->generatePool();
            $rule->setCouponCodeGenerator($generator);
        };
    }

They are generated properly and inserted into my Database, which I can verify with SELECT * FROM salesrule_coupon -> it has 5 coupons after I run this method.

However, if I try to use one of the coupon in the cart it keeps show me message coupon code is invalid.

Whats wrong with the generation of the codes? Am I missing something? Does the rule needs to be worked on after generate the code?

Further information on this topic: Magento generate coupon codes programmatically

0

Use following code to create coupon programatically. I had used this code before, it is a tested code. It will surely work

Set parameters accordingly

$generator = Mage::getModel('salesrule/rule');
$coupon_code = 'abc-xyz';
$data = array(
    'product_ids' => null,
    'name' => $value,
    'description' => null,
    'is_active' => 1,
    'website_ids' => array(1),
    'customer_group_ids' => array(0,1,2,4), //Available customer groups
    'coupon_type' => 2,
    'coupon_code' => $value,
    'uses_per_coupon' => 1,
    'uses_per_customer' => 1,
    'from_date' => null,
    'to_date' => null,
    'sort_order' => null,
    'is_rss' => 0,
    'rule' => array(
        'conditions' => array(
            array(
                'type' => 'salesrule/rule_condition_combine',
                'aggregator' => 'all',
                'value' => 1,
                'new_child' => null
            )
        ),
        'actions' => array(
            array(
                'type' => 'salesrule/rule_condition_product_combine',
                'aggregator' => 'all',
                'value' => 1,
                'new_child' => null
            )
        )
    ),
    'simple_action' => cart_fixed, //Fixed amount discount for whole cart
    'discount_amount' => 100,
    'discount_qty' => null,
    'discount_step' => 0,
    'apply_to_shipping' => 0,
    'simple_free_shipping' => 0,
    'stop_rules_processing' => 0,
    'store_labels' => $coupon_code
);
$validateResult = $generator->validateData(new Varien_Object($data));
if ($validateResult == true) {
    $generator->loadPost($data);
    if($generator->save()){
        return $generator;
    }
}
  • This will create a new Rule entity each time I want to generate a Rule which fills the Database in the long run. My approach is to keep loading on DB as low as possible. I fixed my issue by adding customer_group_ids. – zerokelvin Feb 3 '19 at 21:23
0

Turns out that I forgot to properly set up the customer_group_id attribute in the rule properly.

$salesrule->setCustomerGroupId([0,1,2])->save()

This made usage for all groups possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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