2

How to override collect() using after Plugin in magento2. I need to set custom description in totals.

Magento\SalesRule\Model\Quote\Discount

use Magento\SalesRule\Model\Quote\Discount as Source;

class Discount
{

    public function afterCollect(Source $subject, $result, \Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\ShippingAssignment $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total)
    {
        $items = $shippingAssignment->getItems();
        if (!count($items)) {
            return $subject;
        }
        $address = $shippingAssignment->getShipping()->getAddress();
        $total->setDiscountDescription(implode(",", $address->getFullDescription()));
        return $subject;
    }

}
2
  • Please add the code which you have tried so far, so someone can find what you are doing wrong. Jul 23 '18 at 7:07
  • Updated my code
    – Mahi M
    Jul 23 '18 at 7:32
0

Instead of return $subject return $result

use Magento\SalesRule\Model\Quote\Discount as Source;

class Discount
{

    public function afterCollect(Source $subject, $result, \Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\ShippingAssignment $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total)
    {
        $items = $shippingAssignment->getItems();
        if (!count($items)) {
            return $subject;
        }
        $address = $shippingAssignment->getShipping()->getAddress();
        $total->setDiscountDescription(implode(",", $address->getFullDescription()));
        return $result;
    }

}
1

Instead of using after plugin, you should use around plugin to fulfill your requirements. around plugin will allow to manipulate your parameters as well as the result of the function.

The other thing you are doing wrong is the return parameter. after plugin should return $result instead of $subject Try like.

use Magento\SalesRule\Model\Quote\Discount as Source;

class Discount
{
    public function aroundCollect(Source $subject, 
        \Closure $proceed, 
        \Magento\Quote\Model\Quote $quote, 
        \Magento\Quote\Model\ShippingAssignment $shippingAssignment, 
        \Magento\Quote\Model\Quote\Address\Total $total)
    {
        $result = $proceed($quote, $shippingAssignment, $total);
        $items = $shippingAssignment->getItems();
        if (!count($items)) {
            return $result;
        }
        $address = $shippingAssignment->getShipping()->getAddress();
        $total->setDiscountDescription(implode(",", $address->getFullDescription()));
        return $result;
    }
}
7
  • Why we go for around Plugin?
    – Mahi M
    Jul 23 '18 at 8:20
  • Here you need to update your parameter variables. after plugin will only allow you to manipulate the result of the function, while around will allow to change the input parameters as well as the output results. Jul 23 '18 at 8:23
  • Ok .... ........
    – Mahi M
    Jul 23 '18 at 8:39
  • I have used around plugin ,It will throw the error Uncaught ArgumentCountError: Too few arguments to function Magento\\SalesRule\\Model\\Quote\\Discount::collect(),
    – Mahi M
    Jul 23 '18 at 9:19
  • @ManoM I have updated the answer. Please try to use updated code. In my previous answer there were arguments missing in proceed function. Jul 23 '18 at 9:28

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.