Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 922
Fredd Avatar asked Feb 07 '23 21:02

Fredd


2 Answers

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.

like image 113
Indian Avatar answered May 12 '23 04:05

Indian


you must use following code to redirect from Observer in Magento2

public function execute(Observer $observer)
{
  $redirect = $observer->getEvent()->getRedirect();
  $redirect->setRedirect(true)->setPath('checkout')->setArguments([]);
  return $this;
}
like image 31
Emizen Tech Avatar answered May 12 '23 05:05

Emizen Tech