Redirecting After Login Based on Referrer Page in Magento
If you’re looking to redirect your customers after login based on the page they logged in from, you’ve come to the right place. In this guide, we’ll walk through how to achieve this using a bit of custom code and some built-in Magento functionality.
Understanding the Default Login Redirect Process
By default, when a customer logs in, Magento redirects them to the “customer dashboard” or the “account index” page. This is controlled by the Mage_Customer_AccountController::loginPostAction method in your Magento installation’s <magento_root>/app/code/core/Mage/Customer/controllers/AccountController.php file.
The redirect logic looks something like this:
...
$session->setBeforeAuthUrl($this->_getRefererUrl());
$this->_redirect('customer/account/index');
...
Here, the beforeAuthUrl session is set to the referral URL (the page where the customer was before clicking the login link). Then, the customer is redirected to the account index page.
Redirecting Based on Referrer
To redirect based on the referrer page, we need to modify this behavior. Here’s a step-by-step approach:
1. Determine Your Login Pages
First, you must identify the URLs of the two login pages from which customers can log in. Let’s assume these are:
- Page A:
https://yourstore.com/login-page-a - Page B:
https://yourstore.com/login-page-b
2. Create a Custom Redirect Logic
We’ll create a new custom module to override the default redirection logic. Here’s what you need:
Create files for your custom module:
/app/code/local/YourCompany/LoginRedirect/etc/config.xml
/app/code/local/YourCompany/LoginRedirect/controllers/CustomAccountController.php
In config.xml, define your module:
<?xml version="1.0"?>
<config>
<modules>
<YourCompany_LoginRedirect>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Customer />
</depends>
</YourCompany_LoginRedirect>
</modules>
</config>
In CustomAccountController.php, override the login action:
<?php
class YourCompany_LoginRedirect_CustomAccountController extends Mage_Core_Controller_Front_Action {
public function loginPostAction() {
$session = $this->_getSession();
if ($session->isLoggedIn()) {
return $this->redirect($session->getBeforeAuthUrl());
}
$referrerUrl = $this->_getRefererUrl();
$loginPages = [
'https://yourstore.com/login-page-a',
'https://yourstore.com/login-page-b'
];
if (in_array($referrerUrl, $loginPages)) {
$redirectUrl = $this->getRedirectUrlForPage($referrerUrl);
$session->setBeforeAuthUrl($redirectUrl);
}
$session->setBeforeAuthUrl($this->_getRefererUrl());
$this->_redirect('customer/account/index');
}
private function getRedirectUrlForPage($page) {
if ($page == 'https://yourstore.com/login-page-a') {
return '/specific-redirect-url-a';
} elseif ($page == 'https://yourstore.com/login-page-b') {
return '/specific-redirect-url-b';
}
}
}
3. Compile Your Code
After creating these files, make sure to compile your Magento code and clear the cache:
php magento.php --compile
rm -rf var/cache/*
rm -rf var/session/*
Conclusion
With this custom module and redirection logic in place, you should now be able to redirect customers after login based on the page they logged in from. This is particularly useful for scenarios where you have multiple entry points into your customer account system.
Remember, it’s always recommended to test changes like these thoroughly before deploying them to a live environment.
For more tips and best practices on Magento development, check out our Magento Development Guide at Laravel Company.