Showing posts with label Override controller in magento. Show all posts
Showing posts with label Override controller in magento. Show all posts

Friday, 30 October 2015

How to override createPost Action in Account Controller in Magento

Here we are going to override createpost function in magento

Lets create a small module for overriding the account controller

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

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


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

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

<?xml version="1.0"?>
<config>
    <modules>
        <YourPackageName_Customer>
            <version>0.0.1</version>
        </YourPackageName_Customer>
    </modules>
    <frontend>
        <routers>
            <customer>
                <args>
                    <modules>
                        <yourpackagename_customer before="Mage_Customer">YourPackageName_Customer</yourpackagename_customer>
                    </modules>
                </args>
            </customer>
        </routers>
    </frontend>
</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 customer module with its arguments. In the bottom node goes the current module frontend name (<yourpackagename_customer in my example) with the "before" or "after" attribute name with the value of which module is being overriden (Mage_Customer) and our own module name inside the tags (YourPackageName_Customer).

Step 3:- Create AccountController.php controller file at app/code/local/YourPackageName/Customer/controllers/AccountController.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/Customer/controllers/AccountController.php a file with the same name will have to be created (app/code/local/YourPackageName/Customer/controllers/AccountController.php).

Class definition inside our new file:

<?php
    include_once("Mage/Customer/controllers/AccountController.php");
    class YourPackageName_Customer_AccountController extends Mage_Customer_AccountController
    {
        public function createPostAction()
        {
            // echo "done"; exit;
        }
    }
?>

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;
        }
    }
?>