I’ve got an integer metafield with a value of 12 on a product.
When I use it with a comparison operator:
{% if product.metafields.question_time.questions_count > 0 %}
I get an error: ‘Liquid error: comparison of String with 0 failed’
Liquid seems to be treating the metafield as a string even though the API confirms the metafield is an integer.
According to the documentation integer metafields should be treated as such by Liquid:
You can also specify a metafield as either an integer or a piece of text (a “string”). That way, you’ll end up with the right type of data when you use the metafield in your Liquid.
Is this a known problem? Or am I missing something here?
Yes, I created it as an integer. I then checked by retrieving the metafield through the API and it reports the value_type as integer. The product id is 95821994 if you want to verify from your end?
I’ve filed a ticket about this. I’m on the team that is responsible for liquid drops and all that fancy business in Shopify storefront. I can’t make any promises about when it’ll be shipped, but this article is in the ticket so when it ships I’ll try my best to remember and provide an update about it.
Just to provide an update - We’ve gotten to the source and I’ve got a patch in review. It’s reverse compatible with the current liquid implementation you are using, but you can just compare integers if that’s the data type you know you’ll be working with.
I’m guessing this patch got released? We had a bug in our integration that just got revealed by this.
Let me explain, Chris, and you can let me know if there are any workarounds:
We had a metafield that was being defined as an integer by mistake - it was really supposed to be a string. It didn’t get caught since it was still treated as a string when we read it, and all was working. Then, starting today-ish, those strings started returning “0” instead of their original value.
The unfortunate part is that it appears that the stored data has been converted to an integer (I’m guessing you guys did a data migration to convert all of the integer fields to an integer in storage?) If I change the metafield type to a string, I don’t get my old value back - I still get back “0”.
Am I correct about that last bit? Or is there some way to recover our originally stored strings? Its going to be very hard to reproduce this data - we’ve got thousands of these metafields spread around hundreds of different shops on our install base.
The data should still be fine. This is a long shot but try updating the metafields to be the proper value type of string and see if you can recover your data.
Chris, thanks for the reply. You are correct - if I update the metafield to be type “string” then the data does come through with its original value. (I was wrong earlier when I thought it didn’t)
I wanted to share a small gotcha regarding this for others who may be experiencing the same issue:
Care must be taken if you are using the Shopify ruby gem, and you need to change the metafield value_type without changing the actual value. If you, like me, erroneously stored a string value but set the value_type to integer, your data now (probably) reads out as “0” rather than your original string. The data CAN be recovered by changing the value_type to string, but you must take care not to overwrite your data at the same time. ActiveResource (the lib that underlies the Shopify ruby gem) sends by default ALL of the fields when performing an update - including your value field which now looks like a “0”.
Don’t do this:
metafield # => {"value" => 0, "value_type" => "integer"}
metafield.value_type = "string"
metafield.save
# You just overwrote "value" with "0"!
Instead I suggest:
metafield # => {"value" => 0, "value_type" => "integer"}
minimal_attributes = metafield.attributes.slice("id", "owner_id", "owner_resource")
minimal_attributes.merge!("value_type" => "string")
metafield.attributes = minimal_attributes
metafield.save
# You updated just value_type and left the value as it was. If you had a string in there before, you now have access to it again
Can anyone help - we have many Metafields saved as integer type but which had decimals and we output them as e.g. {{ product-block.metafields.inventory.inches }}. But we had data values in there that were decimals like 9.5.
They are now showing as eg. “9”. How do we output the original data? We are tld we need to convert integer to string. How do we do that outside of the API?
Shopify - why do you do such things without telling people?
You’ll need to update your metafields such that they are stored as integers. So get all your metafields of interest and simply update their value_type.
PUT /admin/metafields/1234.json
{
"metafield" : {
"value_type": "string"
}
}
This was brought to our attention and was actually showing that our API was broken since values were supposed to be integers but were being stored as strings. There was nothing preventing any value from being set as an integer, so you could create a metafield whose value is “abracadabra” with a value_type of integer and that would be “correct”.
The values are stored as strings because it’s the “common denominator” of data-types. So we are just coercing the data to the expected data-type when we read it out of the database. So if it’s an integer we coerce it into an int.