Introduction

In order to add the ‘sort by sales’ option to the product sort dropdown in Prestashop, a couple of files need to be changed. To be specific, we will first edit the template file responsible of displaying the options, product-sort.tpl, and then add 2 overrides: Category.php (class file, to add the new sort option to the query which grabs products) and CategoryController.php, to add the ‘sort by sales’ option to the valid list of ‘sort by’ values.

It looks longer than it is! Let’s get started!

Editing the product-sort template file

First, it is vital that we add the ‘sort by sales’ option (for both ascending and descending order, of course!) to our template. Therefore, open up product-sort.tpl which you can find in you theme’s folder. As always, I am using the default theme, so your code might be a little different!

Note: you can freely choose where to add those 2 lines, I have chosen the spot above the ‘Order by product name’, but you can really add the new options wherever you want, as long as they sit inside the <select> box.

What we did here was simply adding a visual reference so that customers can choose if they want to order by best sales, with best products first (first option), or worst first (second option). Of course, these options will not do anything now!

Adding the class override

Let’s get dirty now! We need to first add an override for the Category class, so that a new parameter (sales) is added to the ‘order by’ clause in the sql query which grabs products. If all what I am saying sounds alien to you, be sure you first understand the basics of MySQL (some great articles can be found here).

Therefore, go to override/classes/ and create a new file named Category.php. Then, open it up and add the following inside:

Then, to save us some trouble, we will copy the original getProducts() method of the Category class. It looks like the following, but be sure you copy your own, if you use a different Prestashop Version than mine! (1.5.6):

Take this method inside the override Class. Let’s get to the tricky part!

See that really long string for the $sql variable? We need to modify that one. First, locate the following, or in any case the last item of the SELECT statement:

[php]
product_shop.price AS orderprice
[/php]

We need to grab one more value, like this:

[php]
product_shop.price AS orderprice, ps.quantity AS sales
[/php]

Notice: we are referring to an alias (ps) which has not been defined yet. Then, for simplicity, we are calling this new parameter ‘sales’. If you remember, we previously used sales:desc and sales:asc for our select box. Well, for simplicity it’s better to call this new ‘order by’ parameter the same way across all files, so that we don’t need to add other parameters to refer to it when it changes!

Now, let’s join that ‘ps’ table we referred to in the select statement, which is product_sale! Locate the last join, which is probably this:

For this one, we must use a different approach. The native productSort() method is nowhere to be found inside CategoryControllerCore. So, why are we extending this? Because that class is extending another as well: FrontController. And this last one contains the method we need. If you are used to the rules of inheritance in OOP, you know that each class extending another has also access to all the parent’s methods, and can of course extend those as well. So, CategoryControllerCore, which extends FrontController, has access to its parent’s productSort. Thus (finally!) its own child, our new CategoryController know about it’s grandpa’s method too!

At this point, if you didn’t shoot your head, go to classes/controller/ and open the original FrontController.php. Locate its productSort() method:

We are almost done! Now pay a closer look at the $order_by_values variable. It’s an array containing all valid names for the ‘sort by’ box! Therefore, at this point we can simply add sales to it, as a last item:

Do you now see why it is important to keep names uniform throughout all the code? Well, at this point we are done!

Go to cache/ and delete class_index.php. Navigate to a category page and test out the form, it should order your products by sales, and preserve changes while navigating through pages!

Important note: if you test out the new sorting options, be sure the orders for your tests are valid, as otherwise products bought will not add up to the quantity which is necessary to calculate the number of sales!

Related

Author Spotlight

Fabio Porta

Fabio has been involved in web development and design since 2005, when launched his first website at the age of 16. He’s now highly skilled in both client and server side development, along with design, and since August 2012 runs a successful website about PrestaShop tutorials and Prestashop Modules called Nemo’s Post Scriptum, at http://nemops.com

Hi Stefania!
I didn’t test it but you should be able to do it by joining ps_specific_price and ordering by “to” (editing the getProducts query in this case). Of course, you must add the ordering condition as explained in the tut, along with this