Conversations about creating, managing, and using metafields to store and retrieve custom data for apps and themes.
I am sending PUT requests to the /products API to update the Product and its Variants at the same time (ie - one API call). Both the Product and Variants have metafields which I am passing as part of the API call.
The issue I am running into is the Product Variant metafields are not being updated if they already exist. I always pass the existing Metafield ID as part of the call so Shopify should be able to find and update the Metafield. If I don't pass the ID, Shopify complains about non-unique Metafields.
Here is a sample request, truncated for brevity and to highlight how I am passing the Metafields:
{ "product": { "id": 12345, "title": "Some title", "variants": [ { "sku": "some-sku", "metafields": [ { "namespace": "omx", "key": "Full Price", "value": "50", "value_type": "string", "id": 3456 }, ... ], "position": "1", "id": 2345 } ], ... } }
This seems like a bug with the API - should I open a support request with Shopify?
I know there is a separate Metafield API, but given the volume of products in this store it would take days to run a full update if I have to update each metafield one by one so that is not an option.
Thanks,
Tom
I'm sorry but I'm afraid that you'll need to perform an update on each metafield individually. While you do get metafields associated to an object you cannot update them.
I'll admit that this is really odd considering you can attach metafields to a variant or create a new variant with metafields attached to it. I think the concern might stem from being able to accidentally clobber other metafields.
I'll file a ticket against our API. I suggest you subscribe to our API Updates forum so you can find out about any new changes/improvements to our API which is where we will announce this if it happens.
Chris | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
Hey Chris,
Thanks for your reply. I ended up just writing a one-off process to update existing product variant metafields.
Also, the Shopify API does support updating Product Metafields in a single PUT, but Product Variant Metafields only support creating new metafields. So the API is not consistent here.
I would love to see this addressed as there is not a great option for doing a bulk update of Product Variant Metafields.
Thanks for your help.
Tom
Are some of the API shortcomings like this on the development list for the next major release? We're loving the scalability of shopify, but maintaining data in Shopify is painful when you have a lot of diverse products, collections,varirants,metadata,navigation linklists,etc.
Updating related records (product metafields, variants, variant metafields, collection metafields, etc) should be doable in one API call, especially when it comes to metafields where you can only have one meta field value per object, you should be able to update them when you call the object that has metafield values.
Often times, the API allows for creation of related records in one API call, but doesn't allow for updates & you have to make multiple calls just to get the object and it's related records.
Also - I may be missing something, but it appears that there is no way to prevent duplicate handles from being inserted via the API.. PLEASE PLEASE add the ability to enforce unique handles instead of automatically changing the handle that is specified in the API call to append 1,2,3..etc....this would eliminate a lot of API overhead in retrieving a list of objects just to check to see if it exists before you insert/update. Right now you just end up with a duplicate inserted & shopify adds 1,2,3,etc to the handle.
With the API requiring multiple calls to get related record IDS and then multiple calls to update the object and it's related records, you can end up with several api calls for an update task.
Make a mistake on a large update that takes several hours and it's enough to make you want to cry and pull your hair out.
It's bad enough only being able to make between 2 and 10 API calls per second, but when you have to make so many calls to manage updates to each object and you have thousands of objects to update it's just really really frustrating.
It's taken us a lot of stumbling around to figure out each API call that needs to be made to get and update an objects related records, especially since there are big differences between one type of objects API and another.
Please consider developing more task-oriented API documentation that explains the steps needed to update a product, it's variants, and metadata. (including related records supported on create, update, etc). Troubleshooting info with explanations of error messages like "metafields.key:must be unique within this namespace on this resource" for example.
I've hit this problem as well - been pulling my hair out for the last few days with updating a few metafields... It's a real PITA... Having to do so many api calls to get xyz is also a pain in the bum... I'm in callback hell.
Oh - and this text editor... I spent about 30 seconds clicking on it (in chrome) as it doesn't automatically give you a cursor... So I just typed and it worked.
@rpw - I agree this is a huge PITA. I ended up just using this approach:
1) For each product, select all variants 2) For each variant, select all metafields 3) Update each variant one at a time
The API around metafields is sorely lacking. You would have to send a delete call for each metafield anyway so you might as well just do an update instead of a delete then update.
One other approach I would probably use if I need to revisit variant metafields - don't use them 🙂 I am thinking you can store variant metafield data as product metafields instead using a convention for naming the keys such as:
KEYNAME_SKU = value
Then you can just build that string in Liquid template and use top-level product metafields for all variants. Top-level product metafields do support bulk updating in my tests.