Conversations about creating, managing, and using metafields to store and retrieve custom data for apps and themes.
Hello,
Someone have some piece of code to update metafield of a product's variant ?
I don't find any solution for this.
Thank you 🙂
Hi Jeremy,
It really depends on language you're using. Our documentation on metafields gives you the raw request here. You'd just need to use POST /admin/products/#{id}/variants/#{id}/metafields.json as the endpoint. If you want to update a metafield, you would switch that POST for a PUT and omit the namespace field.
Here's what it would look like in Ruby using the shopify_api gem, for example.
variant = ShopifyAPI::Variant.find(variant_id)
variant.add_metafield(ShopifyAPI::Metafield.new({
"namespace": "test",
"key": "test",
"value": "this is a test"
"value_type": "string"
}))
Cheers!
To learn more visit the Shopify Help Center or the Community Blog.
Thank you for your fast answer.
I'm sorry for the missing part, i'm using PHP.
I check the doc, it talk about "Update a product metafield" but not a variant of a product.
I'm still looking for the answer, if anyone got it 🙂
If you have the actual metafield itself, just change the value and save it. That works fine. Otherwise, you an just create a new metafield with the same namespace and key, and Shopify will replace the old one, effectively giving you an update. So those are two simple things for you to do.
Hello HunkyBill,
I just try to create a new metafield with the same namespace / key but i have this error :
[errors] => Array ( [metafields.key] => Array ( [0] => must be unique within this namespace on this resource ) )
Here is the url i use :
https://X:X@vivafiesta.myshopify.com/admin/variants/34120177732.json
And the array i PUT :
$products_array = array( "variant" => array( "id" => '34120177732', //"price" => '13', "metafields"=>array( array( "key" => "stock_01", "value" => 100, "value_type" => "integer", "namespace" => "perso" ) ) ) );
Another tips maybe ?
If you do a POST of the same key then yes, you are doing it wrong. Try a PUT which is the correct verb.
No i was using a PUT.
Here is the full code :
define ('BASE_URL' , "https://".$apiKey .":". $pwd ."@vivafiesta.myshopify.com/admin/");
$id_produit ="9805174148";
$id_variant = "34120177732";
$products_array = array(
"variant" => array(
"id" => $id_variant,
"metafields"=>array(
array(
"key" => "stock_01",
"value" => 100,
"value_type" => "integer",
"namespace" => "perso"
)
)
)
);
$url = BASE_URL . "variants/".$id_variant.".json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($products_array));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
$retour = json_decode($response, true);
print_r($retour);
Is there a way to update a variant metafield without having the ID of the metafield ?
I don't think so, I usually just load them, then loop through the fields until I find the namespace/key I need then do an update.
Thank you Trey.
I do it like you say but i have ~8000 products and 6 metafield field that i want to update that way.
The only problem is that it take really a long long time to update all the metafields 😕
Nothing like 6.67 hours of steady API calls to make updates. Sometimes it is not that you chose to use Metafields, but more how you structured your needs. For example, what if you combined all 6 into one data structure, a small snippet of JSON, and then use that? You'd go from 6.67 hours in the best case to 1.1 hours... with a re-arrangement of the data. You could try that anyway.
Everyone knows or should know anyway that Variant Metafields are your slowest possible option since there is no mass write possible, whereas if you package them up as one product metafield, you gain huge speed, as you get that ability to read all variants in the one product API call, followed by one write.
The metafield that i want to update is the stock of our shop so i can't add it to the product level because i have the size and color as variant.
Is it possible to store an array as a variant's metafield ?
Of course you can store it at the product level. It is nothing but a KEY:VALUE pair... so you use the variant ID as the KEY and store the value... be creative. It is a simple data structure.
Thank you Hunkybill i'm about to find a way. It's not really easy to read
(39101 s-1;2;3;4;8;10!39102 m-1;2;3;4;8;10)
but it'll work 🙂
That's the way to go. Has nothing to do with being easy to read. It is a simple data structure, and computers and scripting languages are made for manipulating that data. Since Metafield resources are ideal at storing strings, and JSON is nothing but a string, you are going to benefit by using JSON. I see however you chose your own hand-rolled cryptic format, like a Bank or Insurance company from the 1970's using COBOL or Fortran!
All the power to you! Happy trails! Glad you at least figured out the basic pattern!
Hello Hunky,
I try with json but i don't know how to read it when i'm in my liquid file 😞
Here was the json sample i used :
{"stock_global":[{"sku":"sku1","stock":["1","2","3"]},{"sku":"sku2","stock":["4","5","6"]},{"sku":"sku3","stock":["7","8"]}]}
Any advice ?
Use Javascript after you render the page... that is what it is for.