1

I create the coupon function in Helper which is

public function setCoupon()
    {
        // $this->_state->setAreaCode('adminhtml');  
        // $this->_state->setAreaCode('frontend');  




   $coupon['name'] = 'Offer';
    $coupon['desc'] = 'Discount for vip signup coupon.';
    $coupon['start'] = date('Y-m-d');
    $coupon['end'] = '';
    $coupon['max_redemptions'] = 1;
    $coupon['discount_type'] ='by_fixed';
    $coupon['discount_amount'] = 15;
    $coupon['flag_is_free_shipping'] = 'no';
    $coupon['redemptions'] = 1;
    $coupon['code'] ='NL02-1234'; //this code will normally be autogenetated but i am hard coding for testing purposes  

    $this->_rule->setName($coupon['name'])
            ->setDescription($coupon['desc'])
            ->setFromDate($coupon['start'])
            ->setToDate($coupon['end'])
            ->setUsesPerCustomer($coupon['max_redemptions'])
            ->setCustomerGroupIds(array('0','1','2','3',))
            ->setIsActive(1)
            ->setSimpleAction($coupon['discount_type'])
            ->setDiscountAmount($coupon['discount_amount'])
            ->setDiscountQty(1)
            ->setApplyToShipping($coupon['flag_is_free_shipping'])
            ->setTimesUsed($coupon['redemptions'])
            ->setWebsiteIds(array('1'))
            ->setCouponType(2)
            // ->setProductIds(array(1,2,3));
            // ->setProductIds(array('1','2','3'))
            ->setProductIds(1)
            ->setCouponCode($coupon['code'])
            ->setUsesPerCoupon(NULL);
    $this->_rule->save();
}

where $this->_rule is object of Magento\SalesRule\Model\Rule class it works perfectly but you guys can see that I tried setProductIds to set it for specific products but when it was in array it show me an error Array to String and when I tried setProductIds only for one product Id then it not gives me any error but the coupon works on the every product. How can I set this???

1 Answer 1

0

use Magento\SalesRule\Model\Data\Rule class and function setProductIds() which will accept param type array but function setProductIds() inside Magento\SalesRule\Model\Rule accecpt only string not array. please check core files for more information.

6
  • OK its fine but as you can see that I also apply the single product ID but after that the coupon works for every single Product. Isn't it needs to be only apply on the single product which I mention in setProductIds()???
    – Asad Khan
    Commented Mar 8, 2019 at 11:46
  • just quick question.. is product id 1 is exist? Commented Mar 8, 2019 at 12:01
  • if produtct id not exist it will not set any condition so it will be global coupen code. Commented Mar 8, 2019 at 12:02
  • check column "conditions_serialized" in db table "salesrule" , based on the condition it will apply. Commented Mar 8, 2019 at 12:07
  • Yes it exists..
    – Asad Khan
    Commented Mar 8, 2019 at 13:32

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.