Solved

Storefront API - filtering on product variant color

dev88
Tourist
11 0 6

Hi,

 

How can I use GraphQL to retrieve all products with a specific variant option? (i.e. "color=red")?

 

(in the Storefront API)

Accepted Solution (1)

hassain
Shopify Staff (Retired)
624 104 188

This is an accepted solution.

Hi @dev88 ,

 

When using the Storefront API, you can filter for specific products by passing in the “query” argument (along with the argument of “first” or “last”)  while querying the ‘products’ connection. Through this “query” argument, you can use Shopify’s search syntax to filter by the following supported filter parameters in the ‘products’ connection:

  • available_for_sale
  • created_at
  • product_type
  • tag
  • title
  • updated_at
  • variants.price
  • vendor

You can read more about the supported filter parameters of the “query” argument in the ‘products’ connection here.

 

You are unable to filter by varaints.title (or any other variants sub-field other than variants.price), so you cannot directly filter products by a specific variant option like ‘color=red’. As a workaround however what you could do instead is add the tag “Red” to your products that have a variant with the 'color=red'. (Read more about how to add tags to a product here). Then you can use the following GraphQL query to get all of your products with a variant that has the option for the color red:

 

query {
  products (first: 10, query: "tag:Red" ) {
    edges {
      node {
        id
        availableForSale
        title
        tags
        variants (first: 10) {
          edges {
            node {
              id
              title
            }
          }
        }
      }
    }
  }
}

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

View solution in original post

Replies 2 (2)

hassain
Shopify Staff (Retired)
624 104 188

This is an accepted solution.

Hi @dev88 ,

 

When using the Storefront API, you can filter for specific products by passing in the “query” argument (along with the argument of “first” or “last”)  while querying the ‘products’ connection. Through this “query” argument, you can use Shopify’s search syntax to filter by the following supported filter parameters in the ‘products’ connection:

  • available_for_sale
  • created_at
  • product_type
  • tag
  • title
  • updated_at
  • variants.price
  • vendor

You can read more about the supported filter parameters of the “query” argument in the ‘products’ connection here.

 

You are unable to filter by varaints.title (or any other variants sub-field other than variants.price), so you cannot directly filter products by a specific variant option like ‘color=red’. As a workaround however what you could do instead is add the tag “Red” to your products that have a variant with the 'color=red'. (Read more about how to add tags to a product here). Then you can use the following GraphQL query to get all of your products with a variant that has the option for the color red:

 

query {
  products (first: 10, query: "tag:Red" ) {
    edges {
      node {
        id
        availableForSale
        title
        tags
        variants (first: 10) {
          edges {
            node {
              id
              title
            }
          }
        }
      }
    }
  }
}

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

dev88
Tourist
11 0 6

Thanks for your detailed answer, @hassain.