In this article, I’ll explain How to redirect to a particular page after a customer login In Magento 2. In default Magento 2, customers are redirected to their dashboard page after logging in however sometimes we are required to redirect customers to a specific page after login then you need to follow the below steps.
How to redirect to a particular page after a customer login In Magento 2?
Step 1: Let’s assume you have created a simple module. Now you need to make a plugin to redirect to a particular page.
Create a di.xml file at app/code/Vendor/Module/etc directory and paste the code below.
<?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="technicalmrstar_custom_redirect" type="Vendor\Module\Plugin\Controller\Account\LoginPost" />
</type>
</config>
Step 2: After declare the plugin, you need to create LoginPost.php file at app/code/Vendor/Module/Plugin/Controller/Account directory and paste the code below.
<?php
namespace Vendor\Module\Plugin\Controller\Account;
class LoginPost
{
/**
* Change redirect after login to home instead of dashboard.
*
* @param \Magento\Customer\Controller\Account\LoginPost $subject
* @param \Magento\Framework\Controller\Result\Redirect $result
*/
public function afterExecute(
\Magento\Customer\Controller\Account\LoginPost $subject,
$result
) {
$result->setPath('technicalmrstar/index/index'); // Set path where you want to redirect
return $result;
}
}
As per best practices, a plugin should always be created inside the Plugin directory.
Now you just need to clean the cache and check it after login. As a result, you may see you will be redirected to a custom page after login.
You may also like this:
I hope this blog clearly explains How to redirect to a particular page after a customer login in Magento 2. If I missed anything or you need additional information, please feel free to leave a comment on this blog. I will respond with a proper solution.