Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
For some reason, I can't get any values out of a boolean metafield.
Here's the metafield as it exists (returned from the API):
"metafield": {
"owner_id": 55631235341,
"namespace": "my_store",
"key": "campaign_ended",
"value": true,
"id": 19250369592161,
"description": null,
"created_at": "2022-01-18T12:11:38-05:00",
"updated_at": "2022-01-18T12:30:00-05:00",
"owner_resource": "shop",
"type": "boolean",
"admin_graphql_api_id": "gid://shopify/Metafield/19250593867161"
}
Here are two ways I'm trying to use it. One, just to see the output, and another to check true/false as to whether to show something or not.
<h2>{{ shop.metafields.my_store.campaign_ended }}</h2>
{% if shop.metafields.my_store.campaign_ended %}
<div class="rich-text__blocks">
<h1 class="pledged-total">Finished.</h1>
</div>
{% endif %}
In the <if> check, the div and text never show, even though I know the value is true.
I've also tried:
{% if shop.metafields.my_store.campaign_ended == true %}
If it's any help, in the above example, the <h2> tag outputs nothing. Perhaps I need a filter? I can't figure that one out either.
Any ideas how to use a boolean metafield within an <% if %> statement?
Many thanks
Try string comparison instead
== "true"
Or try outputting the metafield as an object to make sure you have the correct format:
console.log({{shop.metafields.my_store.campaign_ended | json }})
console.log({{shop.metafields.my_store | json }})
That json trick helped me figure out the problem, thank you very much. The problem was I was updating the value in the wrong store via Postman variables being set incorrectly. That's a stupid problem.
Thank you for the kick!
Hi @Forrest_Maready ,
would you mind to share, how we can test boolean metafields on postman?
I am also facing the issue for boolean metafields.
Thanks in advance
Just completed some testing with boolean metafields! This is a collections metafield example, but I found if you use ".value" after your metafield, you can create an if statement with true or false!
For example:
{% if collection.metafields.custom.subscriber_only.value == true %}
This is true
{% else %}
This is false!
{% endif %}
Hope this helps!
In Canada, payment processors, like those that provide payment processing services t...
By Jacqui Mar 14, 2025Unlock the potential of marketing on your business growth with Shopify Academy's late...
By Shopify Mar 12, 2025Learn how to increase conversion rates in every stage of the customer journey by enroll...
By Shopify Mar 5, 2025