Magento – addFieldToFilter() with OR Condition in magento2

attributescollection;filtermagento2

$collectionData = $this->_productCollectionFactory->create()->addAttributeToSelect('*')
    ->addFieldToFilter(['attribute1', 'attribute2'],
        [
            ['in' => $attribute1],
            ['nin' => $attribute2],
        ]);

I want to get collection based on "OR condition" on three attribute for example a product which has color yellow or size small but not manufacture china.

Best Answer

I assume that in your example, yellow, small and china are options in dropdowns.
You will need their ids first, but let's assume the are $yellowId, $smallId and $chinaId.

You can try this filtering.

->addFieldToFilter(['color', 'size', 'country'],
    [
        ['eq' => $yellowId],
        ['eq' => $smallId],
        ['neq' => $chinaId]
    ]);
Related Topic