繁体   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