Magento – Change success message on applying coupon code

couponmagento-2.1messagessuccess-message

In Magento 2.x, By default, we can set coupon codes from Marketing->Cart Price Rules.

I've added a test code and it's working as expected. When I added the code in cart page (mysite.com/checkout/cart) ,

I see the success message as follows:-

You used coupon code "TestCode1".

How to change this message to include whatever text I need to put?

Best Answer

You can override File named CouponPost.php.

To achieve this you can create preference for this.

Vendor/Module/etc/di.xml

<preference for="Magento\Checkout\Controller\Cart\CouponPost" type="Vednor\Module\Controller\Rewrite\Cart\CouponPost" />

Vendor/Module/Controller/Rewrite/Cart/CouponPost.php

namespace Vednor\Module\Controller\Rewrite\Cart;

use Magento\Framework\App\Action\Context;

class CouponPost extends \Magento\Framework\App\Action\Action
{

public function __construct(
    Context $context,
    ......
    \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
) {
    parent::__construct($context);

    .......
}


public function execute()
{

    // Keep all code as it is, Just change message as per your need.

}
}

Hope it will help you. !!

Related Topic