2

I have added a plugin which should be triggered when a coupon code is applied. Sadly nothing happens, no errors too.

Where did I go wrong?

<?php

namespace Zygis\TestModule\Plugin;

class CheckoutCouponApply {

    public function beforeSet(\Magento\Quote\Model\CouponManagement $subject, $cartId, $couponCode)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $logger        = $objectManager->create('\Psr\Log\LoggerInterface');
        $logger->debug('test before set');
    }

    public function afterSet(\Magento\Quote\Model\CouponManagement $subject, $cartId, $couponCode)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $logger        = $objectManager->create('\Psr\Log\LoggerInterface');
        $logger->debug('test after set');
    }
}

And obviously I got it registered in Zygis\TestModule\etc\frontend\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="CheckoutCouponApply" type="Zygis\TestModule\Plugin\CheckoutCouponApply" sortOrder="1" />
    </type>
</config>

Any help is appreciated! Thanks!

4
  • Sadly you are on the wrong path for coupone code. What is your primary goal for plugin?
    – Keyur Shah
    Jul 28, 2016 at 9:20
  • I want to change the error message for an expired coupon code later. However now I just want to see the plugin being fired. Please elaborate why I'm on the wrong path? Jul 28, 2016 at 9:25
  • I have found the right way. I created a preference instead of plugin. Jul 28, 2016 at 12:44
  • Did you found the solution using plugin way? Jul 8, 2019 at 6:11

1 Answer 1

0

If anybody stumbles across this in the future, make sure your di.xml file for the REST endpoints resides in the etc directory, not the etc/frontend directory.

BAD

Vendor\Module\etc\frontend\di.xml

GOOD

Vendor\Module\etc\di.xml

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.