Webspeaks

Thursday, March 31, 2011

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 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:)

27 comments:

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

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

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

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

    ReplyDelete
  5. Thanks Chirag,
    Keep reading...

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

    ReplyDelete
  7. 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 :)

    ReplyDelete
  8. Thank you so much for this clear and detailed explanation – it worked like a charm.

    ReplyDelete
  9. It works like a cake.

    Thanks a lot for sharing !!!

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

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

    ReplyDelete
  12. Hi Will,
    I cannot see your xml file. Please post it again.

    Thanks

    ReplyDelete
  13. 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?

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

    ReplyDelete
  15. 0.1.0










    /MyWareHouse/product/








    admin


    MyWareHouse_Adminhtml_Catalog


    MyWareHouse_admin

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

    ReplyDelete
  17. Thanks a lot, it was good one for me

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

    ReplyDelete
    Replies
    1. Hi,
      To save the form in backend module, you need to create the separate controller in the controllers/Adminhtml/ directory.

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

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

    ReplyDelete
  21. Have tried on Magento EE - does not work

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

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

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

    ReplyDelete

We would love to hear from you...