Updating variant metafield

Hello,

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?

/admin/api/2021-01/variants/{variant_id}.json

Namespace : XYZ

Key : date

Type : string

Hey @Rahul_K1 ,

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.

Regards,
John

Hello,

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"]}}”

Hey @Rahul_K1 ,

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.

{
   "variant":{
     "id":123456,
     "metafields": [{
         "id":18782039965718,
         "value": "new value"
     }]
   }
}

I hope this helps clarify things. If you have any other questions please don’t hesitate to reach out.

Regards,

John

2 Likes

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 change I have seen is, the metafield ID is being changed after every add operation.

Hey @Rahul_K1 ,

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.

Regards,

John

Hi Rahul

Just curious.. what software are you using as middleware to connect the two api end points?

Thanks,

Tnielsen

Hello,

It is Celigo’s integrator.io

Hi John,

I tried to update the field using the solution which you provided, but I am still getting an error 422. Here is my API body

{ “variant”: { “id”: 32867302572114, “metafields”: [ { “id”: 18967307550802, “value”: “test value supernew 3” } ] } }

Best regards,

Hristo

Hey @hristo_ahchiev ,

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.

Regards,

John