Friday 30 October 2015

How to override create new Product Controller in Magento Admin Panel - Magento

Here we are going to override create new product function in magento during admin product creation

Lets create a small module for overriding the product controller

Step 1:- Create YourPackageName_Adminhtml.xml file at app/etc/

<?xml version="1.0"?>
<config>
  <modules>
    <YourPackageName_Adminhtml>
      <active>true</active>
      <codePool>local</codePool>
    <YourPackageName_Adminhtml>
  </modules>
</config>

Step 2:- Create module's config.xml file at app/code/local/YourPackageName/Adminhtml/etc/

Let's take a look at the code that goes into the config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <YourPackageName_Adminhtml>
            <version>0.0.1</version>
        </YourPackageName_Adminhtml>
    </modules>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <yourpackagename_adminhtml before="Mage_Adminhtml">YourPackageName_Adminhtml</yourpackagename_adminhtml>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

In the config node its child has to define whether we are changing the frontend or admin file and we define router node that will override core adminhtml module with its arguments. In the bottom node goes the current module frontend name (<yourpackagename_adminhtml in my example) with the "before" or "after" attribute name with the value of which module is being overriden (Mage_Adminhtml) and our own module name inside the tags (YourPackageName_Adminhtml).

Step 3:- Create ProductController.php controller file at app/code/local/YourPackageName/Adminhtml/controllers/Catalog/ProductController.php

If you noticed, there are no strict files defined. With this we have defined only the path that will look for the controller files by their names. So if we would like to override app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php a file with the same name will have to be created (app/code/local/YourPackageName/Adminhtml/controllers/Catalog/ProductController.php).

Class definition inside our new file:

<?php
    include_once("Mage/Adminhtml/controllers/Catalog/ProductController.php");
    class YourPackageName_Adminhtml_Catalog_ProductController extends Mage_Adminhtml_Catalog_ProductController
    {
        public function newAction()
        {
            // echo "done"; exit;
        }
    }
?>

1 comment:

  1. Hello,
    thanks for the blog. I would like to override an already overriding class. The first override is this:
    OtherCompany_Modulename_Admin

    How can I override the controllers in controller/Admin ? I tried this:
    MyCompany_Mymodulename
    Is the router name I need to overwrite now ?
    Claudia

    ReplyDelete