【问题标题】:Conditional dropdown messaging in Shopify themeShopify 主题中的条件下拉消息
【发布时间】:2021-05-12 16:20:14
【问题描述】:

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

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 对象还没有完全点击。这里出了什么问题,如何按尺寸变体进行选择?

【问题讨论】:

    标签: shopify dropdown liquid shopify-api


    【解决方案1】:

    好的,检查代码后发现问题,你不能在条件检查里面使用{{}},这些{{}}是用来输出液体输出的。所以你的代码就是这样并且运行良好。

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      相关资源
      最近更新 更多