Get Free Audit

How To Create Custom Sorting In Prestashop 1.5

Mar 21, 2013

3419 Alex Simonchik

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:

Andrey_Dubina
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey


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

development_prestashop

PrestaShop Development

Take your online store to the next level with BelVG PrestaShop Development

Visit the page
Andrey Dubina
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

28 Comments

  1. Hi, Alex

    The discounted price is dynamically calculated, so there is no easy way to sort by this type of value without decreasing the performance. The best way is to create a separate index table for storing this value and keep this table for sorting. I can’t describe in details how to make it within the comment, but you can get in touch with our great Support Team and they will prepare a quote about that and implement the feature into your store.

  2. Hi Alex, I would like to be able to sort by discounted price (or specific price how it’s called in the platform) instead of the retail price (price with taxes). Is there an easy way to do that?

  3. Hi Le Roux,

    an idea is the same but flavored with features of the new version.
    The main part of the code will be the same. Unfortunately, I don’t have a much time to describe everything, but you can contact our support team to ask for a paid support: [email protected]

  4. Hi, I’am french,sorry for my english.
    My question is how can I do the same for presta 1.6.17?
    Thanks for help
    yann

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

  6. Hi,
    Search in Prestashop returns results by “position” with desc
    How can change it by date_add with desc ?
    Thank you

  7. 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 [email protected]? They will try to help you.

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

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

  10. Konrad,

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

  11. 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!

  12. Alan,

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

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

  14. 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 [email protected]

  15. 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…

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

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

  18. Raffaele,

    I recommend you to enable the module Blocklayered Navigation

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

  20. 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.”

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

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

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

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

  25. 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:

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

Post a new comment

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