skip to Main Content

According to this documentation:
https://shopify.dev/custom-storefronts/products/filter-products#query-products-by-type

We should be able to filter products within a collection using collectionByHandle.

I have created a very basic test query in the Shopify GraphiQL App explorer tool. When I run the documented query, it returns all products, not filtering at all. See below:

GraphQL example query

This looks like a bug with the API right? Or am I missing something basic?

3

Answers


  1. Chosen as BEST ANSWER

    OK this turned out to be a configuration issue. To allow filtering by product type, it needs to be turned on in the admin for your store. If you navigate to: Online Store > Navigation

    ... and scroll to the bottom, you will see where you can add allowed filters: enter image description here

    Even if it says your theme doesn't support filters, it will still change the way the API behaves.


  2. I have the same problem, the filters param of products query seem to be ineffective and returning me all the products in the collection.
    I can’t find the "allowed filters" option.
    Currently I’m using the Storefront API as an external app, all works fine except that.

    Here the code.

     query (
          $collectionHandle: String, $product_filters: [ProductFilter!], $nQueryElements: Int
        ) {
          collection(handle: $collectionHandle) {
            title
            products(first: $nQueryElements filters: $product_filters) {
              pageInfo {
                hasNextPage
                hasPreviousPage
              }
              edges {
                cursor
                node {
                  title
                  vendor
                  availableForSale
                  id
                  handle
                  productType
                  variants(first: 40) {
                    edges {
                      node {
                        selectedOptions {
                           name
                          value
                            }
                        title
                        compareAtPriceV2 {
                          amount
                        }
                        image {
                          id
                        }
                      }
                    }
                  }
                  priceRange {
                    maxVariantPrice {
                      amount
                    }
                  }
                  images(first: 1) {
                    edges {
                      node {
                        id
                        url(transform:  { maxWidth: 500, maxHeight: 700 })
                      }
                    }
                  }
                }
              }
            }
          }
        }
    

    const variables = { collectionHandle: this.pageURL, nQueryElements: this.nQueryElements, lastCursor: this.queryCursor.last, firstCursor: this.queryCursor.first, product__filters: [{ productVendor: "ASPESI", },], };

    Thanks to who can help.

    Login or Signup to reply.
  3. This can be done now. This post on the Shopify forum explains it perfectly with the latest API.

    In case that post gets deleted, I’m going to put the info below:

    We can now filter by metafields but through a collection. https://shopify.dev/custom-storefronts/products-collections/filter-products#query-products-by-metafi...

    Requirements:

    • The metafield must have been added as a filter in the "Search &
      Discovery app" or the Filters in the Navigation settings.
    • The metafield must be of one of these types: single_line_text_field,
      boolean, numeric_integer, numeric_decimal
    • The Storefront API used must be 2022-04 or higher. I tested it with
      2022-10
    • The metafield must be exposed to the Storefront API

    Knowing that as of today, we have a limit of 5000 products for filters to work in a normal store (see https://help.shopify.com/en/manual/online-store/search-and-discovery/filters)

    I decided to test if that restriction applies to the Storefront API, I tested it with a collection with 11769 products and I was able to get filtered results as expected. So it seems that at this stage we don’t have this limitation in the Storefront API
    .

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search