Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: Storefront API - filtering on product variant color

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 189

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 3 (3)

hassain
Shopify Staff (Retired)
624 104 189

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.

EugeneBoruhov
Shopify Partner
3 0 0

Seems like `available_for_sale` doesn't work

EugeneBoruhov_0-1724783265578.png