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

No comments:

Post a Comment