jquery - Shopify:动态显示购物车的 item_count

标签 jquery shopify rivets.js

我们在标题中显示了 cart.item_count。这对于大多数页面来说都很好。但在一个页面上,我们动态地将商品添加到购物车,并且希望更改反射(reflect)在标题的购物车摘要中。

我认为我们需要 RivetsJS 才能实现此功能;这样我们就有了一个单向绑定(bind)。我尝试了几种方法:

<span rv-text="cart.item_count"></span>

还有:

{ cart.item_count }

第一个显示为空,第二个显示花括号和 cart.item_count,字面上。

我做错了什么?我需要在某处执行 rivets.bind() 吗?

最佳答案

正如@Ronnie评论的那样,您应该使用 Shopify's Ajax API .

Stack Overflow 上还有很多其他问题和解答,其中包含如何执行此操作的示例,这里仅列出其中的一些:

For example :

$.ajax({
    type: 'GET',
    url: 'http://your-store.myshopify.com/cart.json',
    dataType: 'json',
    success: function(data) { 
        var item_count = data.item_count;

        // Update the item count in your header here...
    }
});

您还可以使用 Shopify's jQuery wrapper library 。请参阅Shopify.getCart() :

// ---------------------------------------------------------
// GET cart.js returns the cart in JSON.
// ---------------------------------------------------------
Shopify.getCart = function(callback) {
  jQuery.getJSON('/cart.js', function (cart, textStatus) {
    if ((typeof callback) === 'function') {
      callback(cart);
    }
    else {
      Shopify.onCartUpdate(cart);
    }
  });
};

关于jquery - Shopify:动态显示购物车的 item_count,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29243567/

相关文章:

javascript - 如何使用js从选定的li中获取文本值并将其传递给另一个li中的输入

JavaScript - 需要一种方法来设置元素的 OuterHeight

jquery - JavaScript 生成的文本消失(Cufon 和 Clone)

shopify - Shopify 服务器上的 Assets 缓存时间

Shopify 应用程序请求新范围的许可

css - Shopify 视网膜显示图像 Sprite

javascript - 使用通配符选择类而不是确切的类名

javascript - rivets.js 试图让自定义适配器工作

javascript - 如何使用 rv-each 构造动态 HTML

javascript - rivets.js:使用 init View 中的数据预填充模型