Thursday, December 6, 2012

How to override Admin controller Action in Magento


>> Hi! friends this example will help you to override admin controller action. I had to replicate admin product edit section as there were two types of products. First one was normal Magento products another was Used products. Client wanted to manage them separately. So, I override admin product controller action. How ?

  •  First You need to create a new module. Create a xml file within app/etc/modules/JR_Overridecontroller.xml
                        <?xml version="1.0"?>
                        <config>
                            <modules>
                                <JR_Overridecontroller>
                                    <active>true</active>
                                    <codePool>local</codePool>
                                </JR_Overridecontroller>
                            </modules>
                        </config>

  •  Now make the config.xml within app/code/local/JR/Overridecontroller/etc/config.xml
                         <?xml version="1.0"?>
                        <config>
                            <modules>
                               <JR_Overridecontroller>
                                 <version>1.0.0</version>
                               </JR_Overridecontroller>
                             </modules>
                        
                            <admin>
                                <routers>
                                    <adminhtml>
                                        <args>
                                            <modules>
                                                <JR_Overridecontroller before="Mage_Adminhtml">JR_Overridecontroller</JR_Overridecontroller>
                                            </modules>
                                        </args>
                                    </adminhtml>
                                </routers>
                            </admin>
                        </config>

     With this code you can override any admin controller action. But I'm just showing with product controller.

  •  So, now make the controller file that you need to modify within app/code/local/JR/Overridecontroller/controllers/Catalog/ProductController.php
                   <?php
                        include_once("Mage/Adminhtml/controllers/Catalog/ProductController.php");
                        class JR_Overridecontroller_Catalog_ProductController extends Mage_Adminhtml_Catalog_ProductController
                        {
                            public function editAction(){
                              
                                // Here is your overridden controller method.....
                              
                            }
                        }
                    ?>

Download the Zip from here.


                                                 ********       Good Luck       ************