I could filter a product collection by (attribute A: condition A OR condition B) AND (attribute B: condition C OR condition D) with the following codes:

$collection->addAttributeToFilter('attribute A', [
  [condition here...], //e.g. ['neq' => 'x']
  [condition here...] //e.g. ['gt' => 'y']
$collection->addAttributeToFilter('attribute B', [
  [condition here...],
  [condition here...]

But I have no idea how to achieve this: (attribute A: condition A AND condition B) OR (attribute B: condition C and condition D)

I have also tried to use search criteria builder, but still no luck so far.

  • Try like: $collection->addAttributeToFilter('attribute A', [ [condition here... , condition here...], ]); – fmsthird May 31 at 7:50
  • @magefms The conditions A and B I want would be something like ['gt' => 'xxx'] and ['neq' => 'yyy']. The syntax you provided seems not working. – d.yuk May 31 at 8:45
  • why not try like $collection->addAttributeToFilter('attribute A', [ [condition here...] ]); $collection->addAttributeToFilter('attribute A', [ [condition here number 2...] ]); – fmsthird May 31 at 8:49
  • creating two filter for the same attribute that might work – fmsthird May 31 at 8:49
  • can you try like $collection->addAttributeToFilter( array( array(array('attribute' => 'attribute A', 'neq' => 'x'), array('attribute' => 'attribute A', 'gt' => 'y')), array(array('attribute' => 'attribute B', 'neq' => 'x'), array('attribute' => 'attribute B', 'neq' => 'x')) ) ); – fmsthird May 31 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.