Hi everyone, I'm new here..
So I've just research how to get all product metafields via grapql. And my new task is get all product variants metafields.
I trying to replace "product" in these commands to "variant" but still doesn't work anymore.
Can you help me to give great solution, Thanks for read my question.
// get all product metafields
query allProductsMetafields($cursor: String) {
products(first: 30, after: $cursor) {
edges {
cursor
node {
legacyResourceId
metafields(first: 30) {
edges {
node {
namespace
value
key
}
}
}
}
cursor
}
pageInfo {
hasNextPage
hasPreviousPage
}
}
}
Solved! Go to the solution
If you are looking for any metafields at the variant level then you need to iterate through products, and then drill into the variants to get to them. Like this stripped-down example below. I just pull the first 5 products, iterate through each product's first 5 variants, and finally iterate through each variant's first 5 metafields. Does this work for you?
{
products(first: 5) {
edges {
node {
id
title
variants(first: 5) {
edges {
node {
id
title
metafields(first: 5) {
edges {
node {
description
value
valueType
}
}
}
}
}
}
}
}
}
}
This is an accepted solution.
An alternative to @Greg_Kujawa's solution, which is perfectly fine, is to query product variants from the root level.
{
productVariants(first: 10) {
edges {
node {
metafields(first: 10) {
edges {
node {
namespace
key
value
}
}
}
}
}
}
}
I have 100 products variants with only 10 products variants have metafields.
How can I get these metafields in that? thanks.
I don't think you can query for only those product variants that have a specific (or any) metafield value defined. See https://community.shopify.com/c/Shopify-Discussion/Query-all-products-with-a-given-metafield-value/t... for details.
@Greg_Kujawa
Thank you so much for your response!
If I loop all products variants to get metafields that not good about performance and limit "requestedQueryCost". Right?
User | Count |
---|---|
12 | |
11 | |
10 | |
8 | |
7 |