Skip to content

Instantly share code, notes, and snippets.

@atomgiant
Created November 20, 2015 21:48
Show Gist options
  • Save atomgiant/2f7941cd041affbad51d to your computer and use it in GitHub Desktop.
Save atomgiant/2f7941cd041affbad51d to your computer and use it in GitHub Desktop.
def Shopify.set_product_metafield_map(id, namespace, key, map)
# convert the map to JSON so it fits in one metafield
data = {
"metafield" => {
"namespace" => namespace,
"key" => key,
"value" => map.to_json,
"value_type" => "string"
}
}
request(:post, "products/#{id}/metafields.json", body: data.to_json)
end
> Shopify.set_product_metafield_map(1234567890, "product", "custom_attrs", { "width" => 11.2, "height" => 35.7 })
=> {"metafield"=>
{"id"=>10030144453,
"namespace"=>"product",
"key"=>"custom_attrs",
"value"=>"{\"width\":11.2,\"height\":35.7}",
"value_type"=>"string",
"description"=>nil,
"owner_id"=>1234567890,
"created_at"=>"2015-11-20T16:48:03-05:00",
"updated_at"=>"2015-11-20T16:48:03-05:00",
"owner_resource"=>"product"}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment