Magento 2 - Go directly to the checkout page when adding a product to the cart
<p>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 <a href="http://www.magecorner.com/bypass-cart-and-go-to-checkout/" rel="noreferrer">here</a> and I tried to adapt it to Magento 2. Here are my files:</p>

<p>File etc/frontend/events.xml: </p>

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

<p>File Observer/GoToCheckout.php:
    

<pre><code>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()-&gt;get('Magento\Framework\UrlInterface');
        $url = $urlInterface-&gt;getUrl('checkout');
        $observer-&gt;getControllerAction()-&gt;getResponse()-&gt;setRedirect($url);
    }
}
</code></pre>

<p>What should I change or add to make it work?</p>

<p>Any guidance will be appreciated.</p>