Saturday, March 11, 2017

Extending / Overriding Magento controller

We can override any controller using our custom extension, so you will need to create below extension files and folders:-

For ex. here we are overriding checkout cart controller.


  • app/etc/modules/Namespace_YourModuleName.xml: It's a file used to enable our custom module.
  • app/code/local/Namespace/YourModuleName/etc/config.xml: It's a module configuration file in which we'll set up controller class overriding using certain tags as per the Magento conventions.
  • app/code/local/Namespace/YourModuleName/controllers/CartController.php: It's a custom controller class file which we'll use to override the core "Cart" controller class.
In your app/etc/modules/Namespace_YourModuleName.xml file, write below code:

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

In your app/code/local/Namespace/YourModuleName/etc/config.xml file, write below code:

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Namespace_YourModuleName>
<version>0.1.0</version>
</Namespace_YourModuleName>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<Namespace_YourModuleName before="Mage_Checkout">Namespace_YourModuleName</Namespace_YourModuleName>
</modules>
</args>
</checkout>
</routers>
</frontend>
</config>


In your app/code/local/Namespace/YourModuleName/controllers/CartController.php file, write below code:

<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class Namespace_YourModuleName_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();
}
}
?> 



No comments:

Post a Comment

Previous and next navigation buttons in magento2

Download official Magento marketplace extension Previous and next navigation buttons for magento2 that is successfully passed in extension ...