How to overload a controller
In this example we’re overloading Mage_Checkout_CartController::indexAction().
1. Create your module folders and files |
Magento/app/code/local/MyNameSpace/MyModule/etc/config.xml Magento/app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php Magento/app/etc/modules/MyNameSpace_All.xml
2. Edit /etc/config.xml |
Go to Magento/app/code/local/MyNameSpace/MyModule/etc/config.xml and paste the following xml into it (comments I’m not a 100% sure about are ending with “(?)”):
- <?xml version="1.0"?>
- <config>
- <modules>
- <MyNameSpace_MyModule>
- <version>0.1.0</version>
- </MyNameSpace_MyModule>
- </modules>
- <global>
- <!-- This rewrite rule could be added to the database instead -->
- <rewrite>
- <!-- This is an identifier for your rewrite that should be unique -->
- <mynamespace_mymodule_checkout_cart>
- <from><![CDATA[#^/checkout/cart/$#]]></from>
- <!--
- - mymodule matches the router frontname below
- - checkout_cart matches the path to your controller
- Considering the router below, "/mymodule/checkout_cart/" will be
- "translated" to "/MyNameSpace/MyModule/controllers/Checkout/CartController.php" (?)
- -->
- <to>/mymodule/checkout_cart/</to>
- </mynamespace_mymodule_checkout_cart>
- </rewrite>
- </global>
- <!--
- If you want to overload an admin-controller this tag should be <admin> instead,
- or <adminhtml> if youre overloading such stuff (?)
- -->
- <frontend>
- <routers>
- <mynamespace_mymodule>
- <!-- should be set to "admin" when overloading admin stuff (?) -->
- <use>standard</use>
- <args>
- <module>MyNameSpace_MyModule</module>
- <!-- This is used when "catching" the rewrite above -->
- <frontName>mymodule</frontName>
- </args>
- </mynamespace_mymodule>
- </routers>
- </frontend>
- </config>
3. Edit /controllers/Checkout/CartController.php |
Paste the following php code into Magento/app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php (the only change we’re doing to the indexAction() is adding an error_log() message):
- <?php
- # Controllers are not autoloaded so we will have to do it manually:
- require_once 'Mage/Checkout/controllers/CartController.php';
- class MyNameSpace_MyModule_Checkout_CartController extends Mage_Checkout_CartController
- {
- # Overloaded indexAction
- public function indexAction()
- {
- # Just to make sure
- error_log('Yes, I did it!');
- $cart = $this->_getCart();
- $cart->init();
- $cart->save();
- $this->loadLayout();
- $this->_initLayoutMessages('checkout/session');
- if ($continueShoppingUrl = Mage::getSingleton('checkout/session')->getContinueShoppingUrl(true)) {
- }
- // elseif ($continueShoppingUrl = Mage::helper('catalog')->getLastViewedUrl()) {
- // }
- else {
- $continueShoppingUrl = Mage::getUrl();
- }
- if ($continueShoppingUrl) {
- $this->getLayout()->getBlock('checkout.cart')->setContinueShoppingUrl($continueShoppingUrl);
- }
- $this->renderLayout();
- }
- }
3. Edit Magento/app/etc/modules/MyNameSpace_All.xml |
(This is to activate your module)
- <?xml version="1.0"?>
- <config>
- <modules>
- <MyNameSpace_MyModule>
- <active>true</active>
- <codePool>local</codePool>
- </MyNameSpace_MyModule>
- </modules>
- </config>
4. Edit Magento/app/design/frontend/[myinterface]/[mytheme]/layout/checkout.xml |
Add the following to use the same update handle as before:
- <mymodule_checkout_cart_index>
- <update handle="checkout_cart_index"/>
- </mymodule_checkout_cart_index>
5. Point your browser to /checkout/cart/ |
Take a look in your php error log and you should find ‘Yes, I did it!’.



