簡體   English   中英

Shopify.onItem已添加更新#cart和cart.item.count

[英]Shopify.onItemAdded update #cart and cart.item.count

我正在嘗試在Shopify中制作一個購物車。 我已經使產品頁面在不刷新或未進入購物車頁面的情況下將商品添加到購物車。 要查看購物車,您可以單擊輸入或帶有.cart-show的鏈接,因此我將其添加到“添加到購物車”輸入中:

<input style="margin-top: 40px; width: 50%;" type="submit" value="Add to cart" class="button cart-show" onclick=" Shopify.addItemFromForm('add-item-form'); return false" />

要做的只是將商品添加到購物車並顯示購物車div #menu。 當然,添加新商品時購物車不會更新,問題是:我該怎么做? 每次單擊輸入時,如何使購物車div #cart更新? 添加新產品后,如何在div#cart-total-items中更新cart.item_count? 我已經花了超過一天的時間來解決這個問題,但仍未解決。 我什至嘗試在Shopify論壇上提問,但是它已經死了,沒有幫助。

添加新商品時的“ Shopify.onItemAdded”腳本如下:

 Shopify.onItemAdded = function(line_item) {
    Shopify.getCart();

    refresh #menu script here

    refresh cart.item.count script  in theme.liquid could be something like this, but it doesn't work: 

    $('#cart-total-items').update(cart.item_count);
  };

cart.item_count

<span style="" id="cart-total-items"><a class="cart-show">Bag ({{ cart.item_count }})</a></span>

有任何想法嗎?

這可能不完全是您所需要的,但可能會有所幫助。 我已經在要開發的shopify網站上制作並實現了一個ajax購物車。 我使用jquery和JSON來完成此類工作,這是我的工作方式:

jQuery.getJSON('/cart.js', function (cart, textStatus) {
  //the cart info is in the callback. the item_count is available as part of the callback
  //below I set my cartcount div with the current cart item_count
  var carttext=cart.item_count;
  jQuery("#cartcount").text(carttext);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM