ProductCountQuery
Description
Returns the number of products matching the specified criteria. This query is useful for pagination or when you need to know the total count of products without fetching the actual product data.
Endpoint
query productCount($handle: [QueryArgument], $yuiId: [QueryArgument], $price: [QueryArgument], $type: [String]) {
productCount(handle: $handle, yuiId: $yuiId, price: $price, type: $type)
}
Arguments
| Argument | Type | Required | Description |
|---|---|---|---|
| handle | [QueryArgument] | No | Narrows the query results based on the handle on the product. |
| yuiId | [QueryArgument] | No | Narrows the query results based on the YUI ID on the product. |
| price | [QueryArgument] | No | Narrows the query results based on the price on the product. |
| price_incl_tax | [QueryArgument] | No | Narrows the query results based on the price including tax on the product. |
| price_excl_tax | [QueryArgument] | No | Narrows the query results based on the price excluding tax on the product. |
| specialPrice | [QueryArgument] | No | Narrows the query results based on the special price on the product. |
| special_price_incl_tax | [QueryArgument] | No | Narrows the query results based on the special price including tax on the product. |
| special_price_excl_tax | [QueryArgument] | No | Narrows the query results based on the special price excluding tax on the product. |
| type | [String] | No | Narrows the query results based on the product type the products belong to per the product type's handles. |
Common Element Arguments
In addition to the specific arguments above, this query also supports common element arguments:
| Argument | Type | Required | Description |
|---|---|---|---|
| id | [QueryArgument] | No | Narrows the query results based on the element's ID. |
| uid | [String] | No | Narrows the query results based on the element's UID. |
| status | [String] | No | Narrows the query results based on the element's status. |
| title | [String] | No | Narrows the query results based on the element's title. |
| slug | [String] | No | Narrows the query results based on the element's slug. |
| siteId | Int | No | Determines which site to fetch results from. |
Return Values
The query returns an integer representing the total number of products that match the specified criteria.
Usage Example
query {
productCount(type: ["simple"], price: [">", 10])
}
Example Response
{
"data": {
"productCount": 42
}
}
Notes
- The
QueryArgumenttype allows for various comparison operators. For example, you can useprice: [">", 10]to count products with a price greater than 10. - You can combine multiple arguments to narrow down the count further.
- This query is particularly useful for implementing pagination in your frontend application.
- Unlike the
productsquery, this query does not support thelimitandoffsetarguments, as it always returns the total count. - This query uses the same filtering arguments as the
productsquery, so you can use the same filters to get a count of products that would be returned by a correspondingproductsquery.