As a Shopify/Next.js dev, I know how essential it is to show users how many products match their search or filter—seems basic, right? But as I found out building headless e-commerce with Shopify, Shopify's Storefront API has more quirks than you might expect, especially when it comes to product counts on search and collection pages.
Here's a battle-tested guide (plus my personal "aha" moments) for getting accurate product counts with Shopify's Storefront API.
Why Show Product Counts?
Clear product counts make filtering and pagination user-friendly. Customers expect to know “how many red shirts are there?,” and, “how many total products?” Getting that number right also helps with page navigation and lets your UI show helpful filter counts like “Red (4), Blue (7).”
But here’s where things get tricky, Shopify Storefront API provides product counts differently depending on which query you use.
Types of Queries: Search vs. Collection
After a lot of trial and error, I discovered there are two main ways to get products from Shopify’s API.
Search Query: For searching all products, whether the user is using a global search bar or keyword search.
Collection Query: For browsing products inside a specific collection, like /collections/jackets.
Depending on which one you use, you get product counts in totally different ways.
Use Case 1: Search Query – The Straightforward Way ✅
If you’re building a typical search page, the search query is the right tool for the job. Shopify gives you a totalCount field, which makes product counts simple and reliable.
GraphQL Example:
graphql
query SearchProducts($query: String!$productFilters: [ProductFilter!]$first: Int
$after: String
){
search(query:$querytypes: PRODUCT
productFilters:$productFiltersfirst:$firstafter:$after){
totalCount
productFilters {
id
label
type
values {
id
label
count
}}
edges { node {...}}
pageInfo { hasNextPage endCursor }}}
If you’re not already using ProductFilter with the search query, I highly recommend switching. I cover this in detail in this article. The short version is: ProductFilter lets you request exactly the options (like color, size, etc.) you need. For search queries, the API returns an accurate totalCount and counts for each filter value, so your filtering UI can always tell the user “Red (3), Blue (9),” and so on. This makes everything easier for both you and your users.
Use Case 2: Collection Query – The Slightly Tricky Way
Collection pages use the collection query. This is where counts can get a little confusing. The response does not include a native totalCount. That means you’ll have to calculate the full number yourself.
Example Collection Query:
graphql
query GetCollectionProducts($handle: String!$productFilters: [ProductFilter!]$first: Int
$after: String
){
collection(handle:$handle) {
products(filters:$productFiltersfirst:$firstafter:$after){
filters {
id
label
type
values { id label count }}
edges { node {...}}
pageInfo { hasNextPage endCursor }}}}
The Confusing Bit
While building my client’s store, I noticed something odd. When searching for products inside a collection, my product count would sometimes be off by just a few—like 2 or 5 products. The total wasn’t way off, but it was enough to feel weird. It took me some digging to realize that you have to be careful which filter you use for your totals, and you need to deduplicate some responses. This only happened when searching within a collection, never with global search.
How I Calculate the Collection Count
First, always deduplicate filter values. Shopify’s response can have duplicates, which will cause the sum to be slightly too high. Then, for accuracy, don’t just sum the first filter; prefer "product type" if available, and "availability" as a backup.
Use the search query and ProductFilter when possible. This always gives the most accurate product counts.
When working with collection queries, calculate the total from filter data, use product type or availability filters, and always deduplicate.
If your totals are off by a small number, it’s probably a deduplication or variant-counting issue.
Never add up the variant filters for your total product count (like color, size, etc.), because you’ll get numbers that are slightly too high.
Cursor-based pagination in Shopify means you can’t jump to pages based on index, so always use the count to drive your display, but stick to what the cursor gives you for navigation.
Once you understand these differences, Shopify product counts become easy and can power great user experiences.