1

在我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 &raquo;</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.


}
4

2 回答 2

1

消除

$('span#cart_count_info').load(Cart ({{ cart.item_count }})); 

并将其替换为下面的“UpdateCartDesc”功能(请参阅您的成功消息)

function updateCartDesc(data){
  var $cartLinkText = $('.cart_count_info').html(data.item_count);
}
于 2013-08-23T17:10:39.103 回答
0

当您单击添加到购物车时,将其作为与标题相关的标题,您将获得购物车中产品的更新数量。

只需将类添加cart-count到 div。您的结构应如下所示:

#pageheader > .cart-summary > .cart-count
于 2014-10-10T12:31:00.010 回答