0
votes

I have added a metafield to a collection within my Shopify store. I have a namespace, key, and value. I am looking for a liquid code snippet to check if the collection has a specific metafield key, and then if so, to output the assigned value.

I have tried the following with no success:

{% if relationship = collection.metafields.parent %}
    <span>{{ relationship.parent[value] }}</span>
 {% endif %}

Does anyone have any idea of how I could implement this functionality?

Thanks.

1
What is inside of the variables relationship, value and collection.metafields.parent? Wouldn't collecation.metafields.parent just be the namespace and not a specific metafield?Sam
Note: You can't assign variables inside an if statement in Liquid!Dave B

1 Answers

0
votes

I guess if you wanted to check for the existence of a metafield, or more specifically the existence of a value within a metafield, you could do:

{% if collection.metafields.parent['metafield_name'] %}
  <span>{{ collection.metafields.parent['metafield_name'] }}</span>
{% endif %}

This is taking the value of whatever is contained in the metafield and checking if it returns a truthy or falsey type value. If the value is truthy (if there's text in the metafield) it will then output that to the screen.