Magento – Magento 2: Skipping Shopping Cart Page after Add to Cart

addtocartevent-observermagento2PHPshopping-cart

I'm using Magento 2 CE Version 2.1.0.

I have Googled & also referred some StackOverflow Q&A.

I have done the following things:

app\code\Custom\Module\etc\frontend\events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch_skip_shoppingcart">
        <observer name="skip_shopping_cat_page" instance="Custom\Module\Observer\SkipShoppingCartObserver" />
    </event>
</config>

app\code\Custom\Module\Observer\SkipShoppingCartObserver.php

namespace Custom\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class SkipShoppingCartObserver implements ObserverInterface {


    /**
     * @var \Magento\Framework\App\ActionFlag
     */
    protected $_actionFlag;

    /**
     * @var \Magento\Framework\Message\ManagerInterface
     */
    protected $messageManager;

    /**
     * @var \Magento\Framework\App\Response\RedirectInterface
     */
    protected $redirect;

    /**
     * @var DataPersistorInterface
     */
    private $dataPersistor;

    /**
     * @param \Magento\Captcha\Helper\Data $helper
     * @param \Magento\Framework\App\ActionFlag $actionFlag
     * @param \Magento\Framework\Message\ManagerInterface $messageManager
     * @param \Magento\Framework\App\Response\RedirectInterface $redirect
     */
    public function __construct(
    \Magento\Captcha\Helper\Data $helper, \Magento\Framework\App\ActionFlag $actionFlag, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\Framework\App\Response\RedirectInterface $redirect
    ) {
        $this->_helper = $helper;
        $this->_actionFlag = $actionFlag;
        $this->messageManager = $messageManager;
        $this->redirect = $redirect;
    }

    public function afterAddToCart(\Magento\Framework\Event\Observer $observer) {
        $controller = $observer->getControllerAction();
        $response = $observer->getResponse();
        echo 2;
        exit;
        $response->setRedirect(Mage::getUrl("checkout/onepage"));
        Mage::getSingleton("checkout/session")->setNoCartRedirect(true);
    }

    /**
     *     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer) {
        $actionName = $observer->getEvent()->getRequest()->getFullActionName();
        $controller = $observer->getControllerAction();
        $controller->getResponse();
        echo 1;
        exit;
    }
}

After a product has been added to cart, it's not going neither of above function.

[UPDATE]

Thanks @Raphael for great help. It's working fine on Product Detail Page, but in listing still have issue. In listing after clicking on "Add to Cart" it's calling below services & Stays on same page. May be AJAX issue? Not sure

It keep displaying "Adding.." in Grey

enter image description here

Best Answer

Well it's because the event you're trying to observe never gets dispatched by Magento. In Magento 2 controller_action_predispatch_skip_shoppingcart does not exist. I assume you wrote that event name by yourself assuming it had to be custom.

You need to replace that with controller_action_postdispatch_checkout_cart_add to observe the process after a product has been added to cart.

On top of that, you never call your afterAddToCart method in your observer, so only your execute() method will be called.

Finally, you have some Magento 1 code in there, so you need to replace:

    $response->setRedirect(Mage::getUrl("checkout/onepage"));
    Mage::getSingleton("checkout/session")->setNoCartRedirect(true);

With:

    $response->setRedirect($this->_urlManager->getUrl('checkout/onepage'));
    $this->_checkoutSession->setNoCartRedirect(true);

Where $this->_urlManager needs to be injected via Dependency Injection using the \Magento\Framework\UrlInterface class and $this->_checkoutSession using the \Magento\Checkout\Model\Session class.

Related Topic