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.


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');
            return $this;

1 Answer 1


If i understand your problem correct, you can change the behaviour of what happens after a user authenticates. In de backend go to Stores > Configuration > Customers > Customer Configuration > Login Options

Make sure the option Redirect Customers to Account Dashboard After Logging In is set to No. This way after logging in, the customer goes to the previous page. enter image description here

If this is not the case and you need it for some other reason:

You will need to know what the current request/url is.

One way to get that is to use the \Magento\Framework\UrlInterface which I see you already have injected in your Observer. This has a method getCurrentUrl() which returns the (complete) current url. This can a bit tricky because it returns the uncleaned url and can have extra data like query parameters.

I would suggest you to inject \Magento\Framework\App\Request\Http which holds the current request information. This way you can get information about the current controller, action and route.

 $moduleName = $this->request->getModuleName();
 $controller = $this->request->getControllerName();
 $action     = $this->request->getActionName();
 $route      = $this->request->getRouteName();

Now, if you see that the combination of controller, action and route is what you try to hide, you can redirect the user.

  • You are correct about what I'm looking for. except I also don't want the user to be redirected back to the previous page. As an example, a user adds an item to their cart then clicks "checkout" at the top of the page. They're now prompted to log in. After this user logs in they should continue on with their goal and be redirected to checkout. instead, the user is either directed to their cart or taken back to the same page. This adds an extra step to the sales process. Commented Jan 3, 2018 at 23:57
  • I'll see what I can get out of "\Magento\Framework\App\Request\Http". Commented Jan 3, 2018 at 23:58

Your Answer

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

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