Magento – Magento 2 filter products by price from Rest API

layered-navigationmagento2pricerest apisearch-criteria

Magento-2 we would like to filter the products from REST API

I have filtered product by price in the frontend please check the result in the below screenshot

(24-MB02 price and special price we set at admin end)

Screenshot:-

enter image description here

But API Level:-

the same product 24-MB02 is missing

Api Url:- /rest/V1/products/

http://10.0.0.55/ma221/rest/V1/products/?searchCriteria[filterGroups][0][filters][0][field]=category_id&searchCriteria[filterGroups][0][filters][0][value]=4&searchCriteriafilterGroups[filters][0][field]=price&searchCriteriafilterGroups[filters][0][value]=40&searchCriteriafilterGroups[filters][0][condition_type]=gteq&searchCriteriafilterGroups[filters][0][field]=price&searchCriteriafilterGroups[filters][0][value]=50&searchCriteriafilterGroups[filters][0][condition_type]=lteq

But result we are getting 4 products

enter image description here

output:-

enter image description here

Please help on this. Thanks. How to achieve this?

/vendor/magento/module-catalog/Model/ProductRepository.php

public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
    {
        /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $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');


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

        $collection->load();

        $collection->addCategoryIds();
        $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->hasData(\Magento\Catalog\Model\Product::STORE_ID) ? $product->getStoreId() : null
                    ]
                ),
                $product
            );
        }

        return $searchResult;
    }

Best Answer

This is because product 24-MB02 price is $59.0 (discount price is $40.0) - that's why it does not show in $40-50 range.

But if you filter by "special_price" or whatever your discount price field name is - it will show up.

In order to include both regular and discounted products in your filter query - do logical AND & OR search like so:

searchCriteria[filter_groups][0][filters][0][field]:price
searchCriteria[filter_groups][0][filters][0][value]:MIN_PRICE
searchCriteria[filter_groups][0][filters][0][condition_type]:gteq
searchCriteria[filter_groups][0][filters][1][field]:special_price
searchCriteria[filter_groups][0][filters][1][value]:MIN_PRICE
searchCriteria[filter_groups][0][filters][1][condition_type]:gteq
searchCriteria[filter_groups][1][filters][0][field]:price
searchCriteria[filter_groups][1][filters][0][value]:MAX_PRICE
searchCriteria[filter_groups][1][filters][0][condition_type]:lteq
searchCriteria[filter_groups][1][filters][1][field]:special_price
searchCriteria[filter_groups][1][filters][1][value]:MAX_PRICE
searchCriteria[filter_groups][1][filters][1][condition_type]:lteq

Check out this link if you need more search query examples - https://devdocs.magento.com/guides/v2.3/rest/performing-searches.html#logical-and-search

Related Topic