如何为cart-summary.liquid添加product scope以实现产品集合遍历?
解决cart-summary.liquid中无法访问product.collections的问题
你猜的没错,cart-summary.liquid确实没法直接访问product作用域——这个代码片段的上下文是购物车整体,不是单个产品。要实现需求,得先从购物车项(cart item)里拿到对应的产品对象。
修正后的代码如下:
{% for item in cart.items %} {% for c in item.product.collections %} {% if c.handle == "myCollection" %} <div>Custom product message here.</div> {% endif %} {% endfor %} {% endfor %}
补充说明
- 代码先遍历购物车里的每一项
cart.items,每个购物车项的item.product就是对应的产品对象,这样就能正常访问它的collections了 - 如果担心同个集合的产品重复显示提示,可以加个判断逻辑,比如用变量标记已经显示过的集合,避免重复输出
内容的提问来源于stack exchange,提问作者mcottell
相关产品推荐
相关产品推荐

