Solved

How to get all product variants metafields

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

 

Accepted Solutions (2)

srdjan
Shopify Staff (Retired)
7 1 2

This is an accepted solution.

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

To learn more visit the Shopify Help Center or the Community Blog.

View solution in original post

Gregarican
Shopify Partner
1033 86 285

This is an accepted solution.

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.

View solution in original post

Replies 12 (12)

Gregarican
Shopify Partner
1033 86 285

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

 

tien-luckyfor
Tourist
6 0 0

@Gregarican 

THanks for your helping, 

This work for me!

srdjan
Shopify Staff (Retired)
7 1 2

This is an accepted solution.

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

To learn more visit the Shopify Help Center or the Community Blog.

tien-luckyfor
Tourist
6 0 0

Hi @srdjan ,

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

 

https://youtu.be/_NcPX0r3NNk

 

Gregarican
Shopify Partner
1033 86 285

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

Gregarican
Shopify Partner
1033 86 285

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

tien-luckyfor
Tourist
6 0 0

@Gregarican 

Gregarican
Shopify Partner
1033 86 285

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. 

tien-luckyfor
Tourist
6 0 0

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

 

Gregarican
Shopify Partner
1033 86 285

This is an accepted solution.

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.

tien-luckyfor
Tourist
6 0 0

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

AvidBrio
Shopify Expert
295 17 29

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

If you find our comment helpful, hit the like button and accept it as a solution.
Want us to implement custom changes in your store? Contact us
Email me directly - jim@avidbrio.com