0

I have created a filter to get product bellow is an examples:

$filter = array(
  array('attribute' => 'color', 'eq' => '2'),//id of option
  array('attribute' => 'size', 'eq' => '5'),// tried with many variations 'like', '=', 'in'
  array('attribute' => 'length', 'eq' => '8')
)

$productCollection->addAttributeToSelect('*')
            ->addAttributeToFilter($filter)
            ->load()->getItems();

The above in the front end the selection is:

enter image description here

But my first result is -> enter image description here

I am only able to see exact matching filter somewhere down to the 10th result. not ideal.

How can I get just the exact match result? in this example I should only get product that has

color:blue, size:2 length:30

Thanks.

1 Answer 1

1

Ok, There is another way which gives exact result explained here:

Filtering results using LIKE

code taken from above link:

$collection->addAttributeToFilter('name', array(
 array('like' => '% '.$needle.' %'), //spaces on each side
 array('like' => '% '.$needle), //space before and ends with $needle
 array('like' => $needle.' %') // starts with needle and space after
));

But my case is a foreach loop so the final result using the example above is:

...
   $productCollection->addAttributeToSelect('*');
     foreach ($filter as $key=>$value){
       $productCollection->addAttributeToFilter($key, 
           array('like' => '%'.$value.'%'));
        };
   $productCollection->load()->getItems();
...

NOTE: no need extra space for me needed as mentioned!

This work as a treat!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.