How to add custom Buy Now button on the product page of Magento

How to add custom Buy Now button on the product page of Magento

August 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add custom Buy Now button on the product page of Magento

[Blog Post]

How to Add a Custom “Buy Now” Button on Magento Product Pages

Adding a custom “Buy Now” button to your Magento product pages allows customers to bypass the cart and proceed directly to checkout. This feature is particularly useful for high-value items or when you want to encourage immediate purchases. Here’s a step-by-step guide on how to achieve this in Magento 1.x:

Step 1: Understanding the Requirements

The “Buy Now” functionality requires two key components:

  • A new button added to the product detail page
  • Logic to add the selected product to the cart and redirect to checkout

We’ll use a combination of template overrides, layout updates, and custom JavaScript to implement this feature. Let’s dive into the details:

Step 2: Override the Product View Template

First, you need to override the catalog/product/view.phtml template located in your theme directory (app/design/frontend/rwd/default/template/catalog/product/view.phtml). Make a copy of this file and save it as custom_view.phtml.

Now, open the custom_view.phtml file and locate the line containing the “Add to Cart” button. In Magento 1.x, this is usually around line 270:

<button type="button" class="btn-cart" onclick="productAddToCartForm.submit(this)">
    <span><span><?php echo $this->__('Add to Cart') ?></span></span>
</button>

Replace the entire button section with the following code, which includes both “Add to Cart” and “Buy Now” buttons:

<script type="text/javascript">
    var buyNowUrl = '<?php echo $this->getUrl('checkout/cart/buy'); ?>';
</script>

<button type="button" class="btn-cart add-to-cart-button" onclick="productAddToCartForm.submit(this)">
    <span><span><?php echo $this->__('Add to Cart') ?></span></span>
</button>

<button type="button" id="buy-now-button" data-product-id="<?php echo $this->getProduct()->getId() ?>" class="btn-buy-now">
    <span><span><?php echo $this->__('Buy Now') ?></span></span>
</button>

Step 3: Add the Buy Now JavaScript Functionality

Next, open your theme’s template/catalog/product/view.phtml file again and add the following JavaScript function at the end of the <body> section:

<script type="text/javascript">
//<![CDATA[
    require(['jquery'], function ($) {
        $('#buy-now-button').on('click', function () {
            var productId = $(this).data('product-id');
            $.post(buyNowUrl, {product: productId}, function (response) {
                window.location.href = '<?php echo $this->getUrl("checkout/cart") ?>';
            });
        });
    });
//]]>
</script>

Step 4: Update the Layout XML

To ensure the “Buy Now” button appears on the product detail page, update your theme’s catalog_product_view.xml file. Add the following code inside the <page> tag:

<reference name="content">
    <block type="catalog/product_view" name="product.info.addto" template="catalog/product/view/addto.phtml"/>
</reference>

Step 5: Create the Buy Now Controller

Finally, you need to create a new controller action in your theme’s app/code/local/YourCompany/Theme/controllers/CartController.php file:

class YourCompany_Theme_CartController extends Mage_Core_Controller_Front_Action
{
    public function buyAction()
    {
        $productId = $this->getRequest()->getParam('product');
        $product = Mage::getModel('catalog/product')->load($productId);

        if ($product) {
            $cart = Mage::getSingleton('checkout/cart');
            $quoteItem = $cart->getQuote();
            $quoteItem->addProduct($product, 1);
            $quoteItem->setCheckoutMethod('buy_now');
            $quoteItem->save();

            Mage_Core_Controller_Varien_Action::refreshBlockCache();

            if (!$this->_getSession()->getNoCartRedirect(true)) {
                $this->_redirect('checkout/cart');
            }
        }

        return;
    }
}

Don’t forget to replace YourCompany with the name of your Magento extension.

Conclusion

By following these steps, you can seamlessly add a custom “Buy Now” button to your Magento product detail pages. This feature improves the user experience by enabling customers to skip the cart and proceed directly to checkout, especially for high-value or impulse purchase items.

For more advanced customization options, consider exploring our guide on extending Magento with custom modules. Additionally, you can learn how to optimize your Magento store’s performance by following our best practices and tips at Laravel Company’s Magento blog.

Remember that proper testing is crucial before deploying these changes to a live environment. Always backup your data and test the functionality thoroughly on a staging site.

Happy coding!

Magento 1 Checkout Catalog Themes

Our network