Do Metafield Booleans not work? Or is it just me?

Do Metafield Booleans not work? Or is it just me?

Forrest_Maready
Shopify Partner
32 0 1

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

Replies 4 (4)

iDoThemes
Trailblazer
207 43 94

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 }})

 

Developer of Liquify Chrome Extension -- Enhance the Shopify Theme Code Editor
.




Theme Developer -- Drop me a line
Forrest_Maready
Shopify Partner
32 0 1

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!

KeshavSri
Shopify Partner
2 0 0

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 

Brandon-Flexi
Shopify Partner
10 0 6

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!