0

I need to give Quote Item level manual Discount for selected cart items. After giving discount Cart Item row total will change. As can't modify the product price so custom price solution is also not working.

I tried with a new discount total but it gives a discount on the whole cart.

Anyone, please suggest the best approach for giving a discount on Cart item level without modifying product price.

2

1 Answer 1

0

You can create a plugin for Magento\SalesRule\Model\Validator::process -

    class Myplugin
       {

        protected $discountFactory;

        public function __construct(\Magento\SalesRule\Model\Rule\Action\Discount\DataFactory $discountFactory){
        $this->discountFactory = $discountFactory;
    }

    public function aroundProcess(
        \Magento\SalesRule\Model\Validator $subject,
        \Closure $proceed,
        $item,
        \Magento\SalesRule\Model\Validator $validator
    ){
        $returnValue = $proceed($item);

        $discountData = $this->discountFactory->create();

        $discountData->setAmount('[my_discount_amount]');
        $discountData->setBaseAmount('[my_discount_amount]');
        $discountData->setOriginalAmount('[my_discount_amount]');
        $discountData->setBaseOriginalAmount('[my_discount_amount]');

        $this->setDiscountData($discountData, $item);

        return $returnValue;
    }
}

Replace the [my_discount_amount] with your discount amount.

4
  • 1
    Thanks. I am getting Call to undefined method setDiscountData error. Any idea ? Commented Jun 6, 2019 at 9:36
  • hello, @NoamN pls update full code, also this code sets the mannual discount on each quote item? Commented Jul 30, 2019 at 10:47
  • Hi, you should add cart rule condition in marketting -> cart price rules -> rule name -> "actions" tab rule configuration. There it will apply on cart item level instead of entire cart. Thanks.
    – Sachin
    Commented Mar 8, 2022 at 9:14
  • Facing same issue to give discount to individual item any idea ? magento.stackexchange.com/questions/365333/… Commented Mar 14, 2023 at 13:40

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.