All things Shopify and commerce
Hi, I'm trying to use this API:
https://shopify.dev/docs/api/admin-rest/2023-04/resources/product-variant#put-variants-variant-id
PUT /admin/api/2023-04/variants/808950810.json
example body:
{
"variant": {
"id": 808950810,
"price": 12.79,
"metafields": [
{
"key": "symbol",
"value": "asdasd",
"type": "single_line_text_field",
"namespace": "custom"
}
]
}
}
And it works as described below:
If the metafield isn't set on this variant, it will work fine; it will set the value and return a 200 response.
If the metafield already contains a value, it will return a 422 error with "could not be saved" information.
When adding a new product with a POST request and metafields in nested data, it works as expected. The body has a structure like:
{product: variants:[{metafields: [{},{}]}]}
But it also does not accept updates.
After searching this forum and documentation, I found an endpoint that will return all metafields for a resource:
https://shopify.dev/docs/api/admin-rest/2023-04/resources/metafield#post-blogs-blog-id-metafields
GET /admin/api/2024-04/variants/808950810/metafields.json
It returns 200 with all metadata. Someone on the forum said it can accept a PUT request, but every structure I try to send returns a 406 - Not acceptable response.
Then I tested the last part from the documentation. From the above endpoint, I can obtain the ID for the metafield value object. If I have the ID, I can try to update only this value.
https://shopify.dev/docs/api/admin-rest/2023-04/resources/metafield#put-blogs-blog-id-metafields-met...
PUT /admin/api/2024-01/variants/808950810/metafields/41959940456773.json
Body:
{
"metafield": {
"id": 41959940456773,
"namespace": "custom",
"key": "symbol",
"value": "TP_SM-asdasdasdasdasdasd",
"owner_id": 808950810,
"owner_resource": "variant",
"type": "single_line_text_field"
}
}
Yes, it works.
But if I have 20 custom fields on my variant, I don't want to make 20 requests to update each value in separate requests. Is there any method to update multiple metafields in one PUT request?
The ID numbers provided in the examples are not correct. You can trust that they were provided correctly.
If more information is needed, please explain what additional information I should provide? Support referred me to the forum.
Greetings!
2m ago Learn the essential skills to navigate the Shopify admin with confidence. T...
By Shopify Feb 12, 2025Learn how to expand your operations internationally with Shopify Academy’s learning path...
By Shopify Feb 4, 2025Hey Community, happy February! Looking back to January, we kicked off the year with 8....
By JasonH Feb 3, 2025