Redirecting Customer to the Current Product Page After Login in Magento 2
In Magento 2, redirecting a customer back to the page they were on before logging in can be quite challenging, especially when dealing with custom login forms like the one you have in your header. The standard Magento behavior is to redirect logged-in customers to the Customer Dashboard.
Understanding the Current Setup
Let’s first understand what you’ve set up:
- You have a custom login form in the header.
- A customer is currently on a product page, for example: https://testwebsite.com/catalog/product/view/id/10
- The customer logs in via your custom form.
However, when the customer submits the login form, they are redirected to the Customer Dashboard instead of staying on the same product page.
Incorrect Usage of Plugin Code
You attempted to use a plugin from our Magento 2 Redirect Customer to Custom Page After Login tutorial (https://laravelcompany.com/magento-redirect-customer-to-custom-page-after-login/) but faced issues. The issue lies in incorrectly applying the plugin code:
public function afterExecute(
\Magento\Customer\Controller\Account\LoginPost $subject,
$result)
{
$result->setPath('/'); // Change this to what you want
return $result;
}
This is generic redirect code that sets the path to the home page. It doesn’t account for the current URL or the specific product page.
Redirecting to the Current Product Page
To achieve your desired behavior, you need to modify both your login form and your plugin:
Step 1: Modify the Login Form
In your login form code, instead of using $this->getUrl('customer/account/loginPost'), which is a fixed URL, you should use the current URL where the customer is logged in. You can achieve this by adding a hidden input field for referer_url and using that value in your plugin.
Add the following line to your login form:
<input type="hidden" name="referer_url" value="<?= $currenturl; ?>">
Step 2: Modify the Plugin
In your LoginPostPlugin, update the afterExecute method as follows:
public function afterExecute(
\Magento\Customer\Controller\Account\LoginPost $subject,
$result)
{
$request = $subject->getRequest();
$refererUrl = $request->getParam('referer_url');
if ($refererUrl) {
return $this->_redirect($refererUrl);
}
return $result;
}
Here, we retrieve the referer_url parameter from the request and check if it exists. If it does, we redirect the customer to that URL using the _redirect method.
Conclusion
Redirecting a customer back to their current page after login in Magento 2 requires careful handling of the login process and interaction with the controller plugin system. By modifying your login form to include the referer_url parameter and updating your plugin to handle this new input, you can achieve the desired behaviorâredirecting logged-in customers to the same product page they were on before logging in.
Remember to always test these changes thoroughly to ensure they don’t introduce any security vulnerabilities or unexpected behaviors. If you need further assistance with Magento 2 development or have other questions about e-commerce platforms, feel free to explore our Magento tutorial library for more insights and best practices.
Happy coding!