3

I'd like to create a link which allows a customer to log in before returning to the page they started on, not to the My Account page.

The link needs to work from any location in the site.

Mage_Customer_AccountController has a _loginPostRedirect() which does the redirecting based on a series of session variables (defaulting to the My Account page). Is there a clean, safe way for me to use one of these session variables (from every page) to make my link work as I'd like? Or is my only option to implement a rewrite for this method and change the call to $session->setBeforeAuthUrl(Mage::helper('customer')->getAccountUrl())?

3 Answers 3

4

In the Admin Panel go to System > Configuration > Customer Configuration

In this page, under the Login Options change Redirect Customer to Account Dashboard after Logging in from Yes to No.

When this option is set to No, then the user goes to the login page, it's action will take the HTTP_REFERER of the page and save it in the session so the _loginPostRedirect function redirects to it.

6

You can create an observer for this event controller_action_postdispatch_customer_account_loginPost
with a method similar to this:

public function redirectCustomer($observer){
    $url = 'YOUR URL HERE';
    // $url can be : Mage::getUrl('some/path'); or Mage::getUrl('', array('_direct'=>'some-cms-page.html'));
    Mage::app()->getResponse()->setRedirect($url);
    return $this;
}
1
  • Can I put this code into the login.phtml file? Will that work?
    – yldziner
    Oct 28, 2013 at 15:12
0

Even if you disable Redirect Customer to Account Dashboard after Logging, in the Admin Panel in System > Configuration > Customer Configuration, in many cases referer is not defined / kept when coming from outside the store to a login protected page.

Only the login link on the page passes referer properly base64 encoded by Magento (check the login URL). Also, HTTP_REFERER can't be used as it gets lost on the login redirect (anyway it would need further processing to make it encoded). In _loginPostRedirect() function from Magento core AccountController.php you can find how it works:

$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));

You can see that referer should be passed as a parameter and then it gets decoded by urlDecode().

One solution would be to modify the template file header.phtml and retain the original URL in a session variable, then overwrite the _loginPostRedirect() function to setBeforeAuthUrl() to it. You can find a working solution which I've tested here: http://www.mihaimatei.com/redirect-previous-page-login-magento/

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.