Search code examples
shopware6shopware6-api

Shopware 6: how to prove if product has variants


If I call a product, I will check if the product has variants.

Currently, I make it with an additional request using product.id

const criteria = new Criteria();
criteria.setLimit(1);
criteria.addFilter(
    Criteria.equals('product.parentId', this.product.id)
);
this.productRepository
    .search(criteria)
    .then((result) => {
        ...
    });

I can't find a suitable attribute (field) like e.g. product.hasVariant.

I think this is a simple parent:child relationship. I'm wondering if the only way to achieve this goal is in its children (which have the product.parentId field).

Question: — Is there an attribute, which define, if product has variants? — … or how to define, if a product has variants


Solution

  • Yes, there is a field called childCount.

    JS:

    criteria.addFilter(Criteria.range('childCount', { gt: 0 }));
    

    API:

    {
        "filter": [
            {
                "type": "range",
                "field": "childCount",
                "parameters": {
                    "gt": 0
                }
            }
        ]
    }