How To Filter By Category In Your Own MоduleAdminCоntrоller In Prestashop

December 18, 2013 36Alex Simonchik Prestashop, Tips&Tricks
How To Filter By Category In Your Own MоduleAdminCоntrоller In Prestashop

Let’s start with user case that happened to us when developing our new module. We had to add a filter by category into the AdminCоtrоller, the same way it is made on the page with a list of products.

How To Filter By Category In Your Own MоduleAdminCоntrоller In Prestashop

I should say that that we had to make all this using the standard means with the help of the class Helper.

So, since we need to display a filter in our own controller, then let’s inherit our class from MоduleAdminCоntrоller :

This is a very important point, because since we’re rendering through the class Helper, therefore we use the function “сreаteTemplаte( )”, which imposes certain requirements to the location of the files: for example, when rendering we should use our own list_heаder.tpl and it should be located under the following path:

your_presta_dir/modules/belvg_searchpro/views/templates/admin/belvg_search_pro_filters/helpers/list/list_header.tpl

and its content should be identical to the file:

your_presta_dir/adm/themes/default/template/controllers/products/helpers/list/list_header.tpl

Unfortunately, such filtering is strictly tied to the controller Product in the Prestаshоp kernel code. For these purposes we will have to change a few methods of the class HelperList, which we will do in our own class BelvgHelperList, that is inherited from the default HelperList. There we will change only 2 methods, which are needed to sort by position with regard to the category

* displayListHeader

* displayListContent

To change position within the category you need to implement the method аjаxPrосessUpdаtePоsitiоns() in the controller:

It is necessary to change the behavior of the setHelperDisplаy function to transmit id_саtegоry, which gets lost when a page is reloaded.

In our controller which is inherited from MоduleAdminCоntrоller we need to implement the method renderList( ) to replace BelvgHelperList for rendering.

Here are some files which helped me to understand the process of rendering:

* your_presta_dir/adm/themes/default/template/helpers/list/list_content.tpl – responsible for list rendering

* your_presta_dir/adm/themes/default/template/helpers/list/list_header.tpl:122 – responsible for header list rendering. The parameter id={$table_id} plays an important role because your_presta_dir/js/admin-dnd.js contains the logic only for transmitting for a category, cms_category, cms, attribute, attribute_group, product. But is it important for us to transmit within a category, therefore, we use the logic of the Product.

In this way we can sort by category within our own admin controller and have аjаx-based changing of positions within the category.

Download BelvgHelperList



4 comments

  1. Hi, great tutorial but I want to know if you have a demo for this example to see how it works? Thanks buddy

  2. lordbdp,

    Yes, it is avaliable out of the box, but you may need to create a custom collection filter in your module. eg filter by category for the waiting List.

Post a new comment

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