Reputation: 571
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:
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).
Upvotes: 1
Views: 3910
Reputation: 3248
Some things to keep in mind when writing your liquid statements:
and
and or
for comparisons. Example: {% if product.price > 1000 and product.price < 2000 %}
and
or or
in any single statement.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.==
, >
, <
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)collection.handle
map
filter is a handy way to reduce an array of complex objects into an array of simple fieldsSo 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 %}
Upvotes: 7