Skip to content

Instantly share code, notes, and snippets.

@csaunders
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csaunders/9467039 to your computer and use it in GitHub Desktop.
Save csaunders/9467039 to your computer and use it in GitHub Desktop.
Metafield Migration Guide

It was brought to our attention that the integer value type in our metafields API wasn't working the way you'd expect and required weird hacks in liquid such as {{ metafield.value | plus: 0}} to coerce the values back into integers. This was anything but desirable and we wanted to correct this discrepancy. There were some side-effects that came out of this which were accidental.

This change fixes the way the Shopify Metafield API behaves. According to the documentation two value_types are supported, string and integer though the integer value wasn't being respected and was simply just a string.

With the changes that have been deployed; if your field was an set to an integer value_type it will be properly coereced into the data-type you are expecting. Though if you were using the field incorrectly such as storing "some string data in there" or using unsupported data-types such as float/decimal values (i.e. 14.2) your values would be returned as 0 and 14 respectively.

Did you just destroy my data!?!

Your data is perfectly safe unless you overwrite it and it is rather simple to migrate the data. If you are using the Shopify API gem you can do something similar to the following:

require 'shopify_api'
require 'active_support/all'
metafields = ShopifyAPI::Metafield.find(:all)
metafields = metafields.select { |m| is_metafield_you_care_about?(m) }

metafields.each do |m|
  minimal_attributes = m.attributes.slice("id", "owner_id", "owner_resource")
  minimal_attributes.merge!("value_type" => "string")
  m.attributes = minimal_attributes
  m.save
end

What you want to ensure is that you aren't sending the metafield value in your request.

If you aren't using the Shopify API gem then you'll want to be sure that you are sending JSON similar to the following:

metafield_data.json

{
  "metafield" : {
    "id" : 12345,
    "owner_id" : 54321,
    "owner_resource" : "product",
    "value_type" : "string"
  }
}

You could send this data to a Shop using cURL:

curl -X PUT \
     -H "Accept: application/json" \
     --data @metafield_data.json \
     https://apikey:apipassword@exampleshop.myshopify.com/admin/metafields/12345.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment