paolo@bimodesign.com | +34 608 61 64 10

E-Commerce

        

Magento, how redirect customer and merchant after login

When customers or a merchant sign in magento they will redirect to account's dashbord page by default. If we want to change it, for example redirecting the customer to the home page and the merchant to own panel (In a futher post, I'll explain how to customize the account's dashbord page for the merchant), we have to modify the code. Those are the steps to goal it, using the Intell module.

1. Create config.xml
Modify the app/code/local/Intell/Customer/etc/config.xml with this contents

<config>
    <modules>
        <Intell_Customer>
            <version>1.1.0</version>
        </Intell_Customer>
    </modules>
    <frontend>
        <routers>
            <customer>
                <args>
                    <modules>
                        <intell_customer before="Mage_Customer">Intell_Customer</intell_customer>
                    </modules>
                </args>
            </customer>
        </routers>
    </frontend>    
    <global>  
        <helpers>
            <intell>
                <class>Intell_Customer_Helper</class>
            </intell>
        </helpers>
    </global>
</config>


2. Modify the _loginPostRedirect method
We modify the method code (it's into the file local/Intell/Customer/controllers/AccountController.php) in this way:

protected function _loginPostRedirect()
    {
        $session = $this->_getSession();
        
        $preUrl = $session->getBeforeAuthUrl(true);
        $cusromerRedirectStatus = 1;
        $isMerchant = Mage::getSingleton('customer/session')->getCustomer()->getIsMerchant();
        
        if (!$isMerchant){
            Mage::log('Soy Usuario', null, 'paolo.log', true);
            $customerRedirectUrl = Mage::getUrl() ." "; 
        }else{
            Mage::log('Soy Merchant', null, 'paolo.log', true);
            $customerRedirectUrl = Mage::getUrl() ."customer/account/"; 
        }
        
        $redirectUrl = $preUrl;

        if (!$session->getBeforeAuthUrl() || $session->getBeforeAuthUrl() == Mage::getBaseUrl()) {
            // Set default URL to redirect customer to
            $session->setBeforeAuthUrl(Mage::helper('customer')->getAccountUrl());
            // Redirect customer to the last page visited after logging in
             if ($session->isLoggedIn()) {
                if (!Mage::getStoreConfigFlag(
                    Mage_Customer_Helper_Data::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD
                )) {
                    $referer = $this->getRequest()->getParam(Mage_Customer_Helper_Data::REFERER_QUERY_PARAM_NAME);
                    if ($referer) {
                        // Rebuild referer URL to handle the case when SID was changed
                        $referer = Mage::getModel('core/url')
                            ->getRebuiltUrl(Mage::helper('core')->urlDecode($referer));
                        if ($this->_isUrlInternal($referer)) {
                            $session->setBeforeAuthUrl($referer);
                        }
                    }
                } else if ($session->getAfterAuthUrl()) {
                    $session->setBeforeAuthUrl($session->getAfterAuthUrl(true));
                }
            } else {
                  $customerRedirectUrl=Mage::helper('customer')->getLoginUrl();
            }
        } else if ($session->getBeforeAuthUrl() == Mage::helper('customer')->getLogoutUrl()) {
            $session->setBeforeAuthUrl(Mage::helper('customer')->getDashboardUrl());
        } else {
            if (!$session->getAfterAuthUrl()) {
                $session->setAfterAuthUrl($session->getBeforeAuthUrl());
            }
            if ($session->isLoggedIn()) {
                $session->setBeforeAuthUrl($session->getAfterAuthUrl(true));
            }
        }
        $this->_redirectUrl($customerRedirectUrl);                    
    }

where
- I check if the user is Merchant (isMerchant)
- I need to assign " " to the customer, cause if I put this "/" the path will be "http://www.ecommerce.com//"
- I get the $session->getBeforeAuthUrl(true);, because in case of the customer login in the "one step checkout" page, I need that he remain in this page, and he doesn't redirect to the home.

3. Create the Helper
I add this code into the new file app/local/Intell/Customer/Helper/Data.php

class Intell_Customer_Helper_Data extends Mage_Core_Helper_Abstract
{
}


4. Add the module to the Magento's module directory

<config>
    <modules>
        <Intell_Customer>
            <active>true</active>
            <codePool>local</codePool>
        </Intell_Customer>
    </modules>
</config>

Don't forget to set it "true" ;)