I need help to add "Buy Now" button on the product detail page. Thanks.

closed as unclear what you're asking by Raphael at Digital Pianism, 7ochem, Qaisar Satti, Amit Bera, Rakesh Jesadiya Aug 30 '16 at 5:29

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


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
|   \---Cart
|           Add.php
|   |   module.xml
|   |   
|   \---frontend
|           routes.xml
|           sections.xml
        |       catalog_product_view.xml
        |       buynow.phtml



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->addProduct($product, $params);
            if (!empty($related)) {
                $this->cart->addProductsByIds(explode(',', $related));


             * @todo remove wishlist observer \Magento\Wishlist\Observer\AddToCart
                ['product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse()]

            if (!$this->_checkoutSession->getNoCartRedirect(true)) {
                $baseUrl = $this->_objectManager->get('\Magento\Store\Model\StoreManagerInterface')
                // redirect to checkout page
                return $this->goBack($baseUrl.'checkout/', $product);
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            if ($this->_checkoutSession->getUseNotice(true)) {
            } else {
                $messages = array_unique(explode("\n", $e->getMessage()));
                foreach ($messages as $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.'));
            return $this->goBack();


<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <router id="standard">
        <route id="buynow" frontName="buynow">
            <module name="AAllen_BuyNow"/>


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <action name="buynow/cart/add">
        <section name="cart"/>


<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        <referenceBlock name="product.info.addtocart">

            <block class="Magento\Framework\View\Element\Template" template="AAllen_BuyNow::buynow.phtml"/>
        <referenceBlock name="product.info.addtocart.additional">

            <block class="Magento\Framework\View\Element\Template" template="AAllen_BuyNow::buynow.phtml"/>


<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>


], 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);


            // 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.

  • Hey , After implement the module at magento 2 out of the box, i can see the button "Buy Now" and the buy-now.js is working after check with debug but i cant reach the BuyNow/Controller/Add.php . i try to change the xml files but still i get the core controller instead the custom one. – Goldy Dec 6 '16 at 7:51
  • 1
    Hi, I made a few updates to buy-now.js and Add.php. Make those changes and try it again. – Aaron Allen Dec 6 '16 at 9:30
  • PERFECT , thank you! . By the way , where you get the information about the form action on the js? and why there is xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd" on --sections.xml-- ? – Goldy Dec 7 '16 at 7:35
  • I have created module as per above layout & installed successfully, but buynow button is not showing on product page, can you please assist me for this. – Tush Jan 4 '17 at 13:00
  • Hi i follow this it's working fine but on click buy now button Add to cart change to adding then it redirect to checkout page magento.stackexchange.com/questions/269987/… – Magento 2 Apr 13 at 17:10

I am assuming that you know the basic module creation. Follow below steps:

  1. Create a new module layout.


<?xml version="1.0"?>
    <referenceContainer name="product.info.social">
        <block class="Vendor\Module\Block\Product\View\Extra"
  1. Create a template file


<h3><?php echo 'Custom Button'; ?></h3>


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