1

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?

  • 1
    Simplest way first check inline edit option, then try to find the sentence in side the checkout js files, – Lasantha May 9 '18 at 7:13
2

Just update text in your translation CSV file like below example

"You used coupon code ""%1"".","Your custom text ""%1""."

Translation file something like below

app/design/frontend/Vendor/ThemeName/i18n/en_US.csv

  • I tried this solution, but sometimes translation csv is not working properly in my theme. maybe a problem with my theme. I also tried inline translation, which worked for me in this instance. anyway thank you for helping out – Joey May 9 '18 at 8:05
  • Do you have multi language? – Suresh Chikani May 9 '18 at 8:33
  • I have UK and US – Joey May 9 '18 at 8:43
  • Which command do you use for deployment? – Suresh Chikani May 9 '18 at 8:43
  • 1
    Run this command and check your CSV changes : php bin/magento setup:upgrade php bin/magento setup:static-content:deploy en_GB en_US – Suresh Chikani May 9 '18 at 9:09
1

You can change this message from vendor/magento/module-checkout/Controller/Cart/CouponPost.php file.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Checkout\Controller\Cart;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class CouponPost extends \Magento\Checkout\Controller\Cart
{
    /**
     * Sales quote repository
     *
     * @var \Magento\Quote\Api\CartRepositoryInterface
     */
    protected $quoteRepository;

    /**
     * Coupon factory
     *
     * @var \Magento\SalesRule\Model\CouponFactory
     */
    protected $couponFactory;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param \Magento\Checkout\Model\Session $checkoutSession
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
     * @param \Magento\Checkout\Model\Cart $cart
     * @param \Magento\SalesRule\Model\CouponFactory $couponFactory
     * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
     * @codeCoverageIgnore
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\SalesRule\Model\CouponFactory $couponFactory,
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
    ) {
        parent::__construct(
            $context,
            $scopeConfig,
            $checkoutSession,
            $storeManager,
            $formKeyValidator,
            $cart
        );
        $this->couponFactory = $couponFactory;
        $this->quoteRepository = $quoteRepository;
    }

    /**
     * Initialize coupon
     *
     * @return \Magento\Framework\Controller\Result\Redirect
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    public function execute()
    {
        $couponCode = $this->getRequest()->getParam('remove') == 1
            ? ''
            : trim($this->getRequest()->getParam('coupon_code'));

        $cartQuote = $this->cart->getQuote();
        $oldCouponCode = $cartQuote->getCouponCode();

        $codeLength = strlen($couponCode);
        if (!$codeLength && !strlen($oldCouponCode)) {
            return $this->_goBack();
        }

        try {
            $isCodeLengthValid = $codeLength && $codeLength <= \Magento\Checkout\Helper\Cart::COUPON_CODE_MAX_LENGTH;

            $itemsCount = $cartQuote->getItemsCount();
            if ($itemsCount) {
                $cartQuote->getShippingAddress()->setCollectShippingRates(true);
                $cartQuote->setCouponCode($isCodeLengthValid ? $couponCode : '')->collectTotals();
                $this->quoteRepository->save($cartQuote);
            }

            if ($codeLength) {
                $escaper = $this->_objectManager->get(\Magento\Framework\Escaper::class);
                $coupon = $this->couponFactory->create();
                $coupon->load($couponCode, 'code');
                if (!$itemsCount) {
                    if ($isCodeLengthValid && $coupon->getId()) {
                        $this->_checkoutSession->getQuote()->setCouponCode($couponCode)->save();
                        $this->messageManager->addSuccess(
                            __(
                                'You used coupon code "%1".',
                                $escaper->escapeHtml($couponCode)
                            )
                        );
                    } else {
                        $this->messageManager->addError(
                            __(
                                'The coupon code "%1" is not valid.',
                                $escaper->escapeHtml($couponCode)
                            )
                        );
                    }
                } else {
                    if ($isCodeLengthValid && $coupon->getId() && $couponCode == $cartQuote->getCouponCode()) {
                        $this->messageManager->addSuccess(
                            __(
                                'You used coupon code "%1".',
                                $escaper->escapeHtml($couponCode)
                            )
                        );
                    } else {
                        $this->messageManager->addError(
                            __(
                                'The coupon code "%1" is not valid.',
                                $escaper->escapeHtml($couponCode)
                            )
                        );
                    }
                }
            } else {
                $this->messageManager->addSuccess(__('You canceled the coupon code.'));
            }
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addError($e->getMessage());
        } catch (\Exception $e) {
            $this->messageManager->addError(__('We cannot apply the coupon code.'));
            $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
        }

        return $this->_goBack();
    }
}

You can override this controller file by custom module.

Follow this link: https://www.magestore.com/magento-2-tutorial/magento-2-controller-override-tutorial/

  • Thanks for the answer. so far this is the most complete answer. if you can please improve this little more, so someone else can benefit from this – Joey May 9 '18 at 8:04
1

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. !!

  • You mean first I should copy all the content from CouponPost.php right? I think this can be a better answer if you can somewhat improve this to be a complete answer. start from beginning. Where is CouponPost.php file located, This is about creating a custom module. What files(except the default files) should be there etc.. – Joey May 9 '18 at 8:03
  • 1
    Check here. github.com/TBSMage/StackExchange225264 – user67337 May 9 '18 at 9:41
  • Thanks..This will absolutely help more than just me – Joey May 9 '18 at 10:49
  • @Joey, If it resolve your problem, Give upvote and accept answer to help others. – user67337 May 9 '18 at 10:51

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.