Dynamically Created Metafields not displaying on Front End

I’m having a weird issue where Product/Variant Metafields that I am creating dynamically through the API are not displaying on the front end of my website. But, when I delete the Metafield and then Manually recreated it using the exact same Namespace, Key and Value it shows without any issue.

Here is what i’m using the create the Metafields with PHP

$products_array = array(
		  "variant" => array(
			  "id" => $variantId,
			  "metafields"=>array(
				  array(
				  "key" => "variant_preorder_ship_date",
				  "value" => $newPreOrderDateString,
				  "value_type" => "string",
				  "namespace" => "variant"
				  )
			  )
		  )
	  );

Just to be clear, this does create the Metafield without any issues, they just don’t display on the front end. Has anyone else seen an issue similar to this?

There might be a problem with the metafield type. I noticed that you’re using a deprecated “value_type” parameter instead of the correct one – “type” (link). Since you’re not specifying the correct “type” attribute, Shopify might create a metafield with the default type. An OS 2.0 text field, for instance. In this case, your old Liquid code won’t work. The thing is that the OS 2.0 and legacy metafields have slightly different behavior in terms o Liquid usage. While you can display a legacy metafield value with the code like this:

{{ resource.metafields.namespace.key }}

this code would return a metafield object (which can’t be displayed on the product page), so you have to use another code pattern:

{{ resource.metafields.namespace.key.value }}

You may want to have a closer look at the metafield created by your code and the one created using the default Shopify functionality and make sure that namespace, key and type have the exact same values.

Thanks, this is very helpful.

1 Like