10

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.

0

5 Answers 5

12

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('');
    }
2
  • Where is this ? Commented Oct 21, 2015 at 15:45
  • Should I place it in CSS ? Which file u mean? Commented Oct 21, 2015 at 15:58
8

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">
4

try this

<?php Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getBaseUrl()); ?>
1
  • Where to write this ? Commented Oct 21, 2015 at 15:57
4

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('');
}
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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