2

I would like to override the cart controller in Magento 1.9.2 in order to change the wording of the coupon session messages. There are lots of tutorials and answers which I have gone through, but my override does not seem to be working, I’m not sure if the location of my files are correct.

New Module: app/etc/modules/My_Module.xml

<?xml version="1.0"?>
<config>
    <modules>
        <My_Module>
            <active>true</active>
            <codePool>local</codePool>
        </My_Module>
    </modules>
</config>

Config file: app/code/local/My/Module/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <My_Module>
            <version>0.1.0</version>
        </My_Module>
    </modules>
    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <My_Module before="Mage_Checkout”>My_Module</My_Module>
                    </modules>
                </args>
            </checkout>
        </routers>
    </frontend>
</config>

New controller app/code/local/My/Module/controllers/CartController.php

<?php

require_once 'Mage/Checkout/controllers/CartController.php';

class My_Module_CartController extends Mage_Checkout_CartController
{

    /**
     * Initialize coupon
     */
    public function couponPostAction()
    {
        /**
         * No reason continue with empty shopping cart
         */
        if (!$this->_getCart()->getQuote()->getItemsCount()) {
            $this->_goBack();
            return;
        }

        $couponCode = (string) $this->getRequest()->getParam('coupon_code');
        if ($this->getRequest()->getParam('remove') == 1) {
            $couponCode = '';
        }
        $oldCouponCode = $this->_getQuote()->getCouponCode();

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

        try {
            $codeLength = strlen($couponCode);
            $isCodeLengthValid = $codeLength && $codeLength <= Mage_Checkout_Helper_Cart::COUPON_CODE_MAX_LENGTH;

            $this->_getQuote()->getShippingAddress()->setCollectShippingRates(true);
            $this->_getQuote()->setCouponCode($isCodeLengthValid ? $couponCode : '')
                ->collectTotals()
                ->save();

            if ($codeLength) {
                if ($isCodeLengthValid && $couponCode == $this->_getQuote()->getCouponCode()) {
                    $this->_getSession()->addSuccess(
                        $this->__('Promo code "%s" was applied.', Mage::helper('core')->escapeHtml($couponCode))
                    );
                } else {
                    $this->_getSession()->addError(
                        $this->__('Promo code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode))
                    );
                }
            } else {
                $this->_getSession()->addSuccess($this->__('Promo code was cancelled.'));
            }

        } catch (Mage_Core_Exception $e) {
            $this->_getSession()->addError($e->getMessage());
        } catch (Exception $e) {
            $this->_getSession()->addError($this->__('Cannot apply the promo code.'));
            Mage::logException($e);
        }

        $this->_goBack();
    }
}

The only thing I have changed in the controller is the word ‘coupon’ to ‘promo’ I do not have any error messages.

Thanks

1
  • Without answering the actual issue, if you want to just change the text it would be a whole lot easier to just use the translation system and add those to a translation CSV file
    – Andrew
    Oct 31, 2016 at 13:31

1 Answer 1

0

Rather than answer the question about how to get this override working, I am suggesting a better / simpler way to do what you need to do.

You should be using the build in magento translation system to change the text. notice all text wrapped inside __('') method, this means Magento can translate this for us.

You will find csv transaltions inside your locale for your language set, for example:

app/locale/[en_US]/Mage_Checkout.csv

Where [en_Us] is your locale

You will notice this directory contains csv files for each (most) modules to separate the text you want to change. These files contain two columns, the first column contains the original text (text as inside the source code / php) and the second column is the value you want to be displayed instead (translation) for that particular locale.

You can either edit (if the row already exists) or add new rows.

"Coupon code "%s" was applied.","Promo code "%s" was applied, bla bla"

Having this line inside the Mage_Checkout.csv file would then translate the instances of the first column into the second column when using inside the checkout module.

This is certainly the way to go rather than overriding a controller for this reason. What would you do if you needed multiple language store fronts? You'd be stuck then :)

0

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.