PrestaCraft on Facebook

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


PrestaCraft

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.

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.

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

Add new function:

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.

search_select

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.

Find (ALL OCCURENCES!):

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.

Find:

Add after:

Find:

Add after:

Find:

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.

Comments about this post (2)

Add a comment
Pedro 23-07-2017
Hi, thanks for the tutorial! But I have a problem and it is that my prestashop is 1.4 and as I can not update it since it has been very modified, how could I adapt this tutorial to prestashop 1.4, could you help me with this?

Best regards
Pedro 23-07-2017
Hi, thanks for the tutorial! But I have a problem and it is that my prestashop is 1.4 and as I can not update it since it has been very modified, how could I adapt this tutorial to prestashop 1.4, could I sayudarme with this?

Best regards

Leave a comment

Your email address will not be published.