0

I want to if customer login and customer cart is not empty so customer redirect checkout page other wise customer login and customer cart is empty customer redirect my account page.

Please help me,

Thanks.

6
  • Hi! II'm not sure what you want to achieve. The redirect must occur on login action, right? So, when user logs in: If cart is empty -> redirect to account page; if cart is not empty -> redirect to checkout page. Is that right?
    – dudzio
    May 29, 2021 at 10:02
  • Yes right. when user logs in: If cart is empty -> redirect to account page; if cart is not empty -> redirect to checkout page May 29, 2021 at 10:23
  • Thanks! I'll prepare the answer for you ;)
    – dudzio
    May 29, 2021 at 10:25
  • Thanks! @dudzio May 29, 2021 at 10:31
  • @MohitPatel Please check the answer and update me.
    – Msquare
    May 30, 2021 at 12:52

3 Answers 3

1

You can check quote items count after LoginPost action.

The di.xml (preferably frontend/di.xml since you only need to do the redirect on front) code:

<?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_redirect_after_login" type="Vendor\Magento\Plugin\RedirectAfterLogin" sortOrder="1" />
    </type>
</config>

Check items count and handle redirects in your plugin code:

<?php

namespace Vendor\Module\Plugin;

class RedirectAfterLogin
{

    protected \Magento\Checkout\Model\Session $session;

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

    /**
     * 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
    ) {
        if ($this->getInCartItems()) {
            $result->setPath('checkout');
        } else {
            $result->setPath('customer/account');
        }

        return $result;
    }

    /**
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    protected function getInCartItems()
    {
        try {
            $quote = $this->session->getQuote();
            return $quote->getItemsCount();
        } catch (\Exception $e) {
            return 0;
        }
    }
}
1
1

Try This Code

app/code/VendoreName/ModuleName/etc/di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">

    <type name="\Magento\Customer\Controller\Account\LoginPost">
        <plugin name="vendor_module_loginpostplugin" type="VendoreName\ModuleName\Plugin\LoginPostPlugin" sortOrder="1" />
    </type>

</config>

app/code/VendoreName/ModuleName/Plugin/LoginPostPlugin.php

<?php

namespace VendoreName\ModuleName\Plugin;

class LoginPostPlugin
{

    protected $customerSession;
    protected $quoteFactory;

    public function __construct(
        \Magento\Quote\Model\QuoteFactory $quoteFactory,
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->quoteFactory = $quoteFactory;
        $this->customerSession = $customerSession;
    }

    public function afterExecute(
        \Magento\Customer\Controller\Account\LoginPost $subject,
        $result)
    {
        $customerData = $this->customerSession->getCustomer();
        $quote = $this->quoteFactory->create()->loadByCustomer($customerData);
        $items = $quote->getAllItems();
        if (count($items)) {
            $result->setPath('checkout');
            return $result;
        }
        return $result;
    }

}

I Hope This Helps You.

0

Please follow the below code

1.Create a events.xml file under "Namespace/Module/etc/frontend"

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<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="custom_login_data" instance="Namespace\Module\Observer\CustomerData" shared="false" />
    </event>
</config>

2.Create a observer(CustomerData.php) under "Namespace/Module/Observer"

    <?php
        /**
         * Copyright © 2013-2017 Magento, Inc. All rights reserved.
         * See COPYING.txt for license details.
         */
        namespace Namespace\Module\Observer;
        
        use Magento\Framework\Event\ObserverInterface;
        
        class CustomerData implements ObserverInterface
        {
            protected $_request;
            protected $groupRepository;You can also use the cart helper
            protected $cartHelper;
            private $responseFactory;
            private $url;
            
            public function __construct(\Magento\Framework\App\RequestInterface $request,\Magento\Customer\Api\GroupRepositoryInterface $groupRepository,         \Magento\Checkout\Helper\Cart $cartHelper,\Magento\Framework\App\ResponseFactory $responseFactory,\Magento\Framework\UrlInterface $url
    )
            {
                $this->_request = $request;
                $this->groupRepository = $groupRepository;
                $this->cartHelper = $cartHelper;
                $this->responseFactory = $responseFactory;
                $this->url = $url;
    
            }
        
            /**
             *
             * @param \Magento\Framework\Event\Observer $observer
             * @return void
             */
            public function execute(\Magento\Framework\Event\Observer $observer)
            {
              if ($this->cartHelper->getItemsCount() > 0) {
               $redirectionUrl = $this->url->getUrl('checkout');
               $this->responseFactory->create()->setRedirect($redirectionUrl)->sendResponse();
              }
                return $this;
            }
      
           
            }
 }

I hope it will help you!

3
  • i have already try this solution and again try you answer but not success. May 29, 2021 at 12:20
  • Not success mean..any error or what? May 30, 2021 at 6:28
  • No. Not showing any error May 30, 2021 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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