0

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

2
  • Hi SO Community, Please guide me to achieve functionality. Thanks
    – Jackson
    Oct 5, 2016 at 2:20
  • Guys less than 1 day left please try to help if u can. Thanks
    – Jackson
    Oct 6, 2016 at 5:58

3 Answers 3

1

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.

8
  • Hi @Raphael. Yes u r correct it's Custom. Thanks for answer i have changed things now i think issue with $actionName = $observer->getEvent()->getRequest()->getFullActionName(); $controller = $observer->getControllerAction(); $response->setRedirect(Mage::getUrl("checkout/onepage")); Mage::getSingleton("checkout/session")->setNoCartRedirect(true); I think last 2 lines are for Magento 1 need to convert in Magento 2
    – Jackson
    Sep 26, 2016 at 8:49
  • @AnkitShah see my updated answer ;) Sep 26, 2016 at 8:54
  • Thanks @Raphael. I updated code as you said final code will be public function execute(\Magento\Framework\Event\Observer $observer) { $actionName = $observer->getEvent()->getRequest()->getFullActionName(); $controller = $observer->getControllerAction(); $controller->getResponse()->setRedirect($this->_urlManager->getUrl('checkout')); return $this->_checkoutSession->setNoCartRedirect(true); } . Still issue persist. When i Add to Cart product from Product Listing. It will stay on Same Page. Not redicting to Checkout Page.
    – Jackson
    Sep 26, 2016 at 9:07
  • I have checked with FireBug /Inspect Element it's going to Observer & Checkout page. But it stays the same Category page. Not redirecting. Strange. It's working fine from Product Detail page :). Issue with Only Listing
    – Jackson
    Sep 26, 2016 at 9:09
  • 1
    Hi @Raphael can you please help me out to solve it's 50% Done. 50% Pending.
    – Jackson
    Oct 4, 2016 at 1:15
0

It will not work in listing page so you have to create one plugin for Checkout/Controller/Cart/Add.php and write redirect logic here.

0
0

Follow below steps for redirection to checkout page (Skip shopping cart page):

  1. Create default.xml file at view/frontend/layout directory in your module to override list.phtml and addtocart.phtml file
<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <referenceBlock name="product.info.addtocart">
                <action method="setTemplate">
                    <argument name="template" xsi:type="string">Vendor_Module::product/view/addtocart.phtml</argument>
                </action>
            </referenceBlock>
            <referenceBlock name="category.products.list">
                <action method="setTemplate">
                    <argument name="template" xsi:type="string">Vendor_Module::product/list.phtml</argument>
                </action>
            </referenceBlock>
         </referenceContainer>
    </body>
</page>
  1. Override both files in your module and get base url in those template files

  2. Add below line in both files inside form tag

<input type="hidden" name="return_url" value="<?= $baseUrl."checkout"; ?>" >
where $baseUrl -> Is base URL of your site.

NOTE: In addtocart.phtml file you can't find form tag so, you can add above line before

<div class="box-tocart">

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.