2
votes

Greetings Shopify Pals!

I have a website at: https://www.blakesseedbased.com/ (password: TopSecretPass)

The Goal: We will have products that vary in quantity and would like to display this quantity information between the product title and price from the featured home page products (raspberry, chocolate chip, pineapple).

The Issue: Currently we're able to add content to this area, but we cannot seem to get the if else statement to work correctly. We have a collection called "Snack Bars" however our logic isn't recognising this and injecting the proper code. We tried changing the collection Snack Bars to lower and uppercease snack-bars/Snack-Bars but no luck.

The Logic: IF the product item is from the Snack Bar collection, add the box quantity (box of 12). Else, add "no quantity"

The Code:

{% if product.collections contains 'snack-bars' %}
         <span>Box of 12</span>
{% else %}
          No Box Amount
{% endif %}

Thanks in advance!!

1

1 Answers

1
votes

Greetings Birdie Golden!

The issue you're hitting is that product.collections is an array of Collection objects when you're accessing it in Liquid, and no collection object will ever equal a string value.

Luckily, we can easily create an array of handles using the map filter and assign that to a variable, like this:

{% assign collection_list = product.collections | map: 'handle' %}
{% if collection_list contains 'snack-bars' %}
...

The map filter above will create a new array using just the value that you pass it, so the collection_list variable will be an array of just-the-handles. Since handle is a string, we can use contains as expected to see if the value we want is in the list.

Hope that helps!