I'm trying to improve the sales pipeline of Magento 2 when allow guest checkout is disabled. By default after creating an account with allow guest checkout disabled, a user is redirected to the account center regardless of whether they clicked the account login button or the checkout button.
I would like to use an observer to redirect a user to the cart if they were prompted to log in after clicking on the cart button. Is there a way inside of my observer to know what link was clicked? (checkout or account)
I'm firing the following observer on customer_data_object_login
.
Observer.php:
<?php
namespace Vendor\NameSpace\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class Redirect implements \Magento\Framework\Event\ObserverInterface
{
protected $_responseFactory;
protected $_url;
protected $_session;
public function __construct(
\Magento\Customer\Model\Session $session,
\Magento\Framework\App\ResponseFactory $responseFactory,
\Magento\Framework\UrlInterface $url
) {
$this->_session = $session;
$this->_responseFactory = $responseFactory;
$this->_url = $url;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$isCustomerLoggedIn = $this->_session->isLoggedIn();
if ($isCustomerLoggedIn) {
$event = $observer->getEvent();
$CustomRedirectionUrl = $this->_url->getUrl('checkout/cart');
$this->_session->setBeforeAuthUrl($CustomRedirectionUrl);
return $this;
}
}
}