Magento 1: Redirecting to Previous Page After Successful Login
As a Magento developer, you’ve probably encountered the common issue of wanting to redirect users back to the page they were on before logging in. By default, when a user logs in with Magento 1, they are redirected to the homepage or dashboard.
To solve this problem and redirect customers to the previous page after successful login, we need to modify the core Mage_Core_Controller_Varien_Action controller’s _processLogin method. Let’s dive into the solution step by step.
Understanding the Problem
When a user tries to log in from any page (like a product detail or cart page), Magento stores this request in the session but doesn’t use it during redirection after login. This is why users are always sent back to the homepage instead of their previous location.
The Solution: Redirecting to Previous Page
To fix this issue, we need to override the core _processLogin method in our custom Mage_Core_Controller_Varien_Action class and implement a logic to redirect based on the user’s previous page stored in the session.
Firstly, you should create an extension with a custom controller action file. For this example, let’s assume we have created an extension called “LaravelCompany_RedirectFix” under app/code/local/LaravelCompany/RedirectFix/etc/config.xml:
<config>
<modules>
<LaravelCompany_RedirectFix>
<version>0.1.0</version>
</LaravelCompany_RedirectFix>
</modules>
<global>
<helpers>
<redirectfix>
<class>LaravelCompany_RedirectFix_Helper_Data</class>
</redirectfix>
</helpers>
<models>
<redirectfix>
<class>LaravelCompany_RedirectFix_Model</class>
</redirectfix>
</models>
</global>
<frontend>
<layout>
<updates>
<redirectfix>
<file>custom.phtml</file>
</redirectfix>
</updates>
</layout>
</frontend>
</config>
Next, create your custom controller under app/code/local/LaravelCompany/RedirectFix/controllers/RedirectController.php:
<?php
class LaravelCompany_RedirectFix_RedirectController extends Mage_Core_Controller_Varien_Action
{
public function loginPostAction()
{
$session = $this->_getSession();
if ($session->isLoggedIn()) {
return $this->_redirect('*/*/');
}
$loginUrl = $this->_getUrl('customer/account/login');
// Store previous page URL in session
if (isset($_SERVER['HTTP_REFERER'])) {
$referrer = $_SERVER['HTTP_REFERER'];
$session->setPreviousPage($referrer);
} elseif ($this->getRequest()->isPost()) {
$session->setPreviousPage($loginUrl);
}
// Redirect to login page
return $this->_redirect($loginUrl);
}
}
In the loginPostAction method, we first check if the user is already logged in. If so, we redirect them back to their previous location.
If not, we store the previous page’s URL (either from HTTP Referer or login URL) in the session and then redirect to the Magento default login page.
Conclusion
By implementing this custom controller action, you’ve successfully overridden the core _processLogin method to handle redirection properly after a successful customer login. This solution ensures that users are sent back to their previous location instead of always being redirected to the homepage or dashboard.
Remember to test this fix thoroughly under different scenarios, including various login page access points like product pages, customer account pages, and checkout process.
If you’re looking for more advanced Magento solutions and tips, feel free to explore Laravel Company’s blog for insightful articles on e-commerce development best practices.