I am trying to add/update the variant metafield of a product through API. If there is no value, then I am able to add the date. If there exists a date value already, then I am not able to update it. Could anyone help me with this please?
Happy to help you out with this. Are you using a PUT operation when you try the update? If you are, can you share the x-request-id for the failed update request. I can review our logs further to see if that yields any clues.
That’s right, I am using PUT method. The x-request-id
is 3ed2c211-a395-4138-a185-3cd8a2ad1c97. I believe this is the one. If this
doesn’t work, could you please look at this
– 3eaf6b38-88fd-4267-a2ca-5682af46de68.
The error I am getting is, “{"errors":{"metafields.key":["must be
unique within this namespace on this resource"]}}”
When updating the metafield via a PUT Request to the ProductVariant you’ll need to include the id of the metafield. If you don’t include the id of the metafield you’re communicating to Shopify that in the context of the ProductVariant update I want to create a metafield. That’s the reason for the error, as the key already exists. Here’s an example of what I mean by including the metafield id.
I am getting a different error now. With the solution you provided, it worked first time. The next time when I tried to update, the error I get is {"errors":{"base":["Metafield does not exist"]}}
The ID of a metafield is unique per resource it’s associated to. In this instance a metafield id of one product variant, will not have the same metafield id of another product variant, and once created for the product variant the metafield id will not change. As you add metafields to other product variants even if the metafield properties (value, key, namespace, type) are the same, the metafield id is not shared and will be unique per variant.
Given this explanation, if your testing shows that the metafields for a given resource are changing can you could provide some examples of the request payloads. Also, the x-request-id found in the response headers of each request would help diagnose any issue.
This indicates that the variant id used doesn’t match where the metafield specified exists. I would recommend using the metafield endpoint to determine the resource that owns that metafield with id 18967307550802. I believe you’ll notice that the metafield is attached to another product variant.