Ad

Liquid : How Do I Combine Two Conditions?

- 1 answer

Liquid novice here looking for some help. I have two collections and one product in each collection that have similar names:

(collection) Snack Bars > (product)Chocolate Chip

(collection) Protein Bars > (product)Mint Chocolate Chip

I am trying to hide/show content specific to those items (within the same page) based on the collection and product handle. I've tried the following, but this shows both items even though == should be specific, it's not and displays as it considers chocolate-chip and chocholate-chip-mint to be a match, but it's not:

{% if product.handle == "chocolate-chip" %} // do something {% endif %}

I've tried this, but no go:

{% if collection == "protein-bars" && product.handle == "mint-chocolate-chip" %} // do something {% endif %}

I've also tried this but it doesn't work:

{% if product.handle == "mint-chocolate-chip" | within: collections.protein-bars %} // do something {% endif %}

Ultimately, I just want to verify that if I'm on a product page, my logic checks:

  1. That the product handle in the URL matches (exactly) mint-chocolate-chip.
  2. That the item is a part of the collection : protein-bars (not snack bars)

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

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

You can see on the Mint Chocolate Chip page the logic believes "chocolate-chip" is a product match, and is displaying the information for chocolate-chip on the mint-chocolate-chip page (in the white section under the product display).

Ad

Answer

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 andoror 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 %}
Ad
source: stackoverflow.com
Ad