How to Redirect Customer to Custom Page after Login in Magento 2?
Today, we’re going to teach you guys how to redirect customers to a custom page after login in your Magento 2 store.
By default, Magento 2 redirects customers to the “My Account” page after successful login. However, you can certainly change where to redirect customers after login in Magento 2.
Magento is a flexible CMS that lets you tweak its features and customize them according to your business needs.
And when a customer logs in to your Magento 2 store, it’s a prime opportunity to redirect their attention to anything you want.
So why not redirect them to your latest products page or products with discounts/special offers?
It is already a proven tactic to generate more sales.
In this post, we’re going to show you exactly how to redirect customers to a custom page after login in Magento 2.
Step-by-Step Process to Redirect Customers to Custom Page After Login in Magento 2
Please follow the below steps to learn how to redirect customers to a custom page after login in your Magento 2 store.
Step – 1
First of all, create a di.xml file in the Vendor/Extension/etc/frontend folder and paste the following code:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="\Magento\Customer\Controller\Account\LoginPost"> <plugin name="redirect_custom_url" type="\Vendor\Extension\Plugin\RedirectCustomUrl" sortOrder="1"/> </type> </config>
Step – 2
After that, create a RedirectCustomUrl.php file in the Vendor/Extension/Plugin folder and paste the following code:
<?php namespace Vendor\Extension\Plugin; class RedirectCustomUrl { public function afterExecute( \Magento\Customer\Controller\Account\LoginPost $subject, $result) { $customUrl = 'sales/order/history'; $result->setPath($customUrl); return $result; } }
Here, replace the ‘sales/order/history’ with the path of your required page in the $customUrl variable.
Conclusion
And that’s about it!
This is how you can redirect customers to a custom page after login in Magento 2.
We hope that you found this tutorial useful. If you have any questions, please ask them in the comments below.
And if you need our professional assistance, feel free to contact us anytime.