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

Sumit
  • 4,875
  • 2
  • 19
  • 35
ashanr
  • 115
  • 1
  • 12

2 Answers2

2

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

Sumit
  • 4,875
  • 2
  • 19
  • 35
  • 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 '19 at 08: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 '19 at 09: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 '19 at 08: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 '19 at 08:53
  • I have added registration.php and module.xml inside etc folder to create a basic extension and run bin/magento setup:upgrade to add this plugin to the project. it's run without error but i get a error 1 exception(s): Exception #0 (InvalidArgumentException): Plugin class LoginPost\Redirect\Plugin\LoginPostPlugin doesn't exist , plugin file content and module.xml added in the question above. can you figure out what is went wrong with this. – ashanr Aug 13 '19 at 09:33
  • Have you run the php bin/magento s:up and other deployment commands after adding the module? – Sumit Aug 13 '19 at 10:31
  • You need to change your type in di.xml file from type="\LoginPost\Redirect\Plugin\LoginPostPlugin to type="\Loginpost\Redirect\Plugin\LoginPostPlugin – Sumit Aug 13 '19 at 10:51
  • redirects work fine. but is there a possibility to check if cart is empty inside this plugin ? – ashanr Aug 16 '19 at 07:05
  • 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 '19 at 07:13
  • Can I use this __construct function inside plugin to check if cart is not empty? https://magento.stackexchange.com/a/269134/69209 – ashanr Aug 16 '19 at 07:23
  • Yes, you can use this in your plugin to check cart is empty or not. – Sumit Aug 16 '19 at 07:26
  • this helped me to create the solution. thanks lot for the support you have given. – ashanr Aug 16 '19 at 09:33
  • You're welcome. :) – Sumit Aug 16 '19 at 09:35
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;

    }

    }
Amitkumar solanki
  • 875
  • 1
  • 7
  • 20