The custom "Buy Now" button in shopify is not working correctly
The custom "Buy Now" button in shopify is not working correctly
How to create a custom "Buy Now" button in Shopify so that it goes to the checkout page and not to cart. The problem is that when the user for the first time goes to the site clicks the button, he is redirected to the main page of the site and not to the checkout page. When the button is pressed again, everything works.
https://mistdream.org/products/rainserenity-by-mistdream
enter image description here
My code
{% form 'product', product %}
<div class="item-bottom">
<div class="item-left">
<h4>quantity</h4>
<div class="item-count">
<div class="button-container">
<button type="button" id="decrease">-</button>
</div>
<input type="text" name="quantity" min="1" value="1" id="quantity" aria-label="Quantity of goods">
<div class="button-container">
<button type="button" id="increase">+</button>
</div>
</div>
</div>
</div>
<div class="item-price">
<h4>price</h4>
<p>{{ product.price | money }} <span>129.99$</span></p>
</div>
<select name="id" style="display: none;">
<option value="{{ product.variants[0].id }}">
{{ product.variants[0].title }}
</option>
</select>
<button class="button buynow-btn" title="Buy" onClick="instantBuy()">Buy now</button>
{% endform %}
function instantBuy(){
var $ = jQuery;
var formParams = $('form').serialize();
$.ajax({
url: "/cart",
type: "post",
data: formParams,
success: function(){
window.location.href = "/checkout";
},
error: function(){
}
})
}
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand