Solved

Not able to retrieve list of metafields

yagnesh97
Tourist
3 0 1

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.

https://shopify.dev/api/admin-rest/2021-10/resources/metafield#[get]/admin/api/2021-10/metafields.js...

Could you please help me here?

Accepted Solution (1)
lastiri
Shopify Staff
6 4 3

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.

 

31-30-l71kw-ic1fx.png

 

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.

 

31-40-uoysp-ahn52.png

 

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>

 

31-46-fbnuh-m1bxv.png

 

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

 

 

31-56-ihnta-i9po6.png

 

Hopefully that clarifies your question. Let me know if you have any following questions. 

 

Rafa | 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 the Shopify Help Center  or the Shopify Blog 

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

View solution in original post

Replies 6 (6)

lastiri
Shopify Staff
6 4 3

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

yagnesh97
Tourist
3 0 1

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": []
}

 

lastiri
Shopify Staff
6 4 3

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.

 

31-30-l71kw-ic1fx.png

 

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.

 

31-40-uoysp-ahn52.png

 

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>

 

31-46-fbnuh-m1bxv.png

 

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

 

 

31-56-ihnta-i9po6.png

 

Hopefully that clarifies your question. Let me know if you have any following questions. 

 

Rafa | 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 the Shopify Help Center  or the Shopify Blog 

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

yagnesh97
Tourist
3 0 1

Hey @lastiri,

 

Thanks a lot for sharing the solution. I appreciate your efforts.

automaticaJP
New Member
5 0 0

Hi @lastiri 
Having trouble setting and retrieving metafield values.


Am I understanding the behavior of Metafields when I say

  • 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 on => 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 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"

 

enessahin
Shopify Partner
4 0 1

@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.