How to redirect from my account page to the homepage when a customer is logging in?

  • Use the below code in the di.xml file at JJ/Customproductshow/etc/frontend
<?xml version="1.0" encoding="UTF-8"?>
<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="loginPost" type="JJ\Customproductshow\Plugin\Account\LoginPost" sortOrder="10"/>
    </type>
</config>
  • Paste the below code in LoginPost.php file at JJ/Customproductshow/Plugin
<?php
namespace JJ\Customproductshow\Plugin\Account;

use Magento\Customer\Controller\Account\LoginPost as MagentoLoginPost;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\App\Response\RedirectInterface;
use Magento\Framework\UrlInterface;

class LoginPost
{
    protected $_storemanager;
    protected $_resultRedirect;

    public function __construct(\Magento\Store\Model\StoreManagerInterface $storemanager, \Magento\Framework\Controller\Result\Redirect $resultRedirect)
    {
        $this->_storemanager=$storemanager;
        $this->_resultRedirect=$resultRedirect;
    }

    public function afterExecute(\Magento\Customer\Controller\Account\LoginPost $subject, $result)
    {
        $redirectUrl= $this->_storemanager->getStore()->getBaseUrl();
        $this->_resultRedirect->setPath($redirectUrl);
        return $this->_resultRedirect;
    }
}

These files should be added to an existing custom module or added to a new module.

Leave a comment

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