1

How to redirect customers to the checkout page after login when the cart is not empty for the customer?

Loginpost\Redirect\etc\frontend\di.xml

<?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="loginpost_redirect_loginpostplugin" type="\LoginPost\Redirect\Plugin\LoginPostPlugin" sortOrder="1" />
  </type>
</config>

Loginpost\Redirect\Plugin\LoginPostPlugin.php

   <?php

namespace Loginpost\Redirect\Plugin;

class LoginPostPlugin
{
    public function afterExecute(
        \Magento\Customer\Controller\Account\LoginPost $subject,
        $result)
    {
        $result->setPath('/checkout');
        return $result;
    }

}

Now I'm getting following error after adding this plugin to my project.

plugin class does not exist error

1

You can use a plugin for your requirement.

Create a di.xml file /Vendor/Module/etc/frontend/di.xml with below content

<?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" />
  </type>
</config>

Create LoginPostPlugin.php file /Vendor/Module/Plugin/LoginPostPlugin.php with below content.

<?php

namespace Vendor\Module\Plugin;

class LoginPostPlugin
{
    public function afterExecute(
        \Magento\Customer\Controller\Account\LoginPost $subject,
        $result)
    {
        $result->setPath('/checkout');
        return $result;
    }

}

Reference from Magento 2 - Redirect customer to custom page after login

  • i'm new to magento and there is no directory named module inside vendor. instead of that can I create this plugin inside app/code directory ? – ashanr Aug 12 at 8:33
  • 1
    You need to create a new folder under app/code/[vendor_name]/[module_name]. You can choose the Vendor and Module name as per your requirements. For example, /Sumit/CustomerLogin/Plugin/LoginPostPlugin.php – Sumit Aug 12 at 9:22
  • I have don't this. But still not redirecting to checkout page after login. Are these two the only files inside this module ? – ashanr Aug 13 at 8:51
  • 1
    No, you need to create basic extension having Registration.php and module.xml file in etc folder. For example, /Sumit/CustomerLogin/etc/module.xml – Sumit Aug 13 at 8:53
  • 1
    Perfect, you can check it by current cart quote of the customer while login in the same plugin. If the customer having any product in the cart then redirect to the checkout. – Sumit Aug 16 at 7:13
0

you need to create observer that fire event after customer login.

Create observer as below

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_login">
        <observer name="customer_login_observer" instance="<Your_packagename>\<Your_modulename>\Observer\RedirectCustommer" />
    </event>
</config>

Create RedirectCustommer.php under "Your_packagename\Your_modulename\Observer"

<?php

namespace YourPackage\YourModle\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Controller\ResultFactory;

class RedirectCustommer implements ObserverInterface
{

    /**
     *@var\Magento\Quote\Model\QuoteFactory
     */
    protected $quoteFactory;


    public function __construct(      
        \Magento\Framework\App\ResponseFactory $responseFactory,      
        \Magento\Quote\Model\QuoteFactory $quoteFactory       
    )
    {       
        $this->quoteFactory = $quoteFactory;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {   
        $customer = $observer->getEvent()->getCustomer();
        $landingurl =  $this->getRedirectUrl($customer->getId());
        if ($landingurl) {
                $resultRedirect = $this->responseFactory->create();
                $resultRedirect->setRedirect($landingurl)->sendResponse('200');
                exit();
        }
    }

    public function getRedirectUrl($customerId)
    {

        $quote = $this->quoteFactory->create()->getCollection()->addFieldToFilter('customer_id',$customerId);
        if($quote->getId())
        {
            return $block->getUrl('checkout', ['_secure' => true]);
        }
        return false;

    }

    }
  • Amit, Plugin is the best method to override Magento's functionality instead of using observer. – Sumit Aug 12 at 7:48
  • Considered,But plugin is what used for change argument behavior and return value. – Amitkumar solanki Aug 12 at 8:26
  • @Amitkumarsolanki so what is the ideal way to do this ? using an observer or plugin. i'm using magento 2.1 for this project. – ashanr Aug 12 at 8:29
  • 1
    Both will be fine. but in some cases you should consider loading time for both. – Amitkumar solanki Aug 12 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.