【问题标题】:Output only size variants in Shopify's liquid logic在 Shopify 的流动逻辑中仅输出尺寸变体
【发布时间】:2015-06-25 04:31:00
【问题描述】:

我已经 created a way 在任何给定的集合页面上按尺寸过滤我的 Shopify 商店中的产品。但是,它仅在大小是唯一变量时才有效。

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

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

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

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

Further context.

另外,找到了这个similar question,但它不够相似,无法为我的用例提供解决方案。

【问题讨论】:

  • 我认为product.variants.size 将仅返回产品变体总数。

标签: shopify liquid variants


【解决方案1】:
{% 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 %}

然后您可以使用&lt;ul&gt;{{sizes}}&lt;/ul&gt; 将大小列表项输出为 ul。

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

【讨论】:

    【解决方案2】:

    下面的代码比 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 等 如果是这样 - 遍历值和输出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 2018-09-14
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 2021-03-18
      • 2010-11-21
      • 1970-01-01
      相关资源
      最近更新 更多