On the product detail page of my Shopify
site when you click "Add To Cart
" the page uses AJAX
to do its thing. BUT there is a variable named cart.item_count
that does not update unless the page refreshes.
Question: How to get AJAX to update the cart.item_count
on that page.
Below is the code for the HTML page:
<div id="mini-cart" >
<span class="cart_count_info">Cart ({{ cart.item_count }})</span>//THIS SPAN IS WHAT NEEDS TO BE UPDATED
</div>
<p id="add-to-cart-msg"></p> //THIS "P" IS UPDATED BY THE AJAX WITH MESSAGING SEE BELOW JAVASCRIPT
<form action="/cart/add" method="post" class="variants" id="product-actions" enctype="multipart/form-data">
//TOOK OUT SOME OF THE FORM DATA THAT IS NOT NEED FOR THIS EXAMPLE
</form>
JavaScript Code. There is a line of code at the bottom that updates some copy on the same page... If I can update my cart.item_count
variable that is already found on he page.
function addToCartSuccess (jqXHR, textStatus, errorThrown){
$.ajax({
type: 'GET',
url: '/cart.js',
async: false,
cache: false,
dataType: 'json',
success: updateCartDesc
});
$('#add-to-cart-msg').hide().addClass('success').html('Item added to cart! <a href="/cart" title="view cart">View cart and check out »</a>').fadeIn();
$('span#cart_count_info').load(Cart ({{ cart.item_count }}));
//MAYBE HERE IS WHERE I CAN ADD MY CODE TO UPDATE THE VARIABLE I NEED. MY CODE IS JUST A GUESS SINCE ID DON'T DO MUCH JAVASCRIPT PROGRAMMING.
}