2

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;
    }
6
  • Any updates on this. Jan 8, 2018 at 11:25
  • @PushpendraSingh i didn't get any fix, still my issue exist, may i know whether u have any idea ? Jan 9, 2018 at 11:03
  • I am facing same issue, unfortunately currently I have no Idea. Jan 9, 2018 at 13:06
  • Hi @PushpendraSingh i am also trying same from my end, if u found any solution or fix please share me thanks Jan 9, 2018 at 14:28
  • Any updates on this Jan 18, 2018 at 6:43

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.