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.

Wednesday, November 05, 2014

Manual Login and Logout in WordPress

Very recently, I had a situation where I needed to take user's username and password as inputs and log him/her onto Wordpress manually. And this is how I came up with the solution. Check the code below.

<?php
// This is a very important step
// We need to get the wp object
require("wp-load.php");   

// Log Out if anyone is already logged
// in from same browser
if ( is_user_logged_in() ) 

   // Default Wordpress function 
   // to log a user out
   wp_logout(); 
}

// GET user's Inputs (username, password)
$creds = array();
$creds['user_login']    = $_POST['username'];
$creds['user_password'] = $_POST['password'];

// Extra settings, for Remember me
$creds['remember'] = false;

// The Wordpress function which Logs
// the user in
$user = wp_signon( $creds, false );

// Track ERROR if any
if ( is_wp_error($user) )
{
  echo $user->get_error_message(); 
}
// Login Success
else
{
  echo "You have logged in successfully";
}
?>

The code above is quite self-explanatory. Some points to be noted : 

1. wp_signon() -- It logs any user in with username and passwords passed to it in Array. This function takes a 2nd parameter to mention whether secured cookies are to be used.
2. wp_logout() -- It logs out if any user is logged in
3. The array $cred must be built with the keys ('user_login' and 'user_password') shown above.