Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: How to set default value to metafields : ID won't be given unless you fill the field on UI??

How to set default value to metafields : ID won't be given unless you fill the field on UI??

automaticaJP
New Member
5 0 0

Having trouble setting and retrieving metafield values.


Am I understanding the behavior of Metafields as below?

  • ID of a metafiled is unique to each product and not to each metafield?
  • In other words IDs are not common in all the products even for the same key fields?
  • Edit Metafield that's empty => ID will be given then for the first time?

I wanted to programatically know if a product is sold for the first time ever and decided to create a metafield

 

 

{
"key":"ever_sold",
"type:boolean"
}

 

 

and keeping it true after one is sold otherwise false or empty.

I manually filled this field in a product on UI and did

 

 

GET: products/{product_id}/metafiels.json

 

 

then saw

 

 

{
 "id":22414790557925,
 "key":"ever_sold",
 "type":"boolean",
 ...
}

 

 

So I did

 

 

PUT: products/{product_id}/metafiels/22414790557925.json
{"metafiled":{"value":true}}

 

to all the products and they all came through so I thout this was good.

 

But 22414790557925 won't be listed up with

 

 

GET: products/{product_id}/metafiels.json

 

 

then I realized metafield IDs are not common among products.
BUT! if I do

 

 

GET: products/{product_id}/metafiels/22414790557925.json

 

to any products it gave me back

 

 

"key":"ever_sold"
"value":true
"type:"boolean"

 

but on UI the field is empty. 

 

I am wondering what is the best practice here.


I want my matafield to be filled with `false` as a default value when created.
Mannually filling is not an option to guarantee it and this shop has many new products weekly.

My initial solution is to Watch product.created and do 

 

PUT: products/{product_id}/metafiels/22414790557925.json
{"metafiled":{"value":false}}

 

to all the products anyway. I'm ignoring the fact that this won't show false on UI's product page.

 

Replies 7 (7)

stratify
Shopify Staff (Retired)
20 2 14

Hey there! Strat here from Shopify : )

 

First of all, adding a default value to metafield definitions is a great idea! I've taken note of your feedback and I'll make sure it gets passed onto the right team.

 

In the mean time, Shopify Webhooks is a great use-case for default values. You can add a value to the metafield when a product is created, and updated the value when a product is sold. For any Plus merchants reading this post, you can also use Shopify Flow to accomplish the same workflow without code.

 

Regarding the resource UI, only pinned metafield definitions will show up on the resource page. Ensure that you've setup a metafield definition and the definition has been pinned. The value will be blank until you've added a value.

 

Finally, I want to point you towards this forum post for your questions around metafield IDs. I hope that's helpful!

To learn more visit the Shopify Help Center or the Community Blog.

automaticaJP
New Member
5 0 0

@stratify 
Hi Strat,
Thank you for your reply.


@stratify wrote:

 

In the mean time, Shopify Webhooks is a great use-case ... You can add a value to the metafield when a product is created, .. For any Plus merchants reading this post, you can also use Shopify Flow


I've gone through all you suggested already and yes I do use product.created webhook to add a value to my metafield. Workflow is not available for my client.

I need a metafield  ID to add a value there, don't I?
How do I know its ID before filling any values from UI?
Because listing metafileds didn't give me this filed's ID before filling some value from UI.

Although as I wrote in my post I did

 

 

PUT: products/{product_id}/metafiels/22414790557925.json
{"metafiled":{"value":true}}
#As far as I understand ID is not commonly shared.
#Even so, this added boolean type value to all the products.

 

to all the products which all came through without erros.

Are you saying if via API I should add a metafield itself to all the products created and then update its value?
Setting metafields and pinning them from UI won't actually set them to products?

Finally, I want to point you towards this forum post for your questions around metafield IDs. I hope that's helpful!


This is a link to my question...

stratify
Shopify Staff (Retired)
20 2 14
This is a link to my question...

 

I apologize! I've updated my comment to link to the correct answer about metafield IDs.

 

I need a metafield  ID to add a value there, don't I?

 

Metafields (like all other Shopify resources) do not have an ID until they have been created. The ID that you're using above is a definition ID.

 

Are you saying if via API I should add a metafield itself to all the products created and then update its value?

 

My suggestion would be to create a metafield definition using the boolean type. This will not add metafield values to products. It will provide validation for values that are added as metafields (to the namespace and key) in the future. The definition will also show the metafield on the product UI.

 

 Setting metafields and pinning them from UI won't actually set them to products?

 

Only pinned metafield definitions will show up on the product page UI. 

To learn more visit the Shopify Help Center or the Community Blog.

automaticaJP
New Member
5 0 0

Hi, @stratify 
Thank you for your response.

 

Metafields (like all other Shopify resources) do not have an ID until they have been created. The ID that you're using above is a definition ID.


I'm sorry I feel like I'm repeating myself but
it was a unique ID I found in the list of metafields with

 

 

 

 

 

GET: products/{a product's id}/metafiels.json

 

 

 

 

after I manually filled the targeted first_sold metafield of a product  on UI resource.


NOT definition ID.

Which means 


My suggestion would be to create a metafield definition using the boolean type. This will not add metafield values to products. It will provide validation for values that are added as metafields (to the namespace and key) in the future. The definition will also show the metafield on the product UI.

 

 Setting metafields and pinning them from UI won't actually set them to products?

 

Only pinned metafield definitions will show up on the product page UI. 


the definition has been created and pinned from the very first place.

 

It also makes me feel the API's behavior is strange because my request of updating with that unique ID to all other products came through.
PUT: products/{product_id}/metafiels/22414790557925.json {"metafiled":{"value":true}}

(looped through all product ids existing)

I think decent way here to update(initially fill) metafields to all products is to;

  1. list metafields
  2. grab id by namespace then
  3. use it on endpoint path for "update metafield" request since ID is unique to each product.

But like I said it won't be listed with 
`/products/<id>/metafields.json`
as long as it's empty = until I fill something, only after I filled the field from UI resource.

Charlie-SLA
Tourist
7 0 4

Hi @automaticaJP ,

I was just wondering if you have worked out some way to add default values to meta field?


I am having similar issues, I need to know the meta field ID in advance of editing it, but there is no ID until it has a value, hence I want to automate and add a default value first so I can update it 🙂

automaticaJP
New Member
5 0 0

@Charlie-SLA 
PUT: products/{product_id}/metafiels/22414790557925.json {"metafiled":{"value":true}}

To all products came through.
Then if I call 

GET: products/{product_id}/metafiels/22414790557925.json
It returns {"value":true} with the same type and field name although this won't be in a list of metafields. = GET: products/{a product's id}/metafiels.json

Strange still but it surved my purpose anyway.

AmoghRaghuram93
Shopify Partner
2 0 0

Hi, 

 

I am still facing an issue where when I create a new metafield definition, view the definition on the dashboard it's present. But when I retrieve a list of all metafields present for a customer I still don't see the metafield but is present in the dashboard. Only when I alter the value in the dashboard do I get a metafield id for that particular customer. 

 

Is there anyway I can retrieve a metafield id for each customer once I create a metafield definition without altering any values in the dashboard?