3

I am new to magento, using Magento 1.9. with custom theme. I succesfully created buy now button on addtocart.phtml by the following codes:

<button type="button" title="Buy Now" class="button btn-cart" onclick="setcheckoutLocation('<?php echo $this->getAddToCartUrl($_product) ?>')">
    <span><span>Buy Now</span></span>
</button>
<?php echo $this->getChildHtml('', true, true) ?>

But am confused in where to paste below javascripts on my view.phtml? can any one say exactly next to which function or step number, I should paste these scripts?

<script>
    jQuery.ajax({
        type: "GET",
        url: location,
        success: function (data) {
            window.location.href = "http://www.your-site.com/checkout/onepage/";
        }
    });
</script>

I planned to have 2 buttons,without disturbing eachothers:When someone clicks:

  1. Buynow button which should redirect to checkout/onepage
  2. Add to cart button should do its default checkout/cart

Thanks in advance. Any suggestions are very welcome! :)

2
  • 1
    Can you please explain what you are trying to do Aug 3, 2015 at 11:19
  • I need 'Buynow' button along with Add to cart button for each product Aug 3, 2015 at 13:24

2 Answers 2

3

The add to cart request is a GET request that among other params, it can contain a uenc param with can hold a base64 encoded return URL. The user is redirected to that URL upon return from add to cart.

You can simply create a new button that you add anywhere on the page, that on click will do a GET request to a URL like https://example.com/checkout/cart/add/product/XYZ/uenc/aHR0cHM6Ly9leGFtcGxlLmNvbS9jaGVja291dC9vbmVwYWdlCg==/ where aHR0cHM6Ly9leGFtcGxlLmNvbS9jaGVja291dC9vbmVwYWdlCg== is base64 encoded https://example.com/checkout/onepage

2
  • What code did you use?
    – Adi
    Aug 3, 2015 at 16:10
  • edited my question,above is my codes Aug 14, 2015 at 12:16
0
<script>
jQuery.ajax({
    type:”GET”,
    url:location,
    success:function(data){
        window.location.href = “http://www.your-site.com/checkout/onepage/”;
    }
});
</script>

Put this code in your view.phtml after the closing tag.

1
  • This doesn't make sense. It makes a random AJAX request right after you open the page and then redirects to the checkout. Jun 28, 2016 at 6:19

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.