PrestaShop search engine does its job quite well. But it is missing one thing – the category filter.
You may think: “Yeah, but we have wide range of different filters on the category page”. And it is true, indeed. However it’s good for user experience and it’s also good practice to have it (especially if you run a big store).
So, if you want to make your search engine better, keep reading!
Here is a quick preview of what is going to be changed:
blocksearch-top.tpl – Template file with a new filter
Tools.php – Query to list all (active) categories
Search.php – Search class with modified search query
SearchController.php – Search controller with modified calls to find
function in Search class
Display category filter
Find:
Add before:
Three things are done here:
1. Categories are listed from the query in Tools
class
2. Check if current category is main (root). If so, bold the name.
3. Add selected
HTML attribute to the chosen option, so it remains selected after the form has been sent.
Add new function:
public static function getSearchCategories($id_lang) { return Db::getInstance()->executeS('SELECT cl.id_category, cl.`name`, c.is_root_category FROM '._DB_PREFIX_.'category_lang cl JOIN '._DB_PREFIX_.'category c USING(id_category) WHERE cl.id_lang='.$id_lang.' and c.active=1 ORDER BY c.position ASC, c.is_root_category DESC, cl.`name` ASC'); }
As you can see, there is a condition to display active categories in user language. You can change it to fit your needs.
Now you should see in your shop something like this.
The form is fine but it could look better. So you should use CSS to properly style it to fit your needs.
And it is not sorting by category yet. That’s what we’re going to do now.
Modify search query
You must add a new parameter to all occurences of find
function call.
Find (ALL OCCURENCES!):
Search::find(
Add after (ALL OCCURENCES!):
(int)Tools::getValue('search_category'),
Nothing fancy is done here. We’re passing the $_GET['search_category']
variable using prestashop getValue()
function to the search query.
Time for the last and the most important step – modifying the search query.
find
function. Let’s add here the new parameter right after opening bracket.
Find:
function find(
Add after:
$category = "none",
Find:
if (Group::isFeatureActive()) { $groups = FrontController::getCurrentCustomerGroups(); $sql_groups = 'AND cg.`id_group` '.(count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1'); }
Add after:
$search_category = ($category != "none") ? ' AND cp.id_category='.$category.' ' : ' ';
Find:
WHERE c.`active` = 1
Add after:
'.$search_category.'
The find function checks if category is chosen (and it’s different than default one). If so, there is a new condition which limits the query to the one category. Otherwise, nothing changes so it’s searching in the whole store.
Set overrides
Last and very important thing is to move all Your changes to override/
directory.
1. Copy and paste files:
Source | Destination |
---|---|
classes/Tools.php | override/classes/Tools.php |
controllers/front/SearchController.php | override/controllers/front/SearchController.php |
classes/Search.php | override/classes/Search.php |
2. Replace in DESTINATION files:
– class ToolsCore
replace with class Tools extends ToolsCore
– class SearchControllerCore extends FrontController
replace with class SearchController extends SearchControllerCore
– class SearchCore
replace with class Search extends SearchCore
3. Purge cache:
Remove /cache/class_index.php
file.
Thanks for reading! Make sure to like our Facebook page and check out our free modules.