Magento 2 - Go directly to the checkout page when adding a product to the cart

Fredd picture Fredd · May 31, 2016 · Viewed 7.4k times · Source

I am writing an extension which allows to go directly to the checkout page when clicking on the add-to-cart button on the product page. I found a solution for Magento 1 here and I tried to adapt it to Magento 2. Here are my files:

File etc/frontend/events.xml:

<?xml version="1.0" encoding="utf-8" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_cart_add_product_complete">
        <observer
            name="mycompany_go_to_checkout"
            instance="MyCompany\GoToCheckout\Observer\GoToCheckout" />
    </event>
</config>

File Observer/GoToCheckout.php:

namespace MyCompany\GoToCheckout\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class GoToCheckout implements ObserverInterface
{
    protected $_url;

    public function execute(Observer $observer)
    {
        $urlInterface = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\UrlInterface');
        $url = $urlInterface->getUrl('checkout');
        $observer->getControllerAction()->getResponse()->setRedirect($url);
    }
}

What should I change or add to make it work?

Any guidance will be appreciated.

Answer

Indian picture Indian · Jun 1, 2017

Below is the full working code. I used if for my module.

<?xml version="1.0" encoding="utf-8" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_cart_add_product_complete">
        <observer
            name="mycompany_go_to_checkout"
            instance="MyCompany\GoToCheckout\Observer\GoToCheckout" />
    </event>
</config>

And observer code is:

namespace MyCompany\GoToCheckout\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class GoToCheckout implements ObserverInterface
{
    protected $uri;
    protected $responseFactory;
    protected $_urlinterface;

 public function __construct(
        \Zend\Validator\Uri $uri,
        \Magento\Framework\UrlInterface $urlinterface,
        \Magento\Framework\App\ResponseFactory $responseFactory,
         \Magento\Framework\App\RequestInterface $request
    ) {
        $this->uri = $uri;
        $this->_urlinterface = $urlinterface;
        $this->responseFactory = $responseFactory;
        $this->_request = $request;
    }

    public function execute(Observer $observer)
    {
        $resultRedirect = $this->responseFactory->create();
        $resultRedirect->setRedirect($this->_urlinterface->getUrl('checkout'))->sendResponse('200');
        exit();
    }
}

But this code only works for detail page. In listing page it will not work because its managed by Ajax. So what was the solutions for that? Easy, just create one Plugin for Checkout/Controller/Cart/Add.php and write your logic in this file.