1

I am trying to override Magento\Quote\Model\CouponManagement core vendor file. What I am trying to achieve is change the default error message when the wrong coupon code is added.

My 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">

 <preference for="\Magento\Quote\Model\CouponManagement" type="Ecs\VendorOverride\Model\CouponManagement" /> 

</config>

My CouponManagement.php

<?php
/**
 *
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Ecs\VendorOverride\Model;

use \Magento\Quote\Api\CouponManagementInterface;
use \Magento\Framework\Exception\CouldNotDeleteException;
use \Magento\Framework\Exception\CouldNotSaveException;
use \Magento\Framework\Exception\NoSuchEntityException;

/**
* Coupon management object.
*/
class CouponManagement extends \Magento\Quote\Model\CouponManagement
{
/**
 * Quote repository.
 *
 * @var \Magento\Quote\Api\CartRepositoryInterface
 */
protected $quoteRepository;

/**
 * Constructs a coupon read service object.
 *
 * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository Quote repository.
 */
public function __construct(
    \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
) {

    $this->quoteRepository = $quoteRepository;

}

/**
 * {@inheritdoc}
 */
public function get($cartId)
{
    /** @var  \Magento\Quote\Model\Quote $quote */
    $quote = $this->quoteRepository->getActive($cartId);

    return $quote->getCouponCode();
}

/**
 * {@inheritdoc}
 */
public function set($cartId, $couponCode)
{ 
    /** @var  \Magento\Quote\Model\Quote $quote */
    $quote = $this->quoteRepository->getActive($cartId);
    if (!$quote->getItemsCount()) {
        throw new NoSuchEntityException(__('Cart %1 doesn\'t contain products', $cartId));
    }
    $quote->getShippingAddress()->setCollectShippingRates(true);

    try {
        $quote->setCouponCode($couponCode);
        $this->quoteRepository->save($quote->collectTotals());
    } catch (\Exception $e) {
        throw new CouldNotSaveException(__('Could not apply coupon code'));
    }
    if ($quote->getCouponCode() != $couponCode) {
        throw new NoSuchEntityException(__('Coupon code is not valid or expired'));
    }
    return true;
}

/**
 * {@inheritdoc}
 */
public function remove($cartId)
{
    /** @var  \Magento\Quote\Model\Quote $quote */
    $quote = $this->quoteRepository->getActive($cartId);
    if (!$quote->getItemsCount()) {
        throw new NoSuchEntityException(__('Cart %1 doesn\'t contain products', $cartId));
    }
    $quote->getShippingAddress()->setCollectShippingRates(true);
    try {
        $quote->setCouponCode('');
        $this->quoteRepository->save($quote->collectTotals());
    } catch (\Exception $e) {
        throw new CouldNotDeleteException(__('Could not delete coupon code'));
    }
    if ($quote->getCouponCode() != '') {
        throw new CouldNotDeleteException(__('Could not delete coupon code'));
    }
    return true;
}
}

I am able to print values inside get function but the set function is still returning value from vendor file.

Please help. Thanks

1 Answer 1

3

If you're looking to change the copy, it's probably best to achieve this through the translation system rather than overriding a class. The __() function in your code snippet that the copy is a parameter of is Magento's translation method which allows you to change the text via string replacement before it's rendered to the page.

You'll need to create a translation file for your language and you can place this in your theme or module.

For more information, I'd recommend reading the Translations overview on Magento dev docs here: https://devdocs.magento.com/guides/v2.2/frontend-dev-guide/translations/xlate.html

Otherwise, I'd suggest try clearing your cache and running di compilation (if you haven't already).

bin/magento cache:flush

bin/magento setup:di:compile

1
  • I tried using translation system and is able to do this.Thanks for pointing me out .
    – Melvin
    Jul 18, 2018 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.