Update Metafield Values

Update Metafield Values

Adam_Robson
Shopify Partner
18 0 7

I am attempting to update a custom metafield for each product. We have tens of thousands of products and I was hoping to send the update metafield information using something like this: 

https://MYSHOP/admin/products/7050337351.json

sending JSON as: 

{
    "product": {
        "metafield": [{
            "namespace": "custom_fields",
            "key": "warehouse_location",
            "value": "1234",
            "value_type": "string"
        }]
    }
}

It seems I need to first capture the ID of each metafield previously created before I can update a metafield? Is that correct??? Given that we have tens of thousands of products do I first need to obtain every metafield ID of every product and store it locally before making a second API call to then update the metafield? 

I was hoping to simply update the metafield using the product ID as the API would have enough information to process the update? 

I note many other people on the forum have a similar issue.

Any help would be appreciated? Thank you for reading.

Replies 4 (4)

Zac12
Shopify Partner
65 0 20

If you store to a metafield with the same namespace/key then it will rewrite what was previously set inside of that metafield.

Note that the metafields themselves have relativly large character limits, while the key and namespace are pretty locked down.

Key limit: 30 characters

Namespace limit: 20 characters

https://help.shopify.com/api/reference/metafield )

Value limits: 65,535

( https://ecommerce.shopify.com/c/shopify-apis-and-technology/t/maximum-characters-in-metafield-335323 )

The company I work for went down this route as well with indexing a large range of product information, and ended up writing a custom JSON array, storing it to the customer's assets folder in their theme then calling that with some liquid. Though once you get a number of products in there, it can get pretty large.

Zyber Developer | https://apps.shopify.com/trademe | https://apps.shopify.com/productfilter | https://apps.shopify.com/splittest

Adam_Robson
Shopify Partner
18 0 7

For anyone interested, managed to get this working (updating the metafield) by using the following:

{
  "metafield": {
    "namespace": "custom_fields",
    "key": "warehouse_location",
    "value": "2345",
    "value_type": "string"
  }

and using POST to:

https://MY-SHOP/admin/products/PRODUCT-CODE/metafields.json

Hope the information helps someone else.

Fabián_Valencia
Shopify Partner
8 0 1

That syntax gives a bad request error.

{
"errors": {
"metafield": "Required parameter missing or invalid"
}
}

 

Jason
Shopify Partner
11196 226 2287

Fabian - It would be worth seeing the complete data you are posting to the API call along with the url you are using you are posting to. If using a private API key be sure to remove both the key and password before posting.

★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★