I have followed the steps suggested in

How to add "Buy Now" button in Magento 2.1?

by Aaron Allen. But I couldn't see the buy now button. Could anyone guide me what I am missing ?

Here is a module that adds a 'Buy Now' button that takes customer directly to checkout with the choosen product in cart.

Module Directory:

| registration.php | +---Controller | \---Cart | Add.php | +---etc | | module.xml | | | \---frontend | routes.xml | sections.xml | \---view \---frontend +---layout | catalog_product_view.xml | +---templates | buynow.phtml | \---web \---js buy-now.js


<?php namespace AAllen\BuyNow\Controller\Cart; class Add extends \Magento\Checkout\Controller\Cart\Add { /** * Add product to shopping cart action * * @return \Magento\Framework\Controller\Result\Redirect * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute() { if (!$this->_formKeyValidator->validate($this->getRequest())) { return $this->resultRedirectFactory->create()->setPath('*/*/'); } $params = $this->getRequest()->getParams(); try { if (isset($params['qty'])) { $filter = new \Zend_Filter_LocalizedToNormalized( ['locale' => $this->_objectManager->get('Magento\Framework\Locale\ResolverInterface')->getLocale()] ); $params['qty'] = $filter->filter($params['qty']); } $product = $this->_initProduct(); $related = $this->getRequest()->getParam('related_product'); /** * Check product availability */ if (!$product) { return $this->goBack(); } // empty the cart. $this->cart->truncate(); $this->cart->addProduct($product, $params); if (!empty($related)) { $this->cart->addProductsByIds(explode(',', $related)); } $this->cart->save(); /** * @todo remove wishlist observer \Magento\Wishlist\Observer\AddToCart */ $this->_eventManager->dispatch( 'checkout_cart_add_product_complete', ['product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse()] ); if (!$this->_checkoutSession->getNoCartRedirect(true)) { // redirect to checkout page return $this->goBack('/checkout/', $product); } } catch (\Magento\Framework\Exception\LocalizedException $e) { if ($this->_checkoutSession->getUseNotice(true)) { $this->messageManager->addNotice( $this->_objectManager->get('Magento\Framework\Escaper')->escapeHtml($e->getMessage()) ); } else { $messages = array_unique(explode("\n", $e->getMessage())); foreach ($messages as $message) { $this->messageManager->addError( $this->_objectManager->get('Magento\Framework\Escaper')->escapeHtml($message) ); } } $url = $this->_checkoutSession->getRedirectUrl(true); if (!$url) { $cartUrl = $this->_objectManager->get('Magento\Checkout\Helper\Cart')->getCartUrl(); $url = $this->_redirect->getRedirectUrl($cartUrl); } return $this->goBack($url); } catch (\Exception $e) { $this->messageManager->addException($e, __('We can\'t add this item to your shopping cart right now.')); $this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e); return $this->goBack(); } } }


<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> <router id="standard"> <route id="buynow" frontName="buynow"> <module name="AAllen_BuyNow"/> </route> </router> </config>


<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd"> <action name="buynow/cart/add"> <section name="cart"/> </action> </config>


<?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> <referenceBlock name="product.info.addtocart"> <block class="Magento\Framework\View\Element\Template" template="AAllen_BuyNow::buynow.phtml"/> </referenceBlock> <referenceBlock name="product.info.addtocart.additional"> <block class="Magento\Framework\View\Element\Template" template="AAllen_BuyNow::buynow.phtml"/> </referenceBlock> </body> </page>


<button type="submit" title="<?php /* @escapeNotVerified */ echo __('Buy Now') ?>" id="buy-now" class="action buynow primary" data-mage-init=' { "AAllen_BuyNow/js/buy-now": { "form": "#product_addtocart_form" } } '> <span><?php /* @escapeNotVerified */ echo __('Buy Now') ?></span> </button>


define([ 'jquery' ], function ($) { "use strict"; return function (config, element) { $(element).click(function () { var form = $(config.form); // change form action var baseUrl = form.attr('action'), buyNowUrl = baseUrl.replace('checkout/cart/add', 'buynow/cart/add'); form.attr('action', buyNowUrl); form.trigger('submit'); // set form action back form.attr('action', baseUrl); return false; }); } });

It works by creating a modified version of the controller used to add a product to the cart. When 'Buy Now' is clicked, the 'add product' form's action is switched to the custom controller which then redirects to the checkout page if the item is successfully added to cart.

closed as off-topic by Jai, Manoj Deswal, Piyush, Priyank, 7ochem Apr 6 '18 at 9:08

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced OR seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Jai, Manoj Deswal, Piyush, Priyank, 7ochem
If this question can be reworded to fit the rules in the help center, please edit the question.


I developed module to add buy now button in magento 2.

Download here: https://github.com/prince108/Magento2-Buynow

enter image description here

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