I wanted to redirect user to "Home" page instead of "My Account" page after login, and i was successful to achieve this making use of

<customer_login>

event.

But I was also redirected to home page while i login from onepage checkout which i don't want.

Below is my observer function

public function setRedirectOnLogin(){
    $session = Mage::getSingleton('customer/session');
    if (strpos($session->getBeforeAuthUrl(), 'checkout') === false)
        $session->setAfterAuthUrl(Mage::getBaseUrl());
    $session->setBeforeAuthUrl('');

}

Can any one please guide how can i stop user from redirecting it to homepage after login from onepage checkout?

Thank You.

up vote 9 down vote accepted

Thank you Guys.

below is the observer function that worked for me

 public function setRedirectOnLogin(){
        $session = Mage::getSingleton('customer/session');
        if (strpos(Mage::helper('core/http')->getHttpReferer(), 'checkout') === false)
            $session->setAfterAuthUrl(Mage::getBaseUrl());  
        else             
            $session->setAfterAuthUrl(Mage::helper('core/http')->getHttpReferer());

        $session->setBeforeAuthUrl('');
    }
  • Where is this ? – Pratik C Joshi Oct 21 '15 at 15:45
  • Should I place it in CSS ? Which file u mean? – Pratik C Joshi Oct 21 '15 at 15:58

You should first change your customer configuration:

enter image description here

Then use a referrer param on the form. For instance

<form action="<?php
    echo $this->getUrl(
        'customer/account/loginPost',
        array(
            'referer' =>
            // Encode the base url, or whatever URL we want to go to
            Mage::helper('core')->urlEncode(Mage::getBaseUrl())
        )
    ) ?>" method="post">

try this

<?php Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getBaseUrl()); ?>

You can use Mage::helper('core/http')->getHttpReferer() in the condition you already have to see if the referer is your checkout page and act consequently.

Just to summarise all comments here:

Take a config.xml and register an event for customer_login in the frontend area like this

<frontend>
    <events>
        <customer_login>
            <observers>
                <your_module>
                    <class>your_module/observer</class>
                    <method>customerLogin</method>
                </your_module>
            </observers>
        </customer_login>
    </events>
</frontend>

After that you have to implement that observer method to change the "after auth" url of the customer session. This variable will be used in the AccountController of the customer to check where to redirect after login.

public function customerLogin($observer)
{
    $session = Mage::getSingleton('customer/session');
    if (strpos(Mage::helper('core/http')->getHttpReferer(), 'checkout') === false) {
        $session->setAfterAuthUrl(Mage::getBaseUrl());
    } else {
        $session->setAfterAuthUrl(Mage::helper('core/http')->getHttpReferer());
    }

    $session->setBeforeAuthUrl('');
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.