0

This question is for understanding and clarification purposes.

I have installed Magento CE 1.9.3.1 in my local windows 7 64bit system.

Now I have overrided the CartController through community module as below: magento_root\app\code\community\Company\Customcheckout\etc\config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Company_Customcheckout>
            <version>1.0.0</version>
        </Company_Customcheckout>
    </modules>
    <frontend>
        <routers>
            <Company_Customcheckout>
                <use>standard</use>
                <args>
                    <module>Company_Customcheckout</module>
                    <frontName>customcheckout</frontName>
                </args>
            </Company_Customcheckout>
        </routers>
    </frontend>
    <global>
        <routers>
            <checkout>
                <rewrite>
                    <cart>
                        <to>customcheckout/cart</to>
                        <override_actions>true</override_actions>
                        <actions>
                            <add>
                                <to>customcheckout/cart/add</to>
                            </add>
                            <index>
                                <to>customcheckout/cart/index</to>
                            </index>
                        </actions>
                    </cart>
                </rewrite>
            </checkout>
        </routers>
    </global>
</config>

magento_root\app\code\community\Company\Customcheckout\controllers\CartController.php

<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class Company_Customcheckout_CartController extends Mage_Checkout_CartController {
    public function _construct() {}
    public function addAction() {
        parent::addAction();
    }
    public function indexAction() {
        parent::indexAction();
    }
}

and the app/etc/modules directory has also the relevant module's xml file.

Now when I try to add any product(from product listing page) to cart I am getting 503 Service Unavailable error which is just title of error: Invalid form key. If I disable the module (i.e. disable the override) then the add to cart and redirect to cart works fine.

That makes me wonder, why it would give me Invalid form key error when I have just overrided the CartController class which has core CartController.php class required(path is fully correct) and I am accessing it's methods by parent keyword ? Firstly is it due to parent-child hierarchy of PHP ? Please provide a detailed explanation.

1 Answer 1

1

You should overwrite the cart controller using the xml below if you still want to keep the same URL checkout/cart. Otherwise, you will need to update all the URLs on the frontend to point to customcheckout/cart.

<?xml version="1.0" encoding="UTF-8"?>

<config>
    <modules>
        <Company_Customcheckout>
            <version>1.0.0</version>
        </Company_Customcheckout>
    </modules>

    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <Company_Customcheckout before="Mage_Checkout">Company_Customcheckout</Company_Customcheckout>
                    </modules>
                </args>
            </checkout>
        </routers>
    </frontend>
</config>

Also, it's best practice to use the getModuleDir method when requiring a file from another module:

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Checkout') . DS . 'CartController.php';

class Company_Customcheckout_CartController extends Mage_Checkout_CartController {
    public function addAction() {
        return parent::addAction();
    }

    public function indexAction() {
        return parent::indexAction();
    }
}

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.