Your shopping cart is empty. Browse our Store

Magento

Open Source eCommerce Evolved

How to overload a controller

Last modified by nicolas46 on Mon, June 9, 2008 10:42
Source|Old Revisions  

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 “(?)”):

  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.        
  12.         $cart = $this->_getCart();
  13.         $cart->init();
  14.         $cart->save();
  15.  
  16.         $this->loadLayout();
  17.         $this->_initLayoutMessages('checkout/session');
  18.  
  19.         if ($continueShoppingUrl = Mage::getSingleton('checkout/session')->getContinueShoppingUrl(true)) {
  20.         }
  21. //        elseif ($continueShoppingUrl = Mage::helper('catalog')->getLastViewedUrl()) {
  22. //        }
  23.         else {
  24.             $continueShoppingUrl = Mage::getUrl();
  25.         }
  26.  
  27.         if ($continueShoppingUrl) {
  28.             $this->getLayout()->getBlock('checkout.cart')->setContinueShoppingUrl($continueShoppingUrl);
  29.         }
  30.         $this->renderLayout();
  31.     }
  32. }

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




 

Popular Wiki Tags  |  View all

Professional Services from the Magento Team

Professional Installation from the Magento Team

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs
Sales: Call 877.832.5289 (North America) 310.295.4144 (International) to request a call-back.
© Copyright 2008 Varien. Magento is a trademark of Irubin Consulting Inc. DBA Varien    Privacy Policy|Terms of Service