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.
I hope someone will be kind enough to give me some guidance on how to solve this mystery.
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%'))"
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.