While working in Magento you may sometimes run into situations where you would like to override the functionality of the Magento core controllers. But you cannot edit the core controllers of Magento as it will hinder you from upgrading your Magento version. So we have to find a simple solution for that. Don't worry, today we will see how to override Magento core controllers.
For this, you have to create a custom module. If you do not know how to create a custom module, then read this post.
Now suppose we want to override the checkout/cart controller. To do this, we will have to edit just two files in our custom module.
Suppose our custom module is present in the namespace 'Company' and our module is named as 'Web'.
Step 1:
Open the app\code\local\Company\Web\etc\config.xml and edit it as follows:
The name of the core module to be overridden is written in between the router tags. Here we want to override the checkout module so checkout is to be wrapped in <routers> tags.
After that we will tell Magento to call our custom module before the Mage/Checkout module
Step 2:
Now create the controller file to be overridden, CartController.php in our case. (app\code\local\Company\Web\controllers\CartController.php)
And you are done now. Override what you want:)
For this, you have to create a custom module. If you do not know how to create a custom module, then read this post.
Now suppose we want to override the checkout/cart controller. To do this, we will have to edit just two files in our custom module.
Suppose our custom module is present in the namespace 'Company' and our module is named as 'Web'.
Step 1:
Open the app\code\local\Company\Web\etc\config.xml and edit it as follows:
The name of the core module to be overridden is written in between the router tags. Here we want to override the checkout module so checkout is to be wrapped in <routers> tags.
After that we will tell Magento to call our custom module before the Mage/Checkout module
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Company_Web>
<version>0.0.1</version>
</Company_Web>
</modules>
<frontend>
<routers>
<checkout><!-- Name of core module to be overridden -->
<args>
<modules>
<Company_Web before="Mage_Checkout">Company_Web</Company_Web><!-- Tell Magento to call our custom module before the Mage/Checkout module -->
</modules>
</args>
</checkout>
</routers>
</frontend>
</config>
Step 2:
Now create the controller file to be overridden, CartController.php in our case. (app\code\local\Company\Web\controllers\CartController.php)
<?php
/**
* Magento
*
* Override Mage_Checkout_Cart controller
*/
/**
* IMPORTANT
* Include the core file to be overridden
*/
require_once("Mage/Checkout/controllers/CartController.php");
/**
* IMPORTANT
* Extend the core controller in our custom controller.
*/
class Company_Web_CartController extends Mage_Checkout_CartController
{
/**
* Get current active quote instance
*
* @return Mage_Sales_Model_Quote
*/
public function indexAction()
{
echo "This controller has been overridden.";
}
}
And you are done now. Override what you want:)


Thanks,
ReplyDeleteGreat article, exactly what i was looking for...
Yes thank you...It's not easy to find current information on this...
ReplyDeleteI decided not to use magento, because everything is over-engineered.
ReplyDeletebut problem is that,my controller(Adminhtml->permissions->controllers->usercontroller)
ReplyDeletedidnt gave output
and my controller location is(Intercom->Data->permissions->controllers->usercontroller)
but
it always goes to admin side controller
plz give me solution for these
thanx brother
ReplyDeleteThanks Chirag,
ReplyDeleteKeep reading...
Please give me some help for override admin Catelog Product Controller for mass Action ..How can I?
ReplyDeleteHi Merk,
ReplyDeleteTo override the Admin Catalog Product Controller mass action, you need to write following code:
<config>
...
<global>
<rewrite>
<yournamespace_yourmodule_catalog>
<from><![CDATA[#^/admin/catalog_product/#]]></from>
<to>/yourmodule/product/</to>
</yournamespace_yourmodule_catalog>
</rewrite>
</global>
</config>
After that create ProductController.php in app\code\local\<Yournamespace>\<Yourmodule>\controllers as follows:
require_once("Mage/Adminhtml/controllers/Catalog/ProductController.php");
class Yournamespace_Yourmodule_ProductController extends Mage_Adminhtml_Catalog_ProductController
{
//Code lies here...
}
In this file you can write the custom code for your mass action. Please not that this file is an exact replica of the original ProductController.php. You just need to edit this controller to your needs.
Hope it solves your problem :)
Thank you so much for this clear and detailed explanation – it worked like a charm.
ReplyDeleteIt works like a cake.
ReplyDeleteThanks a lot for sharing !!!
this works, but somehow forces magento to use a new session id and send a new cookie for it
ReplyDeleteStruggling to override the Sendfriend controller and would really appreciate some help! I have this as the config.xml
ReplyDelete0.0.1
TIC_Sendfriend
And my ProductController file starts with:
require_once("Mage/Sendfriend/controllers/ProductController.php");
class TIC_Sendfriend_ProductController extends Mage_Sendfriend_ProductController
But anything in there is just ignored... please help...cheers, Will
Hi Will,
ReplyDeleteI cannot see your xml file. Please post it again.
Thanks
Hi,
ReplyDeleteThanks for this. It is working well on my local machine but on our server /checkout/cart works but /checkout/cart/ only runs the Mage_Check_CartController.
Any ideas?
Hi arvind,
ReplyDeletei want to override admin catalog product controller, but above code is not working for me, plz help me
my config xml is:
0.1.0
/MyWareHouse/product/
admin
MyWareHouse_Adminhtml_Catalog
MyWareHouse_admin
0.1.0
ReplyDelete/MyWareHouse/product/
admin
MyWareHouse_Adminhtml_Catalog
MyWareHouse_admin
$this->helper('core/url')->getCurrentUrl();
ReplyDeleteThanks a lot, it was good one for me
ReplyDeletehi arvin.. can i use same custom module controller to save the other form in my customize backend module? thanks in advance
ReplyDeleteHi,
DeleteTo save the form in backend module, you need to create the separate controller in the controllers/Adminhtml/ directory.
Most other posts about this forget to mention you need to call require_once('controller/to/be/overridden') at the top of your new controller in order for everything to work. Thanks for being thorough.
ReplyDeleteThanks:)
DeleteThanks you so much for this saved me from going bald by pulling my hair out in frustration. :)
ReplyDeleteHave tried on Magento EE - does not work
ReplyDeleteThanks for your great work!. Thanks for clear explaination especially comments on config.xml file code.
ReplyDeleteThanks for your great work! especially comment on config.xml really great thing! got clear idea
ReplyDeleteThanks for your great work! especially comments on config.xml code. Got clear idea how to override magento controller
ReplyDelete