Liquid :我如何结合两个条件?

Bir*_*den 0 conditional liquid shopify

Liquid 新手在这里寻求帮助。我有两个系列和每个系列中的一个产品,它们具有相似的名称:

(集合) 零食棒 > (产品)巧克力片

(集合)蛋白棒 > (产品)薄荷巧克力片

我正在尝试根据集合和产品句柄隐藏/显示特定于这些项目(在同一页面内)的内容。我已经尝试了以下方法,但是即使 == 应该是特定的,这也显示了这两个项目,它不是并且显示为它认为 Chocolate-chip 和 chocholate-chip-mint 是匹配的,但它不是:

{% if product.handle == "chocolate-chip" %} // do something {% endif %}
Run Code Online (Sandbox Code Playgroud)

我试过这个,但不行:

{% if collection == "protein-bars" && product.handle == "mint-chocolate-chip" %} // do something {% endif %}
Run Code Online (Sandbox Code Playgroud)

我也试过这个,但它不起作用:

{% if product.handle == "mint-chocolate-chip" | within: collections.protein-bars %} // do something {% endif %}
Run Code Online (Sandbox Code Playgroud)

最终,我只想验证我是否在产品页面上,我的逻辑检查:

  1. URL 中的产品句柄(完全匹配) mint-chocolate-chip。
  2. 该项目是该系列的一部分:蛋白质棒(不是零食棒)

https://www.blakesesedbased.com/collections/snack-bars/products/chocolate-chip

https://www.blakesesedbased.com/collections/protein-bars/products/mint-chocolate-chip

您可以在 Mint Chocolate Chip 页面上看到逻辑认为“chocolate-chip”是产品匹配,并且在 mint-chocolate-chip 页面上显示了关于 Chocolate-chip 的信息(在产品显示下方的白色部分)。

Dav*_*e B 5

Some things to keep in mind when writing your liquid statements:

  • Liquid is verbose - it uses the literal words and and or for comparisons. Example: {% if product.price > 1000 and product.price < 2000 %}
  • Conditionals cannot contain parentheses. Or at least, they can but they're ignored. Result: Best practice is to only use and or or in any single statement.
  • You cannot use filters inside of if (or unless) statements - you will want to use assign first to create a variable with all the filters applied first, then do your comparisons on that.
  • In addition to ==, >, < and !=, you can use contains inside your statements. If you're using contains on a string, you will match a substring; if you're using contains on an array, you will match an exact value in the array. (Note: you cannot use contains on an array of complex objects, like an array of variants)
  • Collections are objects, so it can never equal a string. You should test for a collection based on some property, such as collection.handle
  • The map filter is a handy way to reduce an array of complex objects into an array of simple fields

So something you could do:

{% assign product_collections = product.collections | map: 'handle' %}
{% if product_collections contains 'my-special-collection' and product.handle == 'my-special-handle' %}
  <h2>Hi Mom!</h2>
{% endif %}
Run Code Online (Sandbox Code Playgroud)