Blog

Redirect Customer After Login Magento2

In this article, we are going to learn about how to redirect customer based on some condition to the other pages of the website.
Let’s say, I want to redirect customer to my account page if customer login and customer cart is empty, but if the customer is logged in and customer cart is not empty customer then redirect to checkout page.
While implementing this, you will also learn how to use plugin.
To accomplish this, we need follow below steps:
    • Create Custom Module
    • Register Plugin
    • Implement Redirect Logic

Step 1: Create Custom Module

It is necessary to create etc folder and add the module.xml file in it

  app/code/Thecoachsmb/CustomerLoginRedirect/etc/module.xml

Contents would be:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Thecoachsmb_CustomerLoginRedirect" > 
     <sequence> 
         <module name="Magento_Customer"/> 
     </sequence>
 </module> 
</config>

To register the module, create a registration.php file in the app/code/Thecoachsmb/CustomerLoginRedirect/registration.php

Contents would be:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
	\Magento\Framework\Component\ComponentRegistrar::MODULE,
	'Thecoachsmb_CustomerLoginRedirect',
	__DIR__
);

Step 2 – Register Plugin 

The next is to tell to Magento that we are creating our own file to change the logic. To do this, we need to create app/code/Thecoachsmb/CustomerLoginRedirect/etc/frontend/di.xml

and content will be as follows:

<?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="thecoachsmb_customerLogin_redirect_after_login" type="Thecoachsmb\CustomerLoginRedirect\Plugin\RedirectAfterLogin" sortOrder="1" />
    </type>
</config>

Step 3 – Implement Redirect Logic

In this step, we are going to create our own function to implement redirect logic. if the customer is logged and has items in the cart then redirect to the checkout page. Otherwise, it will redirect to the customer account page.

To do this, we need to create app/code/Thecoachsmb/CustomerLoginRedirect/Plugin/RedirectAfterLogin.php

and the content will be as below:

<?php
namespace Thecoachsmb\CustomerLoginRedirect\Plugin;

class RedirectAfterLogin
{
    protected $_checkoutSession;

    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession
    ) {
        $this->_checkoutSession = $checkoutSession;
    }

    /**
     * Redirect after login in some conditions
     *
     * @param \Magento\Customer\Controller\Account\LoginPost $subject
     * @param \Magento\Framework\Controller\Result\Redirect $result
     */
    public function afterExecute(
        \Magento\Customer\Controller\Account\LoginPost $subject,
        $result
    ) {
        $itemCount = $this->_checkoutSession->getQuote()->getItemsCount();        
        if ($itemCount) {
            return $result->setPath('checkout');
        }
        return $result;
    }
}

That’s it!!

In this article, we have learn how to implement customer login redirect logic. Thanks for reading this article. See you in next article.

Leave a Reply

Your email address will not be published. Required fields are marked *

🚀 Let’s Connect on LinkedIn! 🚀

Want to level up your skills and knowledge? 🚀
Follow us on LinkedIn to get:
✅ Expert insights on business growth
✅ Daily tips to sharpen your skills
✅ Exclusive updates from The Coach SMB

Let's grow together!