Filtering products by availability using storefront API doesn't work

Hi there,

Using the storefront API, I am trying to fetch the products of a given collection that are available for sale. I am referring to this query: collectionByHandle - Storefront API

The documentation here appears to suggest that filters.available can be passed in as a filter.

This is what my query looks like:

query getProductsByCollectionHandle(
  $handle: String!
  $first: Int!
  $cursor: String
  $sortKey: ProductCollectionSortKeys
  $reverse: Boolean
  $filters: [ProductFilter!]
) {
  collectionByHandle(handle: $handle) {
    ...CollectionFields
    products(
      first: $first
      after: $cursor
      sortKey: $sortKey
      reverse: $reverse
      filters: $filters
    ) {
      edges {
        node {
          ...ProductFields
        }
      }
      pageInfo {
        ...PaginationFields
      }
    }
  }
}

If I try querying using filter.available set to true, I get products back where availableForSale is set to false – which in theory, shouldn’t happen, unless I am misunderstanding what this filter is actually filtering out?

Any help would be greatly appreciated!

Hi, sorry for the delay in response. collectionByHandle is deprecated. Could you please try the collection() query instead. For example:

{
  collection(handle: "unisex") {
    products(
      first: 250
      filters: [{available: true}]
    ) {
      edges {
        node {
          title
          availableForSale
        }
      }
    }
  }
}
1 Like

Thanks @Bret-Shopify! I gave this a go, but still doesn’t seem to be working.

Here’s what my query now looks like:

query getProductsByCollectionHandle(
  $handle: String!
  $first: Int!
  $cursor: String
  $sortKey: ProductCollectionSortKeys
  $reverse: Boolean
  $filters: [ProductFilter!]
) {
  collection(handle: $handle) {
    ...CollectionFields
    products(
      first: $first
      after: $cursor
      sortKey: $sortKey
      reverse: $reverse
      filters: $filters
    ) {
      edges {
        node {
          ...ProductFields
        }
      }
      pageInfo {
        ...PaginationFields
      }
    }
  }
}

We were using the 2024-10 version of the API – I tried switching to 2025-01, but didn’t make any difference. I also tried passing in available: false (expecting only sold out items to be returned) and that didn’t seem to have any effect either.

This is an example of what the variables look like:

{
  "handle": "sleep",
  "first": 1,
  "sortKey": "BEST_SELLING",
  "reverse": false,
  "cursor": null,
  "filters": {
    "available": false
  }
}

This returns a product with a availableForSale prop set to true.

Any other ideas?

Do you have the Search & Discovery app installed and configured?

@Bret-Shopify ah interesting – we do, but ‘availability’ isn’t one of the configured filters. This is what it currently looks like. I’ll try adding this in and see if that does the trick – will get back to you shortly. Thank you for the quick reply and help!

1 Like

@Bret-Shopify that seems to have done the trick – thank you!

1 Like