Metafields are used in shopify to add extra information of shop / collection / products / blog or like so. As there are limited fields on these instances on backend we use metafield to add field and add value.

Whereas metafields on product level is something we store for each product.

I went though Shopify Metafield api reference where there are various endpoints for various activities on metafield.

The requirement i’m discussing on this blog is updating a metafield value of a product. I have created a metafield

“namespace” = “products”
“key” = “related”
“value_type” = “string”

according to shopify metafield api reference to update metafield of a product i need to do a PUT request on

PUT /admin/products/#{id}/metafields/#{id}.json

"metafield": {
   "id": 845366454,
   "value": "titre",
   "value_type": "string"

And i followed this approach and the glitches i found on this update is we need to know the metafield id and the id different for same field on different product.

Image reference of two different product of same metafield having different id

Ok, getting metafield id isn’t that hard, i can simply look at jason data by navigating to <your-store-url>/admin/products/<product_id>/metafields.json and quicky get the metafield id of that product.

But while dynamically updating the product specific metafield value we can’t get the metafield id and update on that particular field as its different with each product for same field.

Now the hack is we will do POST request of metafield creation each time we need to update the value of metafield with same namespace and key value supplying different value for the field and it would update the metafield.

POST /admin/products/#{id}/metafields.json

"metafield": {
   "namespace": "inventory",
   "key": "warehouse",
   "value": 25,
   "value_type": "integer"

This approach doesn’t show error like the metafield is already being created. And each time we supply new value, it updates the new value to metafield.

Below is the POST request i made with php

		# Making an API request can throw an exception
		$updatemetafield = $shopify('POST /admin/products/7676657926/metafields.json', array
				"namespace"=> "products",
			        "key"=> "related",
			        "value"=> "website :",
			        "value_type"=> "string"
	catch (shopify\ApiException $e)
		# HTTP status code was >= 400 or response contained the key 'errors'
		echo $e;
	catch (shopify\CurlException $e)
		# cURL error
		echo $e;

And each time i supply the new value it does change the value. 🙂

Summary : Instead of PUT request to update metafield value do a POST request to create metafield with same namespace and key value supplying updated metafield value and this would update the value.

Happy Shopifying.

Suman K.C

eCommerce Developer, writes about stuff related to eCommerce development. Currently based in Kathmandu, loves reading about startups & new technology, introvert nature, fond of traveling & playing futsal.

More Posts - Website

Follow Me:
LinkedInGoogle Plus