Hello, I am trying to add a custom line item property to the product template in the latest version of the Origin theme. I have done so successfully and it is showing, but I cannot seem to make it render based on collection type. For example:
{% if collection.handle == 'shopify' %}
The first line of code is the issue, but every variation I try does not work. If I do it based on product handle it works. I want all products that are part of www.mywebsite.com/collections/shopify to render this line item field.
right now I am using this code which seems to be picking up product tags which is odd, but it works. Still long term it isn’t ideal for my needs:
{% if product.handle == ‘small’ or ‘shopify’%}
Any ideas on how to fix this?
Thank you to everyone for the help!
if collection.handle == ‘shopify’
That will not trigger in a product template .
collection.handle applies only to the context of either the collection templates context or when referencing a collection object either on another object , as a variable , or a snippet parameter.
Check the products actual collection_s_ array handles
https://shopify.dev/docs/api/liquid/objects/product#product-collections
{% assign product_collections_handles == product.collections | map:'handle' | join:"" %}
`{{product_collections_handles }}`
{% if product_collections_handles contains 'shopify' %}
..
double check that the map filter syntax is correct and outputting expect results
https://shopify.dev/docs/api/liquid/filters/map
If you need exact match to avoid ambiguous contains matching then loop over the array instead for direct comparisons.
I.e. the string ‘hide-on-shopify’ will match ‘shopify’ using contains creating false positive.
The tag conditional needs to be disambiguated because of a logical error.
It’s not checking a handle it’s evaluating the literal string ‘shopify’ to true because of truthiness, i.e. the string exists and has non-zero value. So it should be triggering everywhere meaning it’s just a coincendence when a product has that tag.
{% if product.handle == ‘small’ or ‘shopify’%}
to
{% if product.handle == ‘small’ or product.handle == ‘shopify’ %}
https://shopify.dev/docs/api/liquid/basics#truthy-and-falsy