Table of Contents

In this example we’re overloading Mage_Checkout_CartController::indexAction().

1. Create your module folders and files

  1. Magento/app/code/local/MyNameSpace/MyModule/etc/config.xml
  2. Magento/app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php
  3. 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 “(?)”):

  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <MyNameSpace_MyModule>
  5.             <version>0.1.0</version>
  6.         </MyNameSpace_MyModule>
  7.     </modules>
  8.     <global>
  9.         <!-- This rewrite rule could be added to the database instead -->
  10.         <rewrite>
  11.             <!-- This is an identifier for your rewrite that should be unique -->
  12.             <mynamespace_mymodule_checkout_cart>
  13.                 <from><![CDATA[#^/checkout/cart/#]]></from>
  14.                 <!--
  15.                     - mymodule matches the router frontname below
  16.                     - checkout_cart matches the path to your controller
  17.                    
  18.                     Considering the router below, "/mymodule/checkout_cart/" will be
  19.                     "translated" to "/MyNameSpace/MyModule/controllers/Checkout/CartController.php" (?)
  20.                 -->
  21.                 <to>/mymodule/checkout_cart/</to>
  22.             </mynamespace_mymodule_checkout_cart>
  23.         </rewrite>
  24.     </global>
  25.     <!--
  26.     If you want to overload an admin-controller this tag should be <admin> instead,
  27.     or <adminhtml> if youre overloading such stuff (?)
  28.     -->
  29.     <frontend>
  30.         <routers>
  31.             <mynamespace_mymodule>
  32.                 <!-- should be set to "admin" when overloading admin stuff (?) -->
  33.                 <use>standard</use>
  34.                 <args>
  35.                     <module>MyNameSpace_MyModule</module>
  36.                     <!-- This is used when "catching" the rewrite above -->
  37.                     <frontName>mymodule</frontName>
  38.                 </args>
  39.             </mynamespace_mymodule>
  40.         </routers>
  41.     </frontend>
  42. </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):

  1. <?php
  2. # Controllers are not autoloaded so we will have to do it manually:
  3. require_once 'Mage/Checkout/controllers/CartController.php';
  4. class MyNameSpace_MyModule_Checkout_CartController extends Mage_Checkout_CartController
  5. {
  6.     # Overloaded indexAction
  7.     public function indexAction()
  8.     {
  9.         # Just to make sure
  10.         error_log('Yes, I did it!');
  11.         parent::indexAction();
  12.     }
  13. }

3. Edit Magento/app/etc/modules/MyNameSpace_All.xml

(This is to activate your module)

  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <MyNameSpace_MyModule>
  5.             <active>true</active>
  6.             <codePool>local</codePool>
  7.         </MyNameSpace_MyModule>
  8.     </modules>
  9. </config>

4. Edit Magento/app/design/frontend/[myinterface]/[mytheme]/layout/checkout.xml

Add the following to use the same update handle as before:

  1. <mynamespace_mymodule_checkout_cart_index>
  2.     <update handle="checkout_cart_index"/>
  3. </mynamespace_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!’.