cancel
Showing results for 
Search instead for 
Did you mean: 

Filter by attribute

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Filter by attribute

My client runs a online bookstore and wants books from a certain publisher to be displayed on their frontpage.

 

I have written a small piece of code that should filter products from a any publisher and it works for almost all publishers expect the one my client wants and a few others.

 

It is especially the line "->addAttributeToFilter('publisher', array('like' => 'Alfa'))" the troubles me. The code will return books published by Alfa and it works smoothly. I can change Alfa to almost all of the publishers in the bookstore and it will return books from the selected publisher expect a few where it doesn't return anything. 

 

Code

 

I hope someone will be kind enough to give me some guidance on how to solve this mystery.

 

2 REPLIES 2

Re: Filter by attribute

hi alekri7,

For the like you can use  %. either use in `Alfa%`  for getting the result that containing match words Alfa and anything after that words.

For example if you use `Alfa%` the it will bring result `Alfaaa``Alfabb` any occurrence.

so i can suggest you should use `%Alfa%` i will bring any result that contain `**Alfa**` . so the Filter will be

->addAttributeToFilter('publisher', array('like' => '%Alfa%'))" 
Find helpful ? Consider Giving Kudos to this post.
Problem solved? Click Accept as Solution!"
Qaisar Satti

Re: Filter by attribute

Hi Alekri,

 

$needle='Alfa';
$collection->addAttributeToFilter('publisher', 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
));

Passing the second parameter as an array of arrays will concatenate the conditions using OR

With custom / non-eav models addAttributeToFilter should be replace with addFieldToFilter.