Check for empty values
As a general rule, if a value of a custom field is empty, its corresponding metafield either does not exist or is an empty string. A test for that particular custom field will not return anything ("blank" in Liquid terms), which gives you a nice clean syntax for checking if a given field has a value:
1
{% if product.metafields.accentuate.title %}
2
<h1>{{ product.metafields.accentuate.title }}</h1>
3
{% endif %}
Copied!
If you need to show a default value in case nothing is assigned to a metafield, you can use the 'default' Liquid filter:
1
<h1>{{ product.metafields.accentuate.title | default: 'Default title' }}</h1>
Copied!

Multi-language fields

For multi-language fields, where there can be a value for one language and no value for another, you will need to test if the content for a specific language is a blank string or not:
1
{% if product.metafields.accentuate.message['en'] != blank %}
2
<p>{{ product.metafields.accentuate.message['en'] }}</p>
3
{% endif %}
Copied!

Repeatable fields

If you are testing to see if an element in a repeatable field has a value, you need to test if the element's value is either an empty string or null, both of which can be tested via the length of the element's value:
1
{% for ingredient in product.metafields.accentuate.ingredients %}
2
{% if ingredient.size > 0 %}
3
<p>{{ ingredient }}</p>
4
{% endif %}
5
{% endfor %}
Copied!
And, if you are testing to see if a repeatable field has any elements at all:
1
{% if product.metafields.accentuate.ingredients.size == 0 %}
2
<p>No ingredients have been added</p>
3
{% endif %}
Copied!
Please see this article for details on using the size filter.
Last modified 11d ago
Copy link