2

Is there a way to add a 'Add to Cart and Checkout' button to the product page, so that the product is added to the cart and then takes the user straight to the checkout?

1
  • You mean you want to redirect to Checkout page immediately after adding a product to cart successfully? – Khoa TruongDinh Apr 23 '16 at 7:02
3

By default, Magento used Ajax in product page. When pressing the add to cart button, the data will be sent to vendor/magento/module-checkout/Controller/Cart/Add.php. I have two suggestions for you.

Navigate to vendor/magento/module-catalog/view/frontend/web/js/catalog-add-to-cart.js, we can see ajaxSubmit function which will handle the response data from the server. It will check the back Url that it will redirect to if this url exists.

    success: function(res) {
                    ......
             //Line 70 ~ 80       
             if (res.backUrl) {
                        window.location = res.backUrl;
                        return;
                    }

And then, go to the cart controller vendor/magento/module-checkout/Controller/Cart/Add.php. In execute() method, if a product is added to cart successfully, Magento will call a goBack method to resolve the response data.

protected function goBack($backUrl = null, $product = null)
    {
        if (!$this->getRequest()->isAjax()) {
            return parent::_goBack($backUrl);
        }

        $result = [];

        if ($backUrl || $backUrl = $this->getBackUrl()) {
            $result['backUrl'] = $backUrl;
        } else {
            if ($product && !$product->getIsSalable()) {
                $result['product'] = [
                    'statusText' => __('Out of stock')
                ];
            }
        }

        $this->getResponse()->representJson(
            $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($result)
        );
    }

As can we see, the back url will get the url value from $backUrl argument or $this->getBackUrl(). If we set one, Magento will redirect to it. So, basically, we have two ways to redirect to checkout page.

1. We may rewrite the execute() method and add a back url(this way not recommend). For example:

 #vendor/magento/module-checkout/Controller/Cart/Add.php

     if (!$this->_checkoutSession->getNoCartRedirect(true)) {
            if (!$this->cart->getQuote()->getHasError()) {
                $message = __(
                    'You added %1 to your shopping cart.',
                    $product->getName()
                );
                $this->messageManager->addSuccessMessage($message);
            }
            //Back Url here
            $backUrl = 'checkout';
            //return $this->goBack(null, $product); => Old code
            return $this->goBack($backUrl, $product); // New code
        }

2. We add a return url in the product page template, should rewrite also (easier way). For example, add a return url inside the product view form.

#vendor/magento/module-catalog/view/frontend/templates/product/view/form.phtml

<form ...>
   ...
   <input type="hidden" name="return_url" value="<?php echo $this->getUrl('checkout');?>">
   ...
</form>

More advanced, Magento has a config STORES > Configuration > SALES > Checkout > Shopping Cart > After Adding a Product Redirect to Shopping Cart. We can follow the logic of this option to build our own redirect.

0

that's an ajax call, triggered by

jQuery.mage.catalogAddToCart.ajaxSubmit

so you could extend the widget ( here how: https://learn.jquery.com/jquery-ui/widget-factory/extending-widgets/ ) adding your own function ajaxSubmitAndGoToCheckout or overwriting the exisiting one if you want to leave the phtml untouched.

The idea is to make a js redirect to checkout on request success.

Obviously, with the second option, ALL the request would redirect to checkout.

Michele

1
  • looking at the response callback there's a if (res.backUrl) { window.location = res.backUrl; return; } maybe you could add this response attribute to redirect to checkout – Michele Ongaro Apr 23 '16 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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