在我Shopify
网站的产品详细信息页面上,当您单击“ Add To Cart
”时,该页面用于AJAX
执行其操作。但是有一个名为的变量cart.item_count
,除非页面刷新,否则它不会更新。
问题:如何让 AJAX 更新cart.item_count
该页面上的内容。
下面是 HTML 页面的代码:
<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 代码。底部有一行代码可以更新同一页面上的一些副本...如果我可以更新cart.item_count
已经在他的页面上找到的变量。
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.
}