1

I want to apply the coupon on the specific product I tried:

use Magento\SalesRule\Model\Rule;
 public function __construct(
    \Magento\Framework\App\Helper\Context $helpercontext,
    Rule $rule,
 ){
    $this->_rule = $rule;
    parent::__construct($helpercontext);
 }
 public function setCoupon()
{
    // $this->_state->setAreaCode('adminhtml');  
    // $this->_state->setAreaCode('frontend');  


    $coupon['name'] = 'Offer_asad2';
    $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'] ='NL04-1234'; //this code will normally be autogenerated 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();
}

Now the issue is this code is applying on every single product->setProductIds(array(1,2,3)) and ->setProductIds(array('1','2','3')) but this is not working for me,
How can I apply the I set the coupon for the specific products?

9
  • use setProductId, without 's' – fmsthird Mar 15 '19 at 12:12
  • you use it for one specific product but for more (array of)product, you use with 's'. – fmsthird Mar 15 '19 at 12:29
  • Yeah at the above scenario its I write it setProductIds n I pass the single product value but at that scenario it not gives me any error but in other cases as I mention above giver me an error Array to String how can I solve this? – Asad Khan Mar 15 '19 at 13:06
  • ahh okay, you have to put it inside bracket to indicate it as an array like this: setProductIds([1, 2, 3]) – fmsthird Mar 15 '19 at 13:22
  • Sir Still have the same Problem Notice: Array to string conversion in /home2/mako/public_html/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php on line 228 – Asad Khan Mar 15 '19 at 15:09
0

So at Last I solve this mystery we cannot apply the coupon on the specific product by using getProductIds() to apply the coupon on the specific product we have to add few conditions after the basic coupon code

 $conditions["1"] = array
                (
                "type" => "Magento\SalesRule\Model\Rule\Condition\Combine",
                "aggregator" => "all",
                "attribute" => null,
                "operator" => null,
                "value" => 1,
                "is_value_processed" => null,
            );
            $conditions["1--1"] = array
                (
                "type" => "Magento\SalesRule\Model\Rule\Condition\Product\Found",
                "attribute" => null,
                "operator" => null,
                "value" => 1,
                "is_value_processed" => null,
                "aggregator" => "all",
            );
            $conditions["1--1--1"] = array
                (
                "type" => "Magento\SalesRule\Model\Rule\Condition\Product",
                "attribute" => "sku",
                "operator" => "==",
                // "operator" => "()",
                // "value" => $couponData['general']['product_id']
                "value" => $this->getSKUU(),
            );
            $conditions["1--1--1-1"] = array
                (
                "type" => "Magento\SalesRule\Model\Rule\Condition\Product",
                // "attribute" => "quote_item_row_total",
                "attribute" => "quote_item_qty",
                "operator" => "==",
                "value" => 1
            );

            $actions = array(
                "1" => array(
                        "type"          => "Magento\SalesRule\Model\Rule\Condition\Product",
                        "aggregator"    => "all",
                        "value"         => "1",
                        "new_child"     => false
                ),
                "1--1" => array(
                        "type"          => "Magento\SalesRule\Model\Rule\Condition\Product",
                        "attribute"     => "sku",
                        'operator' => '==',
                        'value' => $this->getSKUU()
                )
            );

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.