0

I want to redirect the user to the checkout page on Adding product to cart, Please help.

1 Answers1

1

@alexeyboltynov First you have enable skip cart option from magento 2 admin. you can found that option easily in your theme settings.

And you have to overwrite core file path: "vendor\magento\module-checkout\Controller\Cart\Add.php"

Find code:

if (!$this->_checkoutSession->getNoCartRedirect(true)) {
    if (!$this->cart->getQuote()->getHasError()) {
        if ($this->shouldRedirectToCart()) {
            $message = __(
                'You added %1 to your shopping cart.',
                     $product->getName()
                );
                    $this->messageManager->addSuccessMessage($message);
        } else {
                        $this->messageManager->addComplexSuccessMessage(
                            'addCartSuccessMessage',
                            [
                                'product_name' => $product->getName(),
                                'cart_url' => $this->getCartUrl(),
                            ]
                        );
                    }
                }
                return $this->goBack(null, $product);
            }

Replace it with:

if (!$this->_checkoutSession->getNoCartRedirect(true)) {
           return $this->resultRedirectFactory->create()->setPath('checkout', ['_current' => true]);
            }

This will redirect users to the checkout page after adding product to cart. And removed success add to cart messages.

Note: Please make you own custom module for this don't overwrite the core file. Hope you know how to create your own module.