使用布鲁克林主题在Shopify中的集合页面上显示所有颜色变体

时间:2016-05-15 09:28:21

标签: collections shopify product variants

嘿,我在shopify网站上使用布鲁克林主题。我有不同的产品将颜色变种。当我点击收藏页面时,我想将该产品的所有颜色变体显示为单独的产品。我昨晚从谷歌上搜索任何帮助。

1 个答案:

答案 0 :(得分:4)

请看下面的代码。

<ul class="colorlist"> 
 {% for option in product.options %}
 {% if option == 'Color' %}
 {% assign index = forloop.index0 %}
 {% assign colorlist = '' %}
 {% assign color = '' %}
 {% for variant in product.variants %}
 {% capture color %}
 {{ variant.options[index] }}
 {% endcapture %}

 {% unless colorlist contains color %}
  {% if variant.available %}

  <li id="{{ variant.id }}" title="{{ variant.inventory_quantity }} In Stock" class="instock"><a href="{{ product.url | within: collection }}?variant={{ variant.id }}" style="background:{{ color | downcase }}">{{ color | downcase }}</a></li>

  {% else %}

  <li id="{{ variant.id }}" title="Out of Stock"  class="outstock" >{{ color | downcase }}</li>

  {% endif %}

{% capture tempList %}
{{colorlist | append: color | append: " " }}
{% endcapture %}
{% assign colorlist = tempList %}
{% endunless %}
{% endfor %}
{% endif %}
{% endfor %}
</ul>

上面的代码可以在集合页面中显示产品的可用颜色。您可以采用相同的循环结构并显示整个产品网格,而不是仅显示变体名称。