Override Controllers in Magento

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 appcodelocalCompanyWebetcconfig.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. (appcodelocalCompanyWebcontrollersCartController.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:)

Written by Arvind Bhardwaj

Arvind is a Magento and WordPress expert with more than 6 years of industry wide experience.

Website: http://www.webspeaks.in/

36 thoughts on “Override Controllers in Magento

  1. 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

  2. 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 appcodelocal<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 🙂

  3. 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

  4. 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?

  5. 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

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

  7. 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.

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

  9. 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

    1. 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>

  10. 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

  11. 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

  12. 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

Comments are closed.