3

In Magento2 API, how would I filter by multiple categories?

V1/products?searchCriteria[filterGroups][0][filters][0][field]=name&searchCriteria[filterGroups][0][filters][0][value]=%test%&searchCriteria[filterGroups][0][filters][0][conditionType]=like&searchCriteria[sortOrders][0][field]=created_at&searchCriteria[sortOrders][0][direction]=DESC&searchCriteria[filterGroups][1][filters][1][field]=category_id&searchCriteria[filterGroups][1][filters][1][value]=105,106,107&searchCriteria[filterGroups][1][filters][1][conditionType]=eq&searchCriteria[pageSize]=8&searchCriteria[currentPage]=1

When the category ids are 105,106,107 I am getting results, but if the category ids are 107,106,105 I am getting empty items.

How can I search for different categories in Magento2 API?

2 Answers 2

0
baseurl/rest/default/V1/products/products
searchCriteria[currentPage]=1&searchCriteria[pageSize]=1&
searchCriteria[filter_groups][0][filters][0] 
[field]=category_id&searchCriteria[filter_groups][0][filters][0][value]= 
[8,4,2]&searchCriteria[filter_groups][0][filters][0][condition_type]=in

For multiple values need to use in please refer below devdocs 
https://devdocs.magento.com/guides/v2.3/rest/performing-searches.html

**Note**: In your search criteria it ill take first category id only.
2
  • If it takes only first category then it will be a problem. If first category not having any products then we will get the empty results . same happening in my case as well. Any solution for this?
    – Dinesh
    Commented Feb 25, 2019 at 8:55
  • If you use eq it will happen like that yous should use in while you are using multiple categories Commented Feb 25, 2019 at 12:34
0

Example with Guzzle:

        $response = $this->client->get('/rest/V1/products', [
        'search_criteria' => [
            'current_page' => $currentPage,
            'page_size' => $pageSize,
            'filter_groups' => [
                [
                    'filters' => [
                        [
                            'field' => 'category_id',
                            'value' => '23356,23355',
                            'condition_type' => 'in'
                        ]
                    ]
                ]
            ]
        ]
    ]);

Your Answer

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

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