How can I correctly use text metafields in liquid statements?

Topic summary

Using Shopify Liquid, the goal was to conditionally render image badges based on a product text metafield. Boolean metafields worked, but comparisons against text failed.

Observed values: product.metafields.custom.product_gluten_contents returned [“Certified Gluten Free”], while .value returned Certified Gluten Free. Direct == string comparisons did not evaluate as expected, likely due to list formatting, casing, or whitespace.

Suggested check: use a single-line text metafield with preset choices, and print the value ({{ product.metafields.custom.product_gluten_contents.value }}) to verify it’s not null and matches exactly.

Resolution: the OP normalized the value by assigning it to a variable and applying filters (downcase and remove spaces), then matched using a case statement. This reliably mapped to three outputs: Certified Gluten Free, Naturally Gluten Free, and Contains Gluten.

Technical context: Liquid is Shopify’s templating language; metafields store custom product data and can be lists or strings, affecting equality checks.

Outcome: Issue resolved; badges render correctly after normalization. No further action items or open questions.

Summarized with AI on January 21. AI used: gpt-5.

On my product page I have a custom liquid section to display an image badge depeding on metafeild value. With true or false metafield I can easily do it but when using text metafields I can’t get it to work.

Example:

{{ product.metafields.custom.product_gluten_contents }} returns the value [“Certified Gluten Free”]

{{ product.metafields.custom.product_gluten_contents.value }} returns the value Certified Gluten Free

The statement I am trying to get to work:

{% if product.metafields.custom.product_gluten_contents.value == "Certified Gluten Free" %}
<p>Certified Gluten Free Image Placeholder</p>
{% elsif product.metafields.custom.product_gluten_contents.value == "Naturally Gluten Free" %}
<p>Naturally Gluten Free Image Placeholder</p>
{% elsif product.metafields.custom.product_gluten_contents.value == "Contains Gluten" %}
<p>Contains Gluten Image Placeholder</p>
{% endif %}

I tried:

product.metafields.custom.product_gluten_contents.value == “Certified Gluten Free”

product.metafields.custom.product_gluten_contents.value == ‘Certified Gluten Free’

product.metafields.custom.product_gluten_contents.value == [“Certified Gluten Free”]

product.metafields.custom.product_gluten_contents.value[“Certified Gluten Free”]

product.metafields.custom.product_gluten_contents == [“Certified Gluten Free”]

and countless others…

What syntax am I getting wrong?

Thank you :slightly_smiling_face:

1 Like

Hi @onlineorganics

Have you tried using single line text with limit to preset choices? You can also try printing the value of the metafield by using the code below so you can verify the value is equal to your text and also value is not null.

Product Value: {{ product.metafields.custom.product_gluten_contents.value }}

I hope it help.

Please don’t forget to Like and Mark Solution to the post that helped you. Thanks!

Thank you for the reply

{{product.metafields.custom.product_gluten_contents.value }} returns the value Certified Gluten Free

That is why I dont understand why the elsif statement is not working

Found the solution storing it in a variable with a case statement:

{%- assign gluten_level_value = product.metafields.custom.product_gluten_contents.value | downcase | remove:" " -%}
{%- case gluten_level_value -%}
{%- when '["certifiedglutenfree"]' -%}

Certified Gluten Free Image Placeholder

{%- when '["naturallyglutenfree"]' -%}

Naturally Gluten Free Image Placeholder

{%- when '["containsgluten"]' -%}

Contains Gluten Image Placeholder

{%- endcase -%}