Tutorialsplane

Magento 2 redirect Customer to Custom Page After Login


Magento 2 redirect Customer to Custom Page After Login– Sometimes we need to redirect customer to some specific page after login. It can be done in many ways like extending the core login functionality, using observers or using a plugin. Here in this article we are going to explain how you can implement this functionality.


Magento 2 redirect Customer to Custom Page After Login Plugin Example

The best way to implement this functionality is plugin Let us see how to do this using plugin.

Step 1 : Create di.xml

First create di.xml located at the below path-

/Vendor/Module/etc/frontend/di.xml

Magento 2 redirect Customer to Custom Page After Login Example:

<?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="vendor_module_loginpostplugin" type="\Vendor\Module\Plugin\LoginPostPlugin" sortorder="1"></plugin>
  </type>
</config>

Step 2 : Create Plugin

Now create plugin which will handle the redirect action.

/Vendor/Module/Plugin/LoginPostPlugin.php

Magento 2 redirect Customer to Custom Page After Login Example:

<?php /**
 *
 */
namespace Vendor\Module\Plugin;

/**
 *
 */
class LoginPostPlugin
{

    /**
     * Change redirect after login to home instead of dashboard.
     *
     * @param \Magento\Customer\Controller\Account $subject
     * @param \Magento\Framework\Controller\Result\Redirect $result
     */
    public function afterExecute(
        \Magento\Customer\Controller\Account\LoginPost $subject,
        $result);
    {
        $redirectPath = 'yourCustomUrl';
        $result-?>setPath($redirectPath);
        return $result;
    }

}

So you can do anything whatever you want after login action.