我想在product.variants标题==“ size”或“ Size”中显示具体文本if
我尝试过:更新-这将是正确的方法,但仍不会发生相同的结果
{% for option in product.options %}
{%- if option == "size" or "Size" -%}
<p id="green-hover">Review size before adding to cart.</p>
{% break %}
{%- endif -%}
{% endfor %}
{% for option in product.options %}
{%- if option contains "size" or "Size" -%}
<p id="green-hover">Review size before adding to cart.</p>
{% break %}
{%- endif -%}
{% endfor %}
在红宝石中,我会这样做:
@product.options.any? { |option| option== "size" || "Size" }
至少这样,未经测试-不要解雇我。
我该怎么做才能让我检查product.variants标题是否等于某个字符串或值?
这似乎起作用:
{% assign option_titles = product.options | join: ' ' %}
{% if option_titles contains 'Size' %}
<p id="green-hover">Review size before adding to cart.</p>
{% endif %}
👋
您是对的,这里的#any?
超级有用,但是Liquid没有这种能力。因为您想同时查找“大小”和“大小”,所以有点笨拙,但这应该可行:
{%- for option in product.options %}
{%- if option contains "size" or option contains "Size" %}
<p id="green-hover">Review size before adding to cart.</p>
{% break %}
{%- endif %}
{%- endfor %}
如果条件中有and
或or
,则需要重复整个条件两次。因此是if option contains "size" or option contains "Size"
而不是if option contains "size" or "Size"
。