commercetools

How to get amount of variants across all products in a commercetools project?


How to build a query (GraphQL or regular REST endpoint) which will return the amount of variants in all products in a commercetools project?

I think facets can help, but I'm not entirely sure how.


Solution

  • The best way to get variant counts is using facets on the product projection search api. The trick is to choose a field that is available on all products. I chose variant id (facet=variants.id) however you could choose any attribute but the resulting facet may have a missing count.

    project_id/product-projections/search?staged=true&limit=0&markMatchingVariants=false&facet=variants.id
    

    response:

    "type": "terms",
    "dataType": "text",
    "missing": 0,
    "total": 12017,
    "other": 0,