Override Controllers in Magento

36 comments
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
<?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:)

36 comments

Thanks,
Great article, exactly what i was looking for...

Yes thank you...It's not easy to find current information on this...

I decided not to use magento, because everything is over-engineered.

but problem is that,my controller(Adminhtml->permissions->controllers->usercontroller)
didnt 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

Please give me some help for override admin Catelog Product Controller for mass Action ..How can I?

Hi Merk,
To 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.

It works like a cake.

Thanks a lot for sharing !!!

this works, but somehow forces magento to use a new session id and send a new cookie for it

Struggling to override the Sendfriend controller and would really appreciate some help! I have this as the config.xml




0.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,
I cannot see your xml file. Please post it again.

Thanks

Hi,

Thanks 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,
i 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










/MyWareHouse/product/








admin


MyWareHouse_Adminhtml_Catalog


MyWareHouse_admin

$this->helper('core/url')->getCurrentUrl();

Thanks a lot, it was good one for me

hi arvin.. can i use same custom module controller to save the other form in my customize backend module? thanks in advance

Hi,
To 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.

Thanks you so much for this saved me from going bald by pulling my hair out in frustration. :)

Have tried on Magento EE - does not work

Thanks for your great work!. Thanks for clear explaination especially comments on config.xml file code.

Thanks for your great work! especially comment on config.xml really great thing! got clear idea

Thanks for your great work! especially comments on config.xml code. Got clear idea how to override magento controller

Thanks Dear,
For awesome help comment in config.xml.

Thanks,
Sunil Kansodiya

I want to override Mage_Adminhtml_Sales_OrderController . I tried this way. But still ti loads the core controller insted of overriden one. Please help me

The controller file should look like:
include_once("Mage/Adminhtml/controllers/Sales/OrderController.php");
class My_Module_Sales_OrderController extends Mage_Adminhtml_Sales_Product_OrderController
{
...

And the config.xml should contain this:

<config>
<admin>
<routers>
<adminhtml>
<args>
<modules>
<my_Module before="Mage_Adminhtml">My_Module</my_Module>
</modules>
</args>
</adminhtml>
</routers>
</admin>
</config>

Hi,

I followed the above technique to override the Mage_Adminhtml_Catalog_ProductController. But on refreshing a funny thing has happened, the product table has disappeared from the screen!

Catalog>Manage Products screen is now empty. Can you please help me with this ?
I just want to extend the Mage_Adminhtml_Catalog_ProductController to add one more function to it.

Thanks a lot in advance.
Kazi

Hi,
I have followed the above steps to override the Mage_Adminhtml_Catalog_ProductController. But on refreshing, a funny thing has happened. The table of products from Catalog>Manage Products has disappeared!

Can you please help me with this? I want to extend the Mage_Adminhtml_Catalog_ProductController to just add one more function to it.

Thanks a lot in advance,
kazi

Hi, Why there are two ways to override a controller ? What is the best way ?

I dont know the second way. I think there is only one way which I have specified here...

Thank you so much..it saved my lots of time.

Hi,
I followed your article to ovveride AccountController but it doesnt give me any result. I mean the default Customer register.phtml which is available in persistent folder is call whenever i go to Customer Account create option. What can i do for this ?

Thanks

We would love to hear from you...

back to top