0

I have created a plugin to add custom functionaly before Coupon apply it's working fine on checkout page but not working on cart page: /etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Model\CouponManagement">
        <plugin name="checkout_coupon_apply" type="Geekay\Rewardpoint\Plugin\CheckoutCouponApply"/>
    </type>

/Plugin/CheckoutCouponApply.php

<?php 
    
    namespace Geekay\Rewardpoint\Plugin;
    use Magento\Framework\Exception\NoSuchEntityException;
    class CheckoutCouponApply {
    
            protected  $_customerSession;
            protected  $_cart;       
            protected  $_rewardHelper;
            protected  $_jsonFactory;
            protected  $_rule;
            protected  $_storeManager;
            protected  $_coupon;
            protected $logger;
            public function __construct(       
                \Magento\Customer\Model\Session $customerSession,
                \Magento\Checkout\Model\Cart $cart,
                \Geekay\Rewardpoint\Helper\Data $asplHelper,
                \Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
                \Magento\SalesRule\Model\Rule $rule,
                \Magento\SalesRule\Model\Coupon $coupon,
                \Magento\Store\Model\StoreManagerInterface $storeManager,
                \Psr\Log\LoggerInterface $logger
            ) {            
                $this->_customerSession = $customerSession;       
                $this->_cart = $cart;                
                $this->_rewardHelper = $asplHelper;
                $this->_jsonFactory = $jsonFactory;
                $this->_storeManager = $storeManager;
                $this->_rule = $rule;
                $this->_coupon = $coupon;
                $this->logger = $logger;
    
            }
        public function beforeSet(\Magento\Quote\Model\CouponManagement $subject, $cartId, $couponCode)
        { 
    // code here
    }
    }

What's wrong here?

1 Answer 1

1

Magento\Quote\Model\CouponManagement will not work on cart page because on cart page a separate controller is calling i.e vendor/magento/module-checkout/Controller/Cart/CouponPost.php

On checkout page, API (http://{{m2url}}/rest/default/V1/carts/mine/coupons/1234 ) is triggering and calling Magento\Quote\Model\CouponManagement set method

You also need to adjust execute method from controller

Hope above will help!

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.