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.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI 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.
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;
}
}
}
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.
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!