The standard Search and Advanced Search look for a single extract so keywords have to be consectutive and in the same order.
This Improved Search breaks the search term into individual keywords and searches for them seperately so it will find them if they are not consecutive and in any order.
Description and Model may still be selected.
Free v1.4.9 Extensions: Default Specials | Improved Search | Customer Activity Report | Customer Groups | Royal Mail With Handling | Improved Product Page | Random Products | Stock Report | All Products
Firstly, make all the keywords lowercase, trim any excess space either side, change any double spacings or more into single spaces using a regular expression, and when exploded, use array_unique to make sure you're not searching the same term twice
Code: Select all
$search_words = trim(preg_replace('/\s\s+/', '', strtolower($keyword));
$search_words = explode(" ", $search_words);
$search_words = array_unique($search_words);
Explode removes the delimiting space and sometimes, it may be useful to use additional whitespace to look for whole words. I didn't bother making sure that keywords were unique because the user enters them to search and if they use the same keyword twice, I figured they deserve to wait a little longer for the result.

I didn't actually realise that the standard search was so weak because it has never been reported to me as a problem; I was responding to someone in support. I think it should be core behaviour.
Free v1.4.9 Extensions: Default Specials | Improved Search | Customer Activity Report | Customer Groups | Royal Mail With Handling | Improved Product Page | Random Products | Stock Report | All Products
The explode doesn't quite work as you would expect. Say someone puts
"Keyword1 Keyword2 Keyword3"
Then the three spaces between Keyword2 and Keyword3 changes into 2 empty strings which will still be used in the query and foreach, so you would end up with like %% which will match everything, and really start straining on your db
The strotolower is nothing to do with the query as such, its to make sure the array_unique finds two words the same and removes them, since it will class Hello and hello as two separate words, even though they are the same when it comes to the query
As I say, it was only what I would consider improvements on the code, it's up to you

Okay, I guess I should change it back to the date_added if that is what it used to be.
Free v1.4.9 Extensions: Default Specials | Improved Search | Customer Activity Report | Customer Groups | Royal Mail With Handling | Improved Product Page | Random Products | Stock Report | All Products
Together with numerous free 'essential' extensions/mods (eg. Filter By Category, Manufacturer Description, Recent Products Module), they comprise my 'core' due to a strange reluctance to make them official.
Free v1.4.9 Extensions: Default Specials | Improved Search | Customer Activity Report | Customer Groups | Royal Mail With Handling | Improved Product Page | Random Products | Stock Report | All Products
I just wanted to say thanks for your contribution, this worked great with Opencart v1.4.9.3.
Best regards,
Gary
Still didn't make it into v1.4.9.5 though, so it needs patching again so that it doesn't look like the search is broken.
Oh well, I can only try!
Free v1.4.9 Extensions: Default Specials | Improved Search | Customer Activity Report | Customer Groups | Royal Mail With Handling | Improved Product Page | Random Products | Stock Report | All Products
Well I feel both bad for users of 1.4.9.5 and lucky that I stayed with 1.4.9.3!

But it is shocking that such a core function of an e-commerce website, search, by default is so lacking in user functionality. You are a real credit to the community here and I hope that your extension gets the due credit it deserves.
All the best!
Users browsing this forum: No registered users and 1 guest