In Magento 2, when a customer successfully logs in, the default behavior is to redirect them to the dashboard. However, in some cases, you may want to redirect the customer to a specific page, such as the homepage or a custom landing page, to enhance user experience and improve your site’s usability.
In this article, we will learn how we can redirect users to some custom page and custom URL.
Step 1 :- Create a Module
Step 2 :-
In the di.xml file, we define a plugin that intercepts the execution of the Magento\Customer\Controller\Account\LoginPost class. This is achieved through the <type> tag.
We name the plugin as “lokesh_cust_redirect” and specify the class where the plugin logic resides, which is Lokesh\MyModule\Plugin\Controller\Account\LoginPostPlugin. The plugin will execute the afterExecute method after the execute method of the original LoginPost class.
Path: app/code/Lokesh/MyModule/etc/di.xml
<?xml version="1.0"?>
<!--
/**
* Created By : Lokesh Pawar
*/
-->
<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="lokesh_cust_redirect" type="Lokesh\MyModule\Plugin\Controller\Account\LoginPostPlugin" />
</type>
</config>
Step 3:-
In the LoginPostPlugin.php file, we have the implementation of the plugin. The afterExecute method takes two parameters: $subject (the original LoginPost class instance) and $result (the result returned by the original execute method).
Inside the method, we set the $YourPath variable to the desired URL path or route identifier of the page we want to redirect the customer to. This allows you to easily change the redirect destination without modifying the core logic.
We then use $result->setPath($YourPath); to modify the result object and set the redirect path to the custom URL. By doing this, we override the default behavior of redirecting to the dashboard and direct the customer to the specified page.
Path: app/code/Lokesh/MyModule/Plugin/Controller/Account/LoginPostPlugin.php
<?php
/**
* Created By: Lokesh Pawar
*/
namespace Lokesh\MyModule\Plugin\Controller\Account;
/**
* Plugin for LoginPost class after customer click on login.
*/
class LoginPostPlugin
{
public function afterExecute(
\Magento\Customer\Controller\Account\LoginPost $subject,
$result
)
{
$YourPath = 'mypage/index/index'; // Change this path where you want to redirect
$result->setPath($YourPath);
return $result;
}
}
In the afterExecute
method, the $result
object is used to set the path where you want to redirect the customer after login. Replace 'mypage/index/index'
with the desired URL path or route identifier of the page you want to redirect the customer to. For example, 'cms/page/view/page_id/5'
will redirect the customer to a specific CMS page with ID 5.
FAQs
Q1: Can I redirect customer to an external URL after login?
Yes, you can. Instead of specifying a route identifier, you can use an absolute URL as the redirect path in the afterExecute
method. However, ensure that the URL is valid and secure to avoid any potential security risks.
Q2: Can I redirect customers based on their customer groups?
Certainly! You can customize the LoginPost
plugin to check the customer group and set different redirect paths based on their group. This way, you can offer personalized experiences to different customer segments.
Q3: Will this customization affect the admin login as well?
No, the customization made through the LoginPost
plugin will only affect customer logins. It will not impact the admin login functionality, which is managed separately.
Q4: How can I revert back to the default behavior of redirecting to the dashboard?
To revert back to the default behavior of redirecting customers to the dashboard after login, simply disable or remove the di.xml
file you created in the etc
directory.
Q5: Are there any other login-related customizations I can make in Magento 2?
Yes, Magento 2 offers a wide range of customization possibilities related to login and customer authentication. You can implement two-factor authentication, customize login error messages, and integrate social media logins, among other options.
Thank you for reading my Article! Feel free to share your thoughts or ask any questions in the comments section below and spread the word by sharing. Your engagement is appreciated!