Trying to ajax add to cart but each submit returns a
https://www.website.com/commerce/cart/updateCart 404 (Not Found)
I have this in my twig template:
{% for book in craft.products().type('books').limit(8).all() %}
<a href="{{ book.url }}" class="book">
{% for image in book.productImages.limit(1).all() %}
<div class="b-img">
<img src="{{ image.getUrl() }}" />
<div class="hover-cart">
<div class="success"><i class="fas fa-check-circle"></i></div>
{% set variant = book.defaultVariant %}
<form method="POST" class="add-to-cart">
{{ csrfInput() }}
<input type="hidden" name="purchasableId" value="{{ variant.id }}">
<input type="number" name="qty" value="1">
<button type="submit"><i class="fas fa-shopping-cart"></i></button>
</form>
</div>
</div>
{% endfor %}
<h3>{{ book.title }}</h3>
<span class="price">{{ book.defaultVariant.price|currency }}</span>
</a>
{% endfor %}
And this as my javascript:
$('.add-to-cart').on('submit', function(e) {
console.log('add to cart clicked');
e.preventDefault();
var form_data = $(this).serializeArray();
console.log(form_data);
$.ajax({
type: 'post',
url: 'commerce/cart/updateCart',
data: form_data,
success: function(response) {
if (response.success) {
console.log('success');
console.log(response.success);
$(this).hide();
$(this).siblings('.success').show();
$(this).parents('.hover-cart').fadeOut();
}
else {
console.log('error');
console.log(response.success);
alert("We're sorry, but the item couldn't be added to your cart. Please try again. If the issue persists, please send an email to [email protected]");
}
}
});
});