5

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.

Fredd
  • 51
  • 1
  • 5

4 Answers4

4

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.

Indian
  • 645
  • 7
  • 22
  • I am trying to redirect to checkout from product detail page, but this is not working in magento 2.2.5. Also can you tell me, how to redirect from plugin? – Nitesh Sep 01 '18 at 19:42
  • 1
    Can you please say the steps to implement this code? I am new for making plugins in magento. – Jithin U. Ahmed Oct 06 '18 at 07:11
2

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;
}
Emizen Tech
  • 3,529
  • 1
  • 17
  • 33
  • I tried the code and I added some log outputs to follow the flow. I appears it stops at the second line: `$redirect->setRedirect(true)->setPath('checkout')->setArguments([]);` There is no additional information in the log files. – Fredd Jun 12 '16 at 20:16
  • Throwing exception "Fatal error: Uncaught Error: Call to a member function setRedirect() on null in /path_to_module_observer/observer.php:100" – Nitesh Feb 02 '18 at 01:37
2

Magento already provide these setting with admin settings,

Magento1

Admin > System > Configuration > Checkout > Shopping Cart > After Adding a Product Redirect to Shopping Cart >YES.

Magento2

Admin-> Store ->Configuration->Sales->Checkout ->After Adding a Product Redirect to Shopping Cart

Set Dropdown value as requirements,

S.P
  • 293
  • 1
  • 3
  • 11
0

For Magento 2 use this code in Observer:

    public function execute(Observer $observer)
    {
        $observer->getRequest()->setParam('return_url', $this->urlInterface->getUrl('checkout'));
        return $this;
    }
Ionuț Gorgos
  • 61
  • 1
  • 5