【问题标题】:Shopify' s all_products[ ] does only accept titleShopify 的 all_products[ ] 只接受标题
【发布时间】:2021-01-26 15:57:50
【问题描述】:

对于客户,我正在构建一种方法来选择产品页面的推荐产品部分中显示的产品。为了实现这一点,我考虑将我想展示的产品的把手插入产品的标签部分。

所以我可以遍历产品中的所有标签,然后为每个标签获取产品对象并显示它。问题是这段代码似乎不起作用:

{% for tag in tags %}
  {% for single_product in all_products[tag] %}
      {{ single_product }}
  {% endfor %}
{% endfor %}

此代码确实有效:

{% for tag in tags %}
  {% for single_product in all_products[tag].title %} <---- Added .title
      {{ single_product }}
  {% endfor %}
{% endfor %}

遗憾的是,我需要整个产品对象来显示它,而不仅仅是产品标题。如何在 shopify 中实现这一点?

附加信息:此代码位于framework--product-recommendations.liquid 文件中

【问题讨论】:

  • 我认为您需要查看collection.productsread more
  • @Onkar 谢谢!这非常有效!
  • 很高兴知道它对您有帮助。

标签: shopify liquid


【解决方案1】:

解决方案是使用集合而不是 all_product。我创建了一个包含所有产品的集合,并且能够完美地循环它。

我的代码现在看起来像这样:

          {% for tag in tags %}
            {% for single_product in all_the_products %}
                {% if tag == single_product.handle %}
                    <div class="product-recommendations--item">
                    {%
                    render 'framework--product--item',
                    product: single_product,
                    view: 'grid'
                    %}  
                    </div>
                {% endif %}
            {% endfor %}
          {% endfor %}

all_the_products 分配给包含所有产品的集合。

【讨论】:

    猜你喜欢
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    相关资源
    最近更新 更多