Magento Redirect to previous page after login
Default behaviour in Magento is to redirect to homepage after the user logs in.
There is also a configuration item in Customer Configuration that states: ‘Redirect Customer to Account Dashboard after Logging in’, and can be set to Yes/No.
Also there are some login redirect extensions freely available. One tested extension ‘MagePycho – Custom Login Redirect’ offers the possibility to set a custom URL to redirect the user after login, like ‘customer/address’, but not redirecting to the page the user wanted to access before logging in.
One tested solution
This solution (which btw. is the only one I’ve found working from the bunch of solutions over the Internet), requires a change in one of your template files, and a module for overwriting AccountController.php. Tested and working on Magento v1.7.0.2.
Implementation
In header.phtml of your template file: \app\design\frontend\default\yourtemplatefolder\template\page\html\header.phtml
/** * Set 'LoginBackUrl' variable in session when loading header.phtml (this file) for the wished page */ if(!Mage::helper('customer')->isLoggedIn()) { $loginback=$this->helper('core/url')->getCurrentUrl(); $check=strstr($loginback, 'customer/account/login'); //check if not loading header.phtml (this) for the redirected login page if(!strlen($check)) { Mage::getSingleton('core/session')->setLoginBackUrl($loginback); } }
Now, search if you have any modules overwriting the public function _loginPostRedirect() from AccountController.php. If you find one you’ll have to extend it in your new model that you have to make, otherwise you’ll extend Mage_Customer_AccountController.
The function which needs to be overwritten is the public function _loginPostRedirect().
References
<?php require_once 'Mage/Customer/controllers/AccountController.php'; //chage Mage with some overwriting module name if needed class Mindbench_Customer_AccountController extends Mage_Customer_AccountController //change if some other module overwrites Mage_Customer_AccountController { public function _loginPostRedirect() { $session = $this->_getSession(); 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()) { $backUrlcustom=Mage::getSingleton('core/session')->getLoginBackUrl(); if (!Mage::getStoreConfigFlag('customer/startup/redirect_dashboard')) { $referer = $this->getRequest()->getParam(Mage_Customer_Helper_Data::REFERER_QUERY_PARAM_NAME); if ($referer) { $referer = Mage::helper('core')->urlDecode($referer); if ($this->_isUrlInternal($referer)) { $session->setBeforeAuthUrl($referer); } } } else if ($session->getAfterAuthUrl()) { $session->setBeforeAuthUrl($session->getAfterAuthUrl(true)); } else if (strlen($backUrlcustom)) { Mage::getSingleton('core/session')->setLoginBackUrl(''); $session->setBeforeAuthUrl($backUrlcustom); } } else { $session->setBeforeAuthUrl(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($session->getBeforeAuthUrl(true)); } }
References
- http://pradeepkumarrcs.blogspot.ro/2013/01/redirect-back-to-page-after-logged-in.html