How To Create Custom Sorting In Prestashop 1.5

March 21, 2013 Alex Simonchik Prestashop
How To Create Custom Sorting In Prestashop 1.5

Our company constantly monitors the official Prestashop forum to be aware of the most common issues people face when using this system. And we see that a frequently asked question is how to create a custom sorting. So today we will show how to do this. At the end of the article you can download the ready-to-use module and customized tpl template product-sort.tpl. As an example we will add a new sorting for the field EAN13.

Img-0how to create custom sorting in PS 1.5

There is no need to display the whole module code. That is why we will just highlight the main points. So, how does Prestashop associate products with a catalog page? There is the function assignProductList of the controller CategoryController which is responsible for that. Below you can see the part of the code which is responsible exactly for defining products for a category.

For our purposes we will create a module with the name: belvg_customsortexample and register it for this hook:

We also need to override the behavior of the function getProducts of the class Category and function productSort of the controller CategoryController. Here we will highlight only the main points, which are necessary for proper functioning of our module:

And the last step is to change the template product-sort.tpl. We need to add options for ean13 into the html element select:

As a result a new sorting has been created. Hurray!

Img-1how to create custom sorting in PS 1.5



24 comments

  1. Hi Alex!

    Is it possible to generate my own pagination if i have $product array with all product i need.

    I try to do like this:

    $this->context->smarty->assign(‘categoryNameComplement’, ”);
    $this->nbProducts = count ($my_products);
    $this->pagination((int)$this->nbProducts); // Pagination must be call after “getProducts”
    $this->cat_products = $my_products;

    but without success

  2. Anton,

    Do you want to create your own controller that will display the collection of those products which satisfy a certain condition (the content of $my_products)?

    As an example, I will show the code of our module Discounted items:

  3. Hi Alex!

    first, congratulations for your module!

    But it works only in categories items.

    Those items shown after a simple search (in search bar) continue ordering by title.
    Error?

    Thanks.

  4. Peter,

    The thing is that the operating principle is described on the basis of the controller Category. But if you are using another controller, for example Search, you need to take into account that you need to transmit custom parameters into SQL. That is why the .zip file does not contain any examples with Search.

  5. Hi Alex,
    thanks for this article, is it possible to add a search by 1 attribute using this method ?

  6. Using this example you can create a custom sorting. If you need advanced attribute search, you can check out how to change the search algorithm here.

  7. Hallo… great idea!

    Can I use it to sort products by colors?

    I made an attribute named “Color” which has values like “Blu, Brown, White, etc.”

  8. Bruno,

    Unfortunately sorting by Bestsellers requires a more complex approach than just changing of the parameter ORDER BY. Perhaps, in future we will dedicate a separate article to this problem, because this issue requires some development.

  9. Raffaele,

    I recommend you to enable the module Blocklayered Navigation

  10. Hi,

    Can I add an attribute like size to the sort by list. or is there any other way to sort with attributes in the product category page. If would be great if I can replace sample EAN with size attribute.

  11. Trus,

    I would say that it is possible to do so, but you need to remember that the data about the product size is stored in another table.

  12. Hello Alex,

    Can I modify the module to work with the reference number of the products and not with ean13?
    Is that possible?

    Thank you…

  13. Spirоs,

    The source code of my solution is open, you can modify it in any way in accordance with your requirements. If you are interested in some specific technical issue, please indicate that so I would be able to answer it. However, if you are not familiar with PHP, you may contact Valerie at store@belvg.com

  14. Great article there and it opened up for me.
    Possible to do a simple sort by product attribute id ie. Attribute Group = Size and Attribute Id = 1 (S) etc? So the products without this product attribute does not show.

  15. Alan,

    Everything is possible:) But in this way you may need to take data from the other tables.

  16. Hi Alex. Great contribution! Can you provide some hint how to set default sort by ID? Newest first (on top of product-list.tpl) ?
    Much appreciated!

  17. Konrad,

    Well actually you can do it as described in the article, but instead EAN13 field use id_product.

  18. Alex! Thanks it work’s like a charm! date_upd variable is even better for my need’s :)

  19. hello,
    Please tell me if i would like to create a sort by discount, how could i do that?

  20. Hi Alex, Mayor and Florin

    I am sorry, but your questions aren’t simple and require a long time for description. Could you contact our Support Team at store@belvg.com? They will try to help you.

  21. In our article we described about adding the field EAN13 field to the search, you should replace all mentions of ean13 to the date_add.
    By the way in our article the date_add field have already exist: $order_by_values = array(0 => ‘name’, 1 => ‘price’, 2 => ‘date_add’, 3 => ‘date_upd’, 4 => ‘position’, 5 => ‘manufacturer_name’, 6 => ‘quantity’, 7 => ‘ean13’); //custom ean13 field

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *