How to addAttributeToFilter AND and OR at the same time?

1.5k Views Asked by At

I'd like to make filter which looks like this :
(pseudo code)

SELECT * FROM Products WHERE 
   (`attribute_foo` LIKE 'bar' AND `attribute_bar` LIKE 'foo')
OR (`attribute_far` LIKE 'boo');

I know how to construct OR condition by providing an array, but not the both.

thanks you

2

There are 2 best solutions below

0
On
"from"=>$fromValue, "to"=>$toValue
"like"=>$likeValue
"neq"=>$notEqualValue
"in"=>array($inValues)
"nin"=>array($notInValues)
"eq"=>$equal
"nlike"=>$notlike
"is"=>$is
"gt"=>$greaterthan
"lt"=>$lessthan
"gteq"=>$greterthanequal
"lteq"=>$lessthanequal
"finset"=>$unknown
"date"=>true, "to" => $now

$collection = Mage::getResourceModel('customer/customer_collection')
        ->addNameToSelect()
        ->addAttributeToSelect('email')
        ->addAttributeToSelect('created_at')
        ->addAttributeToSelect('group_id')
        ->addFieldToFilter('entity_id', array('from'=>20, 'to'=>30))
        ->addFieldToFilter('name', array('like'=>'%cindy%'));
0
On

You can use Magento data object to filter:

$product = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToFilter("attribute_code", $value);

print_r($product->getItems());