2

I am new to Magento, using Magento 1.9. with a custom theme. I successfully 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 javascript on my view.phtml? can anyone 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 each other: When someone clicks

Buy Now button which should redirect to checkout/onepage

Add to cart button should do its default checkout/cart

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

  • Use that script in addtocart.phtml itself – MeenakshiSundaram R Aug 7 '15 at 2:55
  • @ Meenakshi Sundaram : As you said,when buynow button is clicked its simply adding product to cart but not redirecting to checkout/onepage – Rajesh Elangovan Aug 7 '15 at 13:27
  • Can you provide the link from where you found that script. It needs to intercept between your button click action to work properly, – Prateek Aug 7 '15 at 14:44
  • Check your product is added in the cart. Then check your ajax response in browser console. – MeenakshiSundaram R Aug 7 '15 at 15:53
  • @Prateek I get those codes from http://blog.fusedwebspace.co.uk/adding-buy-now-button-to-magento-1-9-that-skips-the-shopping-cart/ link – Rajesh Elangovan Aug 8 '15 at 9:19
4

Kindly open app/design/frontend/package/theme/template/catalog/product/view.phtml

Add below line into <form> Tag

<input type="hidden" name="buy_now" id="buy_now" value="" />

Put below button at anywhere where you want

<button type="button" onclick="jQuery('#buy_now').val('buy_now');productAddToCartForm.submit(this)" class="btn btn-block btn-express-buynow"><i class="cart-icon-white m-r-sm v-middle"></i>Buy Now</button>

Now open app/code/core/Mage/Checkout/controllers/CartController.php

First of all move this file into the local folder and change the code as below instructions

Put below line in _goBack() function at very first

$buy_now = $this->getRequest()->getParam('buy_now');

FInd the line $this->getResponse()->setRedirect($backUrl); and replace this line as below code

if (!empty($buy_now)) {
    $this->_redirect('onepagecheckout'); // If you are using onepagecheckout or use this $this->_redirect('checkout/onepage/')
}else{
    $this->getResponse()->setRedirect($backUrl);
}

Code Taken from this link http://chandreshrana.blogspot.in/2016/01/how-to-add-buynow-button-on-product.html

3

Try this solution.

In your app\design\frontend\default[your theme]\template\catalog\product\view\addtocart.phtml add the below code after add to cart button.

<button type="button" title="<?php echo $this->__('Check out') ?>" class="button btn-cart" onclick="setcheckoutLocation('<?php echo $this->getAddToCartUrl($_product) ?>','<?php echo Mage::getUrl('checkout/url') ?>')"><span><span><?php echo $this->__('Buy Now') ?></span></span></button>
 <script>
 function setcheckoutLocation(location,chkout)
 {
  jQuery.ajax({
                type:"GET",
                url:location,
                success:function(data){
                     window.location.href = chkout;
                }
             });
}
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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