Magento 2: Product Repositories, Filter Groups, and `AND`
Actual annotation of \Magento\Framework\Api\Search\FilterGroup
says (class phpDoc):
Groups two or more filters together using a logical OR
It means that you need to create two groups with one filter in each.
In Magento 2 all the filters in same FilterGroup
will be added using OR
operator. But all the FilterGroup
will be added using AND
operator. So you will need to add multiple FilterGroup
s as below:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$searchCriteria = $objectManager->create('\Magento\Framework\Api\SearchCriteria');
$filter = $objectManager->create('\Magento\Framework\Api\Filter');
$filter->setField('category_id');
$filter->setValue(array(1, 2, 3));
$filter->setConditionType('in');
$filterGroup = $objectManager->create('\Magento\Framework\Api\Search\FilterGroup');
$filterGroup->setFilters([$filter]);
$filterEnabled = $objectManager->create('\Magento\Framework\Api\Filter');
$filterEnabled->setField('status');
$filterEnabled->setValue(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
$filterEnabled->setConditionType('eq');
$filterGroupEnabled = $objectManager->create('\Magento\Framework\Api\Search\FilterGroup');
$filterGroupEnabled->setFilters([$filterEnabled]);
$searchCriteria->setFilterGroups([$filterGroup, $filterGroupEnabled]);
In further details and logical combinations about search criteria, you can find at Magento-2 Search Criteria