2

I am new to Magento 2 and want to change the action of add to cart button on product and category pages directly to the checkout page.

Magento 2: Skipping Shopping Cart Page after Add to Cart

I referred to the solution given in above link, but not able to find the directory which is required to be updated -- event.xml

1 Answer 1

1

Assume that add to Cart url is www.example.com/checkout/cart/add.

In this case, you can use event checkout_cart_add_product_complete .

Using this event,You can Redirect to last page from here you have click on AddtoCart

Observer

<?php
# app/code/Devamitbera/CartRedirect/Observer/ObserverToskipCartgotoCheckout
namespace Devamitbera\CartRedirect\Observer;

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

class ObserverToskipCartgotoCheckout implements ObserverInterface
{


    /**
     *
     * @var \Magento\Framework\UrlInterface
     */
    protected $_urlManager;


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

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


    private $_pageConfig;

    /**
     * @param \Magento\Framework\App\ActionFlag $actionFlag
     * @param \Magento\Framework\Message\ManagerInterface $messageManager
     * @param \Magento\Framework\App\Response\RedirectInterface $redirect
     */
    public function __construct(
        \Magento\Framework\App\ActionFlag $actionFlag,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        \Magento\Framework\UrlInterface $urlManager,
        \Magento\Framework\View\Page\Config $pageConfig     
    ) {
        $this->_actionFlag = $actionFlag;
        $this->messageManager = $messageManager;
        $this->_urlManager = $urlManager;
        $this->redirect = $redirect;

    }

    /**
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $request = $observer->getRequest();
        $response = $observer->getResponse();
        $product  = $observer->getProduct();
      if (!$request->isAjax()) {
            $url = $this->_urlManager->getUrl('checkout/index/index');
            //$this->_objectManager->get('Psr\Log\LoggerInterface')->critical($url);
            $request->setParam('return_url',$url);
        }
    }

}

event file:

<?xml version="1.0"?>
<!--
#app/code/Devamitbera/CartRedirect/etc/events.xml
-->
<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="forrceToCheckout" instance="Devamitbera\CartRedirect\Observer\ObserverToskipCartgotoCheckout" />
    </event>
</config>
5
  • Does it mean that a new file has to be added in custom theme module directory with the event defined in the discussion.
    – avatar
    Jan 8, 2017 at 15:32
  • I have uploaded a customized theme over Magento/blank, and I am not sure where to add these custom file for execution in the custom theme only. The directory structure is same as Luma. Can you direct me to the correct path for execution
    – avatar
    Jan 8, 2017 at 18:46
  • I have added the directories as required in App/code. I took a reference of this article - cloudways.com/blog/add-additional-options-in-magento-2. Still I get Error 503. What might be the error in execution, I'm not able to reach there.
    – avatar
    Jan 8, 2017 at 21:37
  • @avatar,you need to create a custom module for this
    – Amit Bera
    Jan 9, 2017 at 8:44
  • getting this url 503 internal server
    – Amit Bera
    Jan 9, 2017 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.