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.