A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
I have created a few metafield in my development store under but when try to retrieve the list of all the metafields using the mentioned Metafield API, it doesn't show any of it.
Could you please help me here?
Solved! Go to the solution
This is an accepted solution.
Hello @yagnesh97 ,
Thanks for the clarification.
Ok, so what you have created by going through the admin is a Metafield Definition within the owner type Product.
Now that the metafield definition is created, it is time to add the corresponding metafield to each product.
You have two options to do that.
1) You can do so on your Store Admin page by going directly to a product. You'll now see a section called Metafields and your newly created metafield definition will be there. So you can set the metafield there. Make note of the product_id as you'll need it for retrieving it.
2) You can do so by either sending a request with the GraphQL API or the REST API. Since we've been talking about the REST API I'll go into details for it.
Send a request to create a new metafield for a Product resource. Notice that I provide the product_id as part of the URL.
With the metafield created under the product. You also have two options to retrieve the metafields for a particular product.
1) Send a request to general metafields endpoint
https://your-development-store.myshopify.com/admin/api/2021-10/metafields.json
With Query params:
metafield[owner_resource] = product
metafield[owner_id] = <your_product_id>
2) Send a request to retrieve metafields for a particular product
https://your-development-store.myshopify.com/admin/api/2021-10/products/<your_product_id>/metafields.json
Hopefully that clarifies your question. Let me know if you have any following questions.
Rafa | Shopify
------------------------------------------------------------------------------------------------------------------------------------------
Rafa L | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
Hello Yagnesh97,
Let me help you out here.
It really depends on how you initially created the metafield.
Was it done through the Admin Rest API? If so can you provide a snippet on how you did it?
The reason why I ask is because metafields are always attached to a particular resource.
If you didn't provide a resource when creating then it defaults to the Shop resource.
To retrieve all metafields attached to the Shop resource you can do so by sending a request to
https://your-development-store.myshopify.com/admin/api/2022-01/metafields.json
Let me know how you created your metafield and I'll be happy to provide assistance.
Rafa L | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
Hey @lastiri,
I created a Metafield named "highlights" under Settings > Metafields > Add Definition.
But when I query the below cURL request it doesn't show up.
curl -X GET "https://your-development-store.myshopify.com/admin/api/2021-10/metafields.json" \
-H "X-Shopify-Access-Token: {access_token}"
Getting the below response from the API.
{
"metafields": []
}
This is an accepted solution.
Hello @yagnesh97 ,
Thanks for the clarification.
Ok, so what you have created by going through the admin is a Metafield Definition within the owner type Product.
Now that the metafield definition is created, it is time to add the corresponding metafield to each product.
You have two options to do that.
1) You can do so on your Store Admin page by going directly to a product. You'll now see a section called Metafields and your newly created metafield definition will be there. So you can set the metafield there. Make note of the product_id as you'll need it for retrieving it.
2) You can do so by either sending a request with the GraphQL API or the REST API. Since we've been talking about the REST API I'll go into details for it.
Send a request to create a new metafield for a Product resource. Notice that I provide the product_id as part of the URL.
With the metafield created under the product. You also have two options to retrieve the metafields for a particular product.
1) Send a request to general metafields endpoint
https://your-development-store.myshopify.com/admin/api/2021-10/metafields.json
With Query params:
metafield[owner_resource] = product
metafield[owner_id] = <your_product_id>
2) Send a request to retrieve metafields for a particular product
https://your-development-store.myshopify.com/admin/api/2021-10/products/<your_product_id>/metafields.json
Hopefully that clarifies your question. Let me know if you have any following questions.
Rafa | Shopify
------------------------------------------------------------------------------------------------------------------------------------------
Rafa L | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
Hi @lastiri
Having trouble setting and retrieving metafield values.
Am I understanding the behavior of Metafields when I say
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 to a product on UI and did
GET: products/{product_id}/metafiels.json
then saw
"id":22414790557925
So I did
PUT: products/{product_id}/metafiels/22414790557925.json
{"metafiled":{"value":true}}
to all the products and they all came through.
22414790557925 won't be listed up with
GET: products/{product_id}/metafiels.json
but if I do
GET: products/{product_id}/metafiels/22414790557925.json
it gave me back
"key":"ever_sold"
"value":true
"type:"boolean"
@lastiri Is there a way to retrieve all metafields without any product or source ? All i want to do is retrieve entire metafields which shop is using or created. That is all. I can't request for all products individually to see all metafields.