2

How to add product in a cart with custom price 0 after coupon code success. what is the way to achieve this. pls help me with the couponcode success event name.

1 Answer 1

1

Reference Link

Yes there is.

The event you're looking for is controller_action_predispatch_checkout_cart_couponPost

This is generic event that is dispatched before the action class that adds/removes the coupon is called.

As this is the same event for both add and remove you can add the following code at the top of your observer method to check if this is a remove action:

$controller = $observer->getControllerAction();
$remove = $controller->getRequest()->getParam('remove');

if ($remove) {
    // This is a remove action
} else {
    // This is an add action
    $coupon = $controller->getRequest()->getParam('coupon_code');
}

However, when you add a coupon from the checkout flow it won't work as it directly calls the API and there are no events in Magento 2 web APIs

The solution is to use a plugin on Magento\Quote\Model\CouponManagement on the set method:

<?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="Vendor\Module\Plugin\CheckoutCouponApply"/>
    </type>
</config>

Then your plugin class:

<?php 

namespace Vendor\Module\Plugin;

class CheckoutCouponApply {

    public function beforeSet(CouponManagement $subject, $cartId, $couponCode)
    {
         // Do what you need to do
    }
}

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.