Magento, Magento 2

How to Redirect to The Particular Page After Login in Magento 2

How to Redirect to The Particular Page After Login in Magento 2

In this tutorial, Today I will explain to how to redirect to the particular page after login in Magento 2. In default Magento 2, redirect customer to their dashboard page after customer login.

Now, If you want to redirect customer to the particular page after customer login then, you need to follow the below steps for that

You may also like this :

1) Let’s assume that you have created simple module. Now, you need to create plugin for redirect to particular page. Create di.xml file at app/code/RH/Helloworld/etc/frontend/ and paste the below code :

<?xml version="1.0"?>
<!--
/**
 * Created By : Rohan Hapani
 */
-->
<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="rh_custom_redirect" type="RH\Helloworld\Plugin\Controller\Account\LoginPost" />
  </type>
</config>

2) After that, You need to create LoginPost.php plugin file at app/code/RH/Helloworld/Plugin/Controller/Account/ and paste the below code :

<?php
/**
 * Created By : Rohan Hapani
 */
namespace RH\Helloworld\Plugin\Controller\Account;

/**
 * Plugin of Post login customer action.
 */
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('helloworld/index/index'); // Set path where you want to redirect
        return $result;
    }
}

Now, You need to just clean cache and check it after login. You can see that customer will be redirect on custom page after login.

That’s it !!!

I hope this blog is easy to understand about how to redirect to the particular page after login in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.

Stay Safe and Stay Connected !!

Tagged , ,