在Shopify的液体逻辑

时间:2015-06-25 04:31:01

标签: shopify liquid variants

created a way在任何给定的收藏页面上按尺寸过滤Shopify商店中的商品。但是,它只适用于尺寸是唯一的变体。

对于我的商店来说这很好,但是因为我已经开源过滤器,所以即使有多个变种,我也希望它只能用于一个变体。

问题似乎是像{% for variant in product.variants %}这样的行仅适用于一个当前变体(例如大小),但我似乎无法传递大小参数,例如{% for variant in product.variants.size %}

我甚至尝试过the recommended variant.option1(即{% for variant in product.variants.size.option %}),但无济于事。

即使产品有多种变体,是否有某种方法只输出大小变体数组?

Further context.

另外,发现这个similar question,但它不足以为我的用例提供解决方案。

2 个答案:

答案 0 :(得分:2)

{% capture sizes %}{% endcapture %}
{% for variant in product.variants %}
    {% if variant.option1 == 'Size' %}{% capture sizes %}{{ sizes }},{{ variant.option1 }}{% endcapture %}{% endif %}
    {% if variant.option2 == 'Size' %}{% capture sizes %}{{ sizes }},{{ variant.option2 }}{% endcapture %}{% endif %}
    {% if variant.option3 == 'Size' %}{% capture sizes %}{{ sizes }},{{ variant.option3 }}{% endcapture %}{% endif %}
{% endfor %}

您现在可以使用{{ sizes }}作为逗号分隔列表,或者替换html的逗号,例如:

{% capture sizes %}{{ sizes }}<li>{{ variant.option1 }}</li>{% endcapture %}

然后您可以使用<ul>{{sizes}}</ul>将大小列表项输出为ul。

最后,如果您确实需要将大小作为数组,则可以使用split {% assign sizes_array = sizes | split: ',' }}查看the size filterthe forloops "range" parameter可能会提供一种循环方式。

答案 1 :(得分:0)

以下代码比Dan Webb的高效得多

{% for product_option in product.options_with_values %}
{% if product_option.name == 'Sizes' or product_option.name == 'sizes' or product_option.name == 'size' or product_option.name == 'Size' %}
        <ul>
          {% for value in product_option.values %}
          <li>{{ value }}</li>
          {% endfor %}
        </ul>
{% endif %}
{% endfor %}

首先,您遍历选项并检查是否有值 然后检查选项名称是否包含大小或大小等 如果是这样-遍历值和输出

相关问题