Shopify 主题中的条件下拉消息

Conditional dropdown messaging in Shopify theme

我正在尝试根据可用性在 Shopify 主题的变体(大小)下拉列表中构建条件 messaging/labels。 (在下面的代码片段中解释 - 应该给出大小和与可用性相关的消息)。我使用了几个不同的文档链接到达那里:

https://shopify.dev/docs/themes/liquid/reference/objects/product_option https://shopify.github.io/liquid-code-examples/example/product-variant-selector

并提出以下内容,写在 product_template.liquid 中:

 <select id="ProductSelect-{{ forloop.index0 }}" data-index="option{{ forloop.index }}">
           {%- if {{ product_option.name == "size" }}  -%}
                {%- for value in option.values -%}
                    {%- if product.variants[forloop.index0].available -%}
                        <option value="{{ value | escape }}  - ships faster "{% if option.selected_value == value %} selected="selected"{% endif %}>
                     {{ value | escape }} 
                        </option>  
                     {%- else -%}
                        <option value="{{ value | escape }} - ships longer "{% if option.selected_value == value %} selected="selected"{% endif %}>
                    {{ value | escape }} 
                        </option>
                    {%- endif -%}
            {%- endfor -%}
        {%- endif -%}
      </select>

在尝试保存时出现此错误:This file contains the following errors: Line 101 — Liquid syntax error: Unexpected character { in "{{ product_option.name == "size" }}"

我通常对 Liquid 没意见,但 Shopify 对象还没有完全点击。这里出了什么问题,我如何 select 尺寸变体?

好的,检查代码后发现问题,不能在condition check里面使用{{}},这些{{}}是用来输出liquid输出的。所以简单地说,您的代码就是这样并且运行良好。

<select id="ProductSelect-{{ forloop.index0 }}" data-index="option{{ forloop.index }}">
           {%- if  product_option.name == "size"   -%}
                {%- for value in option.values -%}
                    {%- if product.variants[forloop.index0].available -%}
                        <option value="{{ value | escape }}  - ships faster "{% if option.selected_value == value %} selected="selected"{% endif %}>
                     {{ value | escape }} 
                        </option>  
                     {%- else -%}
                        <option value="{{ value | escape }} - ships longer "{% if option.selected_value == value %} selected="selected"{% endif %}>
                    {{ value | escape }} 
                        </option>
                    {%- endif -%}
            {%- endfor -%}
        {%- endif -%}
      </select>