【问题标题】:Pulling Products From Multiple Collections - Shopify Liquid从多个系列中提取产品 - Shopify Liquid
【发布时间】:2021-06-11 15:02:39
【问题描述】:

我在尝试从与产品页面上的产品关联的多个集合中提取产品时遇到问题。我编写了以下代码:

{% for product in product.collections limit: 6 %}
  ** Products **
{% endfor %}

但是,这只会提取与产品相关联的所有集合列表,而不是这些集合中的产品。然后我尝试了以下方法:

{% for product in collections[product.collections].products limit: 6 %}
  ** Products **
{% endfor %}

返回“液体错误:预期句柄是字符串但得到数组”错误消息。

我不确定我应该如何处理这个问题。有人知道我哪里出错了吗?

【问题讨论】:

    标签: shopify liquid


    【解决方案1】:

    你要禁食。 Collections 是一个数组,因此您必须先定义您需要的集合,然后再定义它的产品。

    因此,例如,如果您只想要当前产品的第一个集合中的产品,您可以尝试这样来访问 product.collections 数组中的集合本身:

     {% for product in product.collections.first.products %}
         Do something with product object.
     {% endfor %}
    

    然后,关于您要实现的目标,首先遍历 product.collections,然后遍历每个集合产品。像这样:

    {% for collection in product.collections %}
        {% for product in collection.products limit:6 %}
            Do something
       {% endfor %}
    {% endfor %}
    

    请注意,双循环是贪婪的,可能会影响页面加载时间。因此,如果产品属于很多集合,您可能还需要限制主循环以避免不必要的负载。

    【讨论】:

      猜你喜欢
      • 2019-07-12
      • 2023-02-26
      • 2017-09-14
      • 2023-02-08
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多