0

我已经创建了一种方法,可以在任何给定的集合页面上按尺寸过滤我的 Shopify 商店中的产品。但是,它仅在大小是唯一变体时才有效。

对于我的商店来说这很好,但由于我已经开源了过滤器,我希望它只适用于一种变体,即使有多个变体也是如此。

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

我什至尝试了推荐的 variant.option1(即{% for variant in product.variants.size.option %})但无济于事。

即使产品有多个变体,是否有某种方法可以仅输出尺寸变体的数组?

进一步的上下文。

另外,发现了这个类似的问题,但它不够相似,无法为我的用例提供解决方案。

4

2 回答 2

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 }}now 用作逗号分隔列表,或者将逗号换成 html,例如:

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

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

最后,如果您确实需要将大小作为数组,您可以使用拆分 {% assign sizes_array = sizes | split: ',' }}来查看大小过滤器,并且forloops“范围”参数可能会提供一种循环方式。

于 2015-06-27T11:12:20.577 回答
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 %}

首先,您遍历选项并检查带有值的选项,然后检查选项名称是否包含 size 或 Size 等,如果是的话-遍历值和输出

于 2019-08-07T11:06:45.017 回答