Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

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
Magento Community
Magento Community
Back to top