简体   繁体   English

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

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

I'm trying to make a ajaxed shopping cart in Shopify. 我正在尝试在Shopify中制作一个购物车。 I have made the product page add an item to cart without refreshing or going to the cart page. 我已经使产品页面在不刷新或未进入购物车页面的情况下将商品添加到购物车。 To see cart, you can click on an input or link with .cart-show, so I added this to the 'add to cart' input: 要查看购物车,您可以单击输入或带有.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" />

What that does, is simply add the item to cart and display the cart div #menu. 要做的只是将商品添加到购物车并显示购物车div #menu。 Of course cart doesn't get updated when a new item is added, and the question is: how can I do this? 当然,添加新商品时购物车不会更新,问题是:我该怎么做? How can I make the cart div #cart update each time the input is clicked? 每次单击输入时,如何使购物车div #cart更新? And how do I update the cart.item_count in the div #cart-total-items when a new product is added? 添加新产品后,如何在div#cart-total-items中更新cart.item_count? I've used more than a day to figure this out and still haven't gotten anywhere. 我已经花了超过一天的时间来解决这个问题,但仍未解决。 I have even tried asking on the Shopify Forum, but it is simply dead and no help to get over there. 我什至尝试在Shopify论坛上提问,但是它已经死了,没有帮助。

The 'Shopify.onItemAdded' script for when an new item is added is as follows: 添加新商品时的“ 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 cart.item_count

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

Any ideas? 有任何想法吗?

This might not be exactly what you need but it may help. 这可能不完全是您所需要的,但可能会有所帮助。 I have made and implemented an ajax cart on a shopify site I develop for. 我已经在要开发的shopify网站上制作并实现了一个ajax购物车。 I use jquery & JSON to do this type of stuff and here's what I do: 我使用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