PrestaCraft on Facebook

Please like our page on Facebook to get notifications about new tutorials :)


PrestaCraft on Facebook

Please like our page on Facebook to get notifications about new tutorials :)


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!

Before any changes, please remember to do a backup of all your files! I don’t take any responsibility for presumable inconveniences or bugs which may occur after those modifications.

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

Edit: themes/default-bootstrap/modules/blocksearch/blocksearch-top.tpl
Let’s start with the select category filter.


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.

Edit: classes/Tools.php
Now it’s time for query to list (all active) categories in current language.

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 
    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

Edit: controllers/front/SearchController.php
We will start with the controller.

You must add a new parameter to all occurences of find function call.



Add after (ALL OCCURENCES!):


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.

Edit: classes/Search.php
As you should figure out from the code. search is done using find function. Let’s add here the new parameter right after opening bracket.


function find(

Add after:

$category = "none",


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.' ' : '  ';


WHERE c.`active` = 1

Add after:


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.

Leave a comment

Your email address will not be published.


Leave a comment

Your email address will not be published.