You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 13:57:03