2

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

1 Answer 1

9

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);  
7
  • Sir thanks for your answer, I tried your solution. First i was hide my add to cart button and create new Buy now button in addtocart.phtml then added needed code from your solution. Output it display a button with out any action. Again i was enable add to cart button then i will check, Buy now button takes no action, Add to cart button goes to check out page, but i can't enter any data in one page checkout. All the sections are empty or can't show the content area just show the heading only. Commented Jun 29, 2016 at 7:21
  • please check now,it was a syntax issue
    – Amit Bera
    Commented Aug 3, 2016 at 5:29
  • @AmitBera, Amit. Great bro. i just tried your answer. and it is working properly. +1 for your answer from me. But, when i clicked on buynow button , i am getting the error on popup, then i clciked on ok then its going to checkout. please check once. Commented May 15, 2018 at 5:42
  • @AmitBera, i tried your answer. its working perfectly. but, when i clicked on buynow button it shows one form key error on popup. plz help me to clear that. lh3.googleusercontent.com/… Commented May 30, 2018 at 11:28
  • Really strange .. may be you need page refresh.Seems issue session
    – Amit Bera
    Commented May 30, 2018 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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