Magento Forum

   
Using OR in a product collection filter. 
 
dereklittler
Jr. Member
 
Total Posts:  3
Joined:  2013-01-15
 

Suppose I have four conditions cond_a, cond_b, cond_c and cond_d. I need to introduce the following into the WHERE clause :

WHERE (cond_a AND cond_b) OR (cond_c AND cond_d)

The only way I have found to do this is as follows:

$collection->addAttributeToFilter(cond_a)->addAttributeToFilter(cond_b);
$condition->getSelect()->orwhere(1,1);
$collection->addAttributeToFilter(cond_c)->addAttributeToFilter(cond_d);

this produces the SQL

WHERE cond_a AND cond_b OR (1) AND cond_c AND cond_d

which is logically equivalent and works correctly, but is there a neater way to achieve the required result?
Thanks for any suggestions

 
Magento Community Magento Community
Magento Community
Magento Community
 
dereklittler
Jr. Member
 
Total Posts:  3
Joined:  2013-01-15
 
IndiesWebs - 01 May 2013 07:13 PM

Hi,
You have to filter your collection based on the different condition. For your problem you may use “In” or ‘Not In’ for the collection. Please see the following link:
http://fishpig.co.uk/blog/addattributetofilter-conditionals-in-magento.html

Hope this will help you.

Thanks for your reply, but your suggestion won’t work in this case.

The conditions I am working with are something like this (but more complicated):

WHERE (`length` >= 2 AND `width` <= 6) OR (`width` >= 2 AND `length` <= 6)

(so I’m looking for something within a particular size range, but I don’t care which orientation it is in)

 
Magento Community Magento Community
Magento Community
Magento Community
 
IndiesWebs
Sr. Member
 
Avatar
Total Posts:  189
Joined:  2013-09-18
 

Hi,
So you need to perform this code under a specific size range. For your help, refer the code below.

$productcollection Mage::getModel('catalog/product')->getCollection()
->
addFieldToFilter('length',array('lteq' => 6)) 
->
addFieldToFilter('length',array('gteq' => 2))

Hope this is now what you want.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top