Skip to content

API with price filter and configurable products

I’m working with the Magento 2 API and can’t work out why when using a price filter configurable products are being filtered out completely from the results. I remove the price filter and the products appear. I understand configurable products have a
‘price’ of 0 and stock of 0, however they are being removed somewhere along the line.

Here is the custom script in the API module we are currently using (however this is the same behaviour if you use the standard v1/products API)

{
    $categoryIds = [];
    $category_id = null;

    $filterGroups = $searchCriteria->getFilterGroups();
    foreach ($filterGroups as $filterGroup) {
        $filters = $filterGroup->getFilters();
        foreach ($filters as $filter) {
            if ($filter->getField() === 'category_id') {
                $category_id = $filter->getValue();
                $categoryIds[] = $category_id;
                break 2; // Break both loops since we found the category_id filter
            }
        }
    }

    if ($category_id !== null) {
        // Retrieve all child category IDs iteratively
        $childCategoryIds = $this->getAllChildCategoryIds($category_id);
        $categoryIds = array_merge($categoryIds, $childCategoryIds);

        $filterGroups = $searchCriteria->getFilterGroups();
        foreach ($filterGroups as $filterGroupKey => $filterGroup) {
            $filters = $filterGroup->getFilters();
            foreach ($filters as $filterKey => $filter) {
                if ($filter->getField() === 'category_id') {
                    unset($filters[$filterKey]);
                }
            }
            $filterGroups[$filterGroupKey]->setFilters($filters);
        }

        $categoryIdsFilter = $this->filterBuilder
            ->setField('category_id')
            ->setValue($categoryIds)
            ->setConditionType('in')
            ->create();

        $filterGroups[0]->setFilters([$categoryIdsFilter]);
        $searchCriteria->setFilterGroups($filterGroups);
    }

    /** @var MagentoCatalogModelResourceModelProductCollection $collection */
    $collection = $this->collectionFactory->create();
    $this->extensionAttributesJoinProcessor->process($collection);

    $collection->addAttributeToSelect('*');
    $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
    $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
    $collection->addMinimalPrice();

    $this->collectionProcessor->process($searchCriteria, $collection);

    $collection->load();

    $collection->addCategoryIds();
    $this->addExtensionAttributes($collection);
    $searchResult = $this->searchResultsFactory->create();
    $searchResult->setSearchCriteria($searchCriteria);
    $searchResult->setItems($collection->getItems());
    $searchResult->setTotalCount($collection->getSize());

    foreach ($collection->getItems() as $product) {
        $this->cacheProduct(
            $this->getCacheKey(
                [
                    false,
                    $product->getStoreId()
                ]
            ),
            $product
        );
    }

    return $searchResult;
}

I can print the collection before running the collectionProcessor and see the configurable product however it disappears once the filters are applied.

Anyone had this issue before?

One would assume that this should always return configurable products based on their minimal_price in any case but it appears it just removes them completely from the results whether or not it is a gteq or lteq query and the product fits.

Any help greatly appreciated.