What's your biggest current challenge? Have your say in Community Polls along the right column.
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

How to get Metafield for all variants of a product

How to get Metafield for all variants of a product

CFashion
Shopify Partner
8 0 0

I have a software platform that creates products on Shopify with Rest API calls.
The variants of these products have a metafield 'supplierRef' in the 'descriptor' namespace.
When I create a product with variants with that metafield it works fine.
When I try to update an existing product variant I get an error because the metafield I try to push already exists.
When creating the product the variants in the body response do not have the metafield data attached to them.

How can I get the metafield Id of all the variants with a Rest API call?

Or how can I make the create/update call return the metafield data?

Reply 1 (1)

francesco_gs
Shopify Partner
73 6 12

Hi there!
in order to update a metafield on a resource you will need to pass the metafield_id along with metafield info (namespace, key, type, value).

In order to retrieve this info, you will need to ask for resource' metafields with the /metafields.json endpoint, so in your case 

GET /products/{{product.id}}/variants/{{variant.id}}/metafields.json

 

Unfortunately the create call cannot return the metafields data.

 

I strongly suggest you to switch to GraphQL for this kind of API operations, it is a definitely more efficient and powerful method.