11/22/2010

How to overwrite cart controller add Action index Action in Magento

The Following Tutorial will Shows How to override cart controller Add Action or Index Action

First you need to give your Module name and Name_space ,I have taken Anjan as Namespace and override as Modulename
Now Create your module folders and files like below Instruction

1. Magento/app/code/local/Anjan/Override/etc/config.xml
2. Magento/app/code/local/Anjan/Override/controllers/CartController.php
3. Magento/app/etc/modules/Anjan_Override.xml

In config.xml Write the Foloowing code

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Anjan_Override> <!--Change Anjan To Your Module NameSpace and Override to Your Module name-->
<version>0.1.0</version>
</Anjan_Override>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<Anjan_Override before="Mage_Checkout">Anjan_Override</Anjan_Override><!--Change Anjan To Your Module NameSpace and Override to Your Module name-->
</modules>
</args>
</checkout>
</routers>
</frontend>
</config>

In CartController.php write the following code

<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class Anjan_Override_CartController extends Mage_Checkout_CartController
{
public function addAction()
{
echo 'I successfully Override Cart Controller';
parent::addAction();
}
public function indexAction()
{
echo 'I successfully Override Cart Controller';
parent::addAction();
}
}
?>

In Anjan_Override.xml write the following code

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Anjan_Override>
<active>true</active>
<codePool>local</codePool>
</Anjan_Override>
</modules>
</config>

Now upload these files into your server,refresh your cache , the click on Add to cart button you can see the message is displaying . You can use die() statement to make sure whether is overrides or not .

22 comments:

  1. Anonymous3/25/2011

    Hi,

    I had created all these files with the same example that you have given like "Anjan_Override".. but this is not working... any suggestion..?

    ReplyDelete
  2. Please Clear your cache , As I have worked on it several times and it works always, Please check may be you have missed out something

    ReplyDelete
  3. Anonymous3/25/2011

    Got it.. after clearing cache it works well..
    Thanks for your script...

    Balan

    ReplyDelete
  4. Anonymous3/29/2011

    xml code is truncated above.. pls suggest me..

    ReplyDelete
  5. Use `Compare` instead of `Checkout` may it work

    ReplyDelete
  6. I tried to do this with the Mage_Adminhtml_Catalog Product Controller, but it's not picking my module up. Here is my config.xml




    Sttl_Upermission_Catalog

    ReplyDelete
  7. it helps me a lot. A big thank you for sharing.

    ReplyDelete
  8. That was helpful. Thank you very much!

    ReplyDelete
  9. Didn't work for me. Double checked everything :(

    ReplyDelete
    Replies
    1. Did you cleared your cache? because it worked for everyone. May be you missed out something.

      Delete
  10. It will definately dont work,unless you put a file in mymodule.xml in app/design/mymodule/mytheme/layout/

    ReplyDelete
  11. It's helpful for me. Thank u so much

    ReplyDelete
  12. Thank You! It helped me a lot...

    I was getting infinite redirect loop error at first. But on commenting "parent::addAction();" in CartController.php its ok.

    ReplyDelete
  13. I had created all these files with the same example that you have given like "Anjan_Override".. but this is not working... any suggestion..?

    and Clear your cache again & again
    Please help me

    ReplyDelete
    Replies
    1. Have yo create xml file in app/etc/modules/Anjan_override.xml , I have not mentioned in this article

      Delete
    2. yes i have create app/etc/modules/Anjan_override.xml
      and local/Anjan/Override/controllers/CartController.php
      local/Anjan/Override/etc/config.xml

      Delete
  14. yes i have create a etc/modules/Anjan_override.xml

    and controller
    local/Anjan/Override/controllers/CartController.php
    config.xml
    local/Anjan/Override/etc/config.xml
    I have remove cache and i have implement after 6 month's it's working but today I am totally confused what are the issue


    controller


    config xml




    0.1.0







    Anjan_Override







    etc/module/




    true
    local



    Please help me

    ReplyDelete
    Replies
    1. The problem is probably here:
      CLASS

      After where you define the class make sure you do not have an enter before

      They must be touching no spaces

      Delete
  15. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Rename errors/local.xml.sample to local.xml and check what is the error

      Delete