Magento Custom Redirection

The default Magento behavior after login is to redirect the user to his dashboard. A common requested customization is to change this behavior so that the logged in user will be redirected to a different page.
When user execute the logout action instead of viewing a boring page that says they logged out, I want them to be redirected to another attractive page of website. Create a more efficient user experience by sending users exactly where you need them to land when logging in, logging Out and signup.

Custom Redirection extension is a complete solution for custom url redirection for your ecommerce website built on magento platform. It allows admin to create redirection rules for different set of customer groups after login, logout, new account creation.

I assume that you already know the basic structure of Magento. Let’s try to implement the Custom redirection extension step by step.

Basic Structure

Step1: Create the Configuration file

<frontend>
        <events>
            <customer_login>
                <observers>
                    <redirection>
                        <class>redirection/observer_customer</class>
                        <method>customerLogin</method>
                    </redirection>
                </observers>
            </customer_login>
            <customer_register_success>
		        <observers>
		          <redirection>
		            <class>redirection/observer_customer</class>
                    <method>customerRegistration</method>
		          </redirection>
		        </observers>
		    </customer_register_success>
		    <controller_action_postdispatch_customer_account_logout>
		        <observers>
		          <redirection>
		            <class>redirection/observer_customer</class>
                    <method>customerLogout</method>
		          </redirection>
		        </observers>
		    </controller_action_postdispatch_customer_account_logout>
        </events>
    </frontend>

In this configuration file We will use customer_login, customer_register_success, controller_action_postdispatch_customer_account_logout event(hook). customer_login event will call after the post login, customer_register_success event will call after the sign up and controller_action_postdispatch_customer_account_logout will call after successful logout. I think no need to explain the other part of config file.

Step2: Create the System Configuration file

<config>
    <tabs>
        <iPragmatech translate="label" module="redirection">
            <label>iPragmatech Extension</label>
            <sort_order>300</sort_order>
        </iPragmatech>
    </tabs>
    <sections>
        <redirection translate="label">
            <label>Custom Redirection</label>
            <tab>iPragmatech</tab>
            <frontend_type>text</frontend_type>
            <sort_order>1001</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <settings translate="label">
                    <label>Custom Redirection Setting</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>100</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <enabled translate="label comment">
                            <label>Redirection Setting</label>
                            <frontend_type>select</frontend_type>
				     <source_model>
					adminhtml/system_config_source_enabledisable
				     </source_model>
                            <sort_order>1</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </enabled>
                        <group translate="label comment">
                            <label>Customer Group</label>
                            <frontend_type>select</frontend_type>
                            <source_model>
					adminhtml/system_config_source_customer_group
				    </source_model>
                            <sort_order>2</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <depends>
                                <enabled>1</enabled>
                            </depends>
                        </group>
                        <login_redirection>
                            <label>Login Redirection</label>
                            <frontend_type>select</frontend_type>
				<source_model>
					adminhtml/system_config_source_yesno
				</source_model>
                        <sort_order>3</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                        <depends>
                           <enabled>1</enabled>
                        </depends>
                        </login_redirection>
                        <path_redirect>
                            <label>Path Redirect</label>
                            <frontend_type>text</frontend_type>
                            <validate>validate-text</validate>
                            <sort_order>4</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <depends>
                                <login_redirection>1</login_redirection>
                                <enabled>1</enabled>
                            </depends>
                        </path_redirect>
                    </fields>
                </settings>
            </groups>
        </redirection>
	</sections>

System Configuration file will create the fields in the backend. tag will create the iPragmatech Tab in System Configuration of backend.
tag will create the redirection fields in backend. Here I have created the Custom Field for Login Redirection, likewise you can create field for Logout Redirection and Signup Redirection.

Step3: Create the adminhtml configuration file (Admin resource for Custom Redirection)

<acl>
   <resources>
       <all>
               <title>Custom Redirection</title>
        </all>
        <admin>
             <children>
                  <system>
                      <children>
                            <config>
                                <children>
                                    <redirection translate="title">
                                        <title>Custom Redirection</title>
                                    </redirection>
                                </children>
                            </config>
                        </children>
                    </system>
                </children>
            </admin>
        </resources>
  </acl>

