I am using the Shopify AJAX API (https://help.shopify.com/en/themes/development/getting-started/using-ajax-api) and trying to get the page to send the user to the cart page after click. By default (I believe) the API keeps you on the same page.
Here is my code:
<script>
$('#productAdd').on('click', function(){
Shopify.addItemFromForm('productForm');
window.alert = function() {};
});
</script>
Anyone know how to make this redirect to the cart page after click? Thanks!
Found a solution to this. It's a bit of a different approach, but it's working now and does what it should.
New code:
<a id="productAdd"
onClick="
let id = {{ product.selected_or_first_available_variant.id }};
Shopify.addItem(id, 1);
window.alert = function() {};
setTimeout(function(){ window.location.href = '/cart';},1000);
return false"
href="#" class="btn btn-primary section-cta">{{ section.settings.url_text }}</a>
Quick explanation of some of the code:
This is a bit hacky, so I'm open to any suggestions for improvement. Thanks!
The approach would be to either:
im having a similar issue
this is the button code. i cant seem to get it to work.
<button class="button btn-cart {% if sold_out %} btn disabled {% endif %}" type="button" data-toggle="tooltip" data-loading-text="{{ 'products.product.loading' | t }}" title="{{ 'products.product.add_to_cart' | t }}" {% if sold_out == false %}onclick="{% if product.variants.first.available %}cart.add('{{ product.variants.first.id }}'){% else %}window.top.location.href='{{product.url}}'{% endif %};"{% endif %}>
<span>{% if sold_out == false %}{{ 'products.product.add_to_cart' | t }}{% else %} {{ 'products.product.sold_out' | t }}{% endif %}</span>
</button>
Im trying to have it add the product and then redirect to checkout vs just placing it into the cart
User | Count |
---|---|
13 | |
12 | |
10 | |
8 | |
8 |