【问题标题】:Shopify: how to add a class depending on collectionShopify:如何根据收藏添加类
【发布时间】:2015-05-13 11:32:44
【问题描述】:

他们希望在客户主页上列出“所有产品”,并在底部添加更多按钮。

有四个类别,每个类别都有不同的关联颜色。我想按日期列出的订单提取所有产品,并根据类别对产品应用一个类。

目前我有

{% assign collection = collections.all %}
{% for product in collection.products %}
{% if collection.handle == 'clothing' %}
  <div class="theme-red overview-item">
{% elsif collection.handle == 'pictures' %}
  <div class="theme-green overview-item">
{% elsif collection.handle == 'posters' %}
  <div class="theme-blue overview-item">
{% elsif collection.handle == 'other' %}
  <div class="theme-beige overview-item">
{% else %}
  <div class="theme-none overview-item">
{% endif %}  
  ...
  </div>
{% endfor %}

这目前被选为无主题,那是因为我正在调用 collections.all?

如何获得所有显示的产品而不管集合如何,然后按照上面的 if/else 语句应用执行某些操作。

任何想法,我都试过看看product.collections,collection.all_types 有没有快乐。

【问题讨论】:

    标签: collections filtering shopify liquid


    【解决方案1】:

    您收到theme-none 的原因是因为您的 if 语句检查集合的句柄是否为“服装”、“图片”等,但您已将集合设置为 collections.all,因此它永远不会匹配任何这些条件。

    您想要做的是检查当前产品是否在这些集合之一中。例如:

    {% assign collection = collections.all %}
    
    {% for product in collection.products %}
      {% for c in product.collections %}
        {% if c.handle == 'clothing' %}
          {% assign theme = "red" %}
        {% elsif c.handle == 'pictures' %}
          {% assign theme = "green" %}
        {% elsif c.handle == 'posters' %}
          {% assign theme = "blue" %}
        {% elsif c.handle == 'other' %}
          {% assign theme = "beige" %}
        {% else %}
          {% assign theme = "none" %}
        {% endif %}  
      {% endfor %}
    
      <div class="theme-{{ theme }} overview-item">
        ...
      </div>
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 2019-10-07
      • 2018-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多