Magento – Magento 1.9 How to add buy now button in product page

addtocartmagento-1.9product

I am using magento 1.9, I want to add BUY NOW button on product details page, Buy now button redirect to checkout page. I tried many solution already solved in this forum, But all the solutions are BUY NOW button redirect to cart page only. Here I was list outed already tried solutions are,

  1. Solution1

  2. Solution2

  3. Solution3
  4. Solution4
  5. Solution5

Best Answer

First, change Add to Cart Button to Buy Now.

Then add a hidden input field to the #product_addtocart_form form. Name should be return_url and its value should be <?php echo Mage::getUrl('checkout/onepage')?>":

<input type="hidden" value="<?php echo Mage::getUrl('checkout/onepage')?>" name="return_url" />

If you want both Add to Cart and Buy now, add the buy now button to addtoCart.phtml

<button type="button" 
        title="<?php echo $buttonTitle ?>"
        class="button btn-cart" 
        onclick="productBuyNowForm.submit(this)">
    <span><span>Buynow</span></span>
</button>

On that buttons onlick event, a custom VarienForm is used, productBuyNowForm.submit(this), which will submit this form and redirect to checkout onepage.

To define this custom form, add the following JavaScript code at view.phtml

var productBuyNowForm = new VarienForm('product_addtocart_form');
productBuyNowForm.submit = function (button, url) {
    if (this.validator.validate()) {
        var form = this.form;
        var oldUrl = form.action;

        if (url) {
            form.action = url;
        }

        /* add return Url */
        var inputreturn= document.createElement("input");
        inputreturn.type = "hidden";
        inputreturn.name = "return_url";
        inputreturn.value = "<?php echo Mage::getUrl('checkout/onepage')?>";
        document.getElementById('product_addtocart_form').appendChild(inputreturn);
        /* add return Url */
        // Append a line break 
        var e = null;
        try {
            this.form.submit();
        } catch (e) {
        }
        this.form.action = oldUrl;
        if (e) {
            throw e;
        }

        if (button && button != 'undefined') {
            button.disabled = true;
        }
    }
}.bind(productBuyNowForm);