How to get all product variants metafields

Solved
Highlighted
Tourist
6 0 0

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
    }
  }
}

 

0 Likes
Highlighted
Shopify Partner
661 46 132

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
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

 

Highlighted
Shopify Staff
Shopify Staff
7 1 2

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
            }
          }
        }
      }
    }
  }
}
Tourist
6 0 0

@Greg_Kujawa 

THanks for your helping, 

This work for me!

0 Likes
Highlighted
Tourist
6 0 0

Hi @srdjan ,

As screen record it's response empty, Is that right?

 

https://youtu.be/_NcPX0r3NNk

 

0 Likes
Highlighted
Shopify Partner
661 46 132

I haven't defined any product variant metafields, so I didn't see anything coming back in the response. If I get a chance today I'll add some metafield values and see what comes back...

0 Likes
Highlighted
Shopify Partner
661 46 132

Just ran a quick test in the GraphiQL app. Added a metafield value to a product variant and then queried against it. It seemed to work fine. Specifics below!

Metafield Add.jpgMetafield Query.jpg

0 Likes
Highlighted
Tourist
6 0 0

@Greg_Kujawa 

0 Likes
Highlighted
Shopify Partner
661 46 132

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. 

Highlighted
Tourist
6 0 0

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

 

0 Likes