I am creating a module for redirect login/register success to checkout if customer has some items on quote but that's not working.
If I stop the event with that die;
on Observer/Redirect.php
above that redirect works, but I need continue for merge quotes.
events.xml
<?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="lima_customer_login_redirect" instance="Lima\CustomerLoginRedirect\Observer\Redirect" />
</event>
</config>
/Observer/Redirect.php
<?php
namespace Lima\CustomerLoginRedirect\Observer;
use \Magento\Framework\Event\Observer;
use \Magento\Framework\Event\ObserverInterface;
use \Magento\Quote\Model\QuoteFactory;
use \Magento\Framework\Controller\ResultFactory;
use \Magento\Framework\View\Element\Context;
class Redirect implements ObserverInterface
{
protected $_objectManager;
protected $_quote;
protected $_responseFactory;
protected $_url;
protected $_scopeConfig;
public function __construct(
Context $context,
QuoteFactory $quoteFactory,
\Magento\Framework\App\ResponseFactory $responseFactory,
\Magento\Framework\UrlInterface $url
) {
$this->_quote= $quoteFactory;
$this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->_responseFactory = $responseFactory;
$this->_url = $url;
$this->_scopeConfig = $context->getScopeConfig();
}
public function execute(Observer $observer)
{
$module_status = $this->_scopeConfig->getValue('customer_login_redirect/redirect/status', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
if($module_status){
$cart = $this->_objectManager->get('\Magento\Checkout\Model\Cart');
$quote = $cart->getQuote();
$totalItems = count($quote->getAllItems());
if($totalItems > 0){
$redirectionUrl = $this->_url->getUrl('onepagecheckout');
}else{
$redirectionUrl = $this->_url->getUrl('/');
}
$this->_responseFactory->create()->setRedirect($redirectionUrl)->sendResponse();
// die;
return $this;
}
}
}
Thanks.
Best Answer
Instead of using
customer_login
event, suggesting to you implement Plugin on class Magento\Checkout\Model\Session .Implement after plugin method on method loadCustomerQuote, From where you have to set the
setBeforeAuthUrl
for the customer session.As per as, Magento if you set your desired URL to
setBeforeAuthUrl
, then after login Magento automatically redirect method that desired URL.Most of the important point, Magento merge customer existing quote object with the current quote which is created before login quote login using loadCustomerQuote using event
customer_login
.So, now lets create a plugin.
First define plugin via
di.xml
at `app/code/{Vendor}/{Modulename}/etc/Plugin class
LoginRedirection.php
atapp/code{Vendor}\{Modulename}\Plugin\LoginRedirection
Also, this module should have:
app/code/{Vendor}/{Modulename}/etc/module.xml
.app/code/{Vendor}/{Modulename}/composer.json
app/code/{Vendor}/{Modulename}/registration.json
.Note that:
Donot forgot to '
php bin/magento cache:flush
.php bin/magento di:compile
.Aftre the changes .