Monday, November 10, 2014

How to override Customer Account controller in Magento 1.8

I once had a requirement to change/modify or add new functionalities to the "Customer Account Controller" which is responsible for new registration, customer log in, log out, redirect to Customer dashboard, Forgot Password procedures etc. In such cases, we better not modify the core files directly. If we change the core files, all our work would get lost if Magento is upgraded to other versions. So, it is better if extend the core controllers and add our custom codes in it. This way our custom code would be there even if Magento is upgraded.

To achieve this, we would create a new module. The following points need to be remembered. 

To know how a basic 'Hello World' module is created in Magento, check this article "Building a basic Hello World module in Magento 1.8".

1. The controller which we want to override is: app/code/core/Customer/controllers/AccountController.php

2. Our new module name is "Newcustom_Customer" where "Newcustom" is the namespace name and "Customer" is our module name.

3. Next we create an xml file called "Newcustom_Customer.xml" inside app/etc/modules/Newcustom_Customer.xml 

4. So, we create a folder structure like this :: app/code/local/Newcustom/Customer and inside it, we create 2 more folders namely "etc" and "controllers".

5. Inside the app/code/local/Newcustom/Customer/etc folder, we create the configuration file for the module called config.xml

6. Inside the app/code/local/Newcustom/Customer/controllers folder, we copy paste the original app/code/core/Customer/controllers/AccountController.php

So, we need to work on mainly 3 files :: 

a. app/etc/modules/Newcustom_Customer.xml
b. app/code/local/Newcustom/Customer/etc/config.xml
c. app/code/local/Newcustom/Customer/controllers/AccountController.php

1. Let's start with the first file Newcustom_Customer.xml. It's content should be like this ::

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

The above piece of XML tells Magento that this module would run from "app/code/local" codepool. 

2. Next check out the config.xml file for our module. 

<?xml version="1.0"?>
<config>
  <modules>
    <Newcustom_Customer>
      <version>0.1.0</version>
    </Newcustom_Customer>
  </modules>

  <frontend>
   <routers>
    <customer>
<use>standard</use>
     <args>
       <modules>
         <Newcustom_Customer before="Mage_Customer">Newcustom_Customer</Newcustom_Customer>
       </modules>
     </args>
    </customer>
   </routers>
  </frontend>
</config>

This XML would help to override the core Mage_Customer's controller.

3. And finally, the controller file itself (AccountController.php) needs to be modified. But it should extend the Mage_Customer's AccountController.php file's Mage_Customer_AccountController class.

<?php
// Include the Original Mage_Customer's AccountController.php file
// where the class 'Mage_Customer_AccountController' is defined
require_once Mage::getModuleDir('controllers', 'Mage_Customer').DS.'AccountController.php'; 

// Now extend the class 'Mage_Customer_AccountController'

class Newcustom_Customer_AccountController extends Mage_Customer_AccountController  
{

  // We can add more functionality

  public function test()
  {   

  }
  
  // Re-Write the Old Methods
  public function loginPostAction()
  {
        
  }      

}

?>

Now, we are done. As we have extended the original 'Mage_Customer_AccountController' class, all the original functions are automatically available to this new over-ridden controller. And we can still add some more functionality by adding our custom functions to our class.  Some points need to be noted here :

1. Check how our extended class is defined with a name "Newcustom_Customer_AccountController".
2. We are extending the original Mage_Customer's controller class 'Mage_Customer_AccountController' to get the original functionality.

Hope this helps.

1 comment:

Chandan Patra said...

Hi Bruce,

Thanks for your comments. Wordpress and Magento have their own niche users and purposes. For small content-based websites, Wordpress is very nice and sufficient. For small E-Commerce set-ups, we can highly use Wordpress and Woo-Commerce to build up a decent webstore. For scalability, we need to use Magento. Magento has a robust framework to assure protection from hacking.

I use both at my WorkPlace.