Step4: Create the Customer.php in Model > Observer

class Ip_Redirection_Model_Observer_Customer extends Varien_Event_Observer
{
   /*method for Login Redirection*/
   public function customerLogin(Varien_Event_Observer $observer)
   {    
   	if (Mage::helper('redirection')->isEnabled()){	
   		if($this->_CustomerGroup()) {
			$_session = $this->_getSession();
			$_session->setBeforeAuthUrl(Mage::helper('redirection')				->setRedirectOnLogin());
		}
	}
   }
   /*method for SignUp Redirection*/
   public function customerRegistration(Varien_Event_Observer $observer)
   {
   	if (Mage::helper('redirection')->isEnabled() && Mage::helper('redirection')		->isoptionEnabled('registration_redirection')) {
   			$_session = $this->_getSession();
	   		$_session->setBeforeAuthUrl(Mage::helper('redirection')				->setRedirectOnSignup());
   	}
   }
   
   /*method for Logout Redirection*/
   public function customerLogout(Varien_Event_Observer $observer)
   {
   	if (Mage::helper('redirection')->isEnabled() && Mage::helper('redirection')	   ->isoptionEnabled('logout_redirection')) {
   		if($this->_CustomerGroup()) {
	   		$observer->getControllerAction()
	   		->setRedirectWithCookieCheck(Mage::helper('redirection')				->setRedirectOnLogout());
   		}
   	}
   }
   
   /*check the customer group*/
   protected function _CustomerGroup()
   {
   	$customer = $this->_getSession()->getCustomer();
   	$group_id = Mage::helper('redirection')->getConfigValue('group');
   	if($customer) {
   		if($customer->getGroupId() == $group_id) {
   			return TRUE;
   		}
   	}

   	/*redirect for all General/Retailer and Wholeseller*/
	if($group_id == ''){
		return true;	
	}	
   }
   
   protected function _getSession()
   {
   	return Mage::getSingleton('customer/session');
   }
}

Let’s deep explanation of each function writen in the customer.php file. Basically the definitions of Customer Login, Customer Logout and Customer signup are written here. Suppose when you execute the customer_login event, it automatically call the customerLogin method. In this method we find the customer session and using the customer session call the SetBeforeAuthUrl method. SetBeforeAuthUrl method Redirect back to page after logged-in in Magento.

Step5: Create the Helper File Data.php

class Ip_Redirection_Helper_Data extends Mage_Core_Helper_Abstract
{
	const XML_CONFIG_PATH = 'redirection/settings/';
	
	public function getConfigValue($key, $ipobject = '')
	{
		return Mage::getStoreConfig(self::XML_CONFIG_PATH . $key, $ipobject);
	}
	
	/*method for login redirection */
	public function setRedirectOnLogin(){
		$_path = (string) $this->_getConfigValue('path_redirect');
		return Mage::getUrl($_path);
	}
	
	/*method for Signup redirection */
	public function setRedirectOnSignup(){
		$_path = (string) $this->_getConfigValue('signup_path_redirect');
		return Mage::getUrl($_path);
	}
	
	/*method for Logout redirection */
	public function setRedirectOnLogout(){
		$_path = (string) $this->_getConfigValue('logout_path_redirect');
		return $_path;
	}
	
	public function isEnabled()
	{
		return (bool) $this->_getConfigValue('enabled');
	}
	
	public function isoptionEnabled($value)
	{
		return (bool) $this->_getConfigValue($value);
	}
	
	protected function _getConfigValue($key)
	{
		return Mage::getStoreConfig(self::XML_CONFIG_PATH . $key);
	}
}

Data helper file collect the data from the redirection configuration setting. The setRedirectOnLogin method collect the Customer Login setting. Likewise the setRedirectOnSignup & setRedirectOnLogout will collect the Sign up and Logout setting.

Custom Redirection extension is a complete solution for custom url redirection for your ecommerce website built on magento platform. It allows admin to create redirection rules for different set of customer groups after login, logout, new account creation.

Magento Custom Redirection was last modified: June 20th, 2022 by Pragmaapps