How to get all product variants metafields

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

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

An alternative to @Gregarican '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
            }
          }
        }
      }
    }
  }
}
1 Like

@Gregarican

THanks for your helping,

This work for me!

Hi @srdjan ,

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

https://youtu.be/_NcPX0r3NNk

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…

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!

Hi @Gregarican

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/td-p/651540 for details.

1 Like

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

If you need to retrieve a large amount of records using GraphQL, it’s usually best to try bulk operations → https://shopify.dev/tutorials/perform-bulk-operations-with-admin-api. I did a test of this, and pulled over 49K products using this mechanism. And the results were available in 11 minutes. Using the REST API it would’ve taken awhile to iterate through each paginated query response.

2 Likes

@Gregarican wow amazing,

Actualy until now I’ve just know these thing,

I tested on my store with 46k products and get product info, product seo, product metafields, variant info and variant metafields.

The result so amazing It’s take about 29 minutes with jsonline file (45mb).

Thanks you so much! That is everything I need./

@Gregarican Thanks for your answer. I have one question about product variants metafields in shopify theme liquid, i get the all meta filed but i want show only the active variants meta field.