How to automatically associate all categories to a new customer group on PrestaShop 1.7

You may not know it, but when you create a new customer group on Prestashop, it will not have access to any category, you will have to go to each category to associate this new group.

To be honest, this seems like an aberration, but it has been around forever.

I believe that a future version of Prestashop 1.7 will integrate this feature but as of today, nothing is done.

Here is an interpretation of the solution proposed by Eolia at the time of Prestashop 1.6 for the version 1.7 of PrestaShop.

The expected result

What we want to do with this tutorial is simply to add a switch on the page for creating a customer group so that when you register this new group, it is associated with all the categories in the catalog.

Here is what you will get visually

The method to obtain it

We will intervene only on one file: controllers/admin/AdminGroupsController.php

I didn't decide to make an override of the controller because it is necessary to integrate all the code of the called functions which, even if it is cleaner in the future, doesn't bring much here.

The creation of the switch

We will first integrate the code to create this new choice switch.

You must integrate this code in the call made from the inputs in the function renderForm().

Personally, I added it after the price display switch, so after the input with the name show_price.

The recording

The second part will be to record the information of the choice of the association to all the categories of this group and then to select all the categories and to really associate them to the group.

To say quickly it is complicated, but to make if we leave as intelligently made Eolia we fall quickly on our pasta.

In the function already present in the source code processSave(), we add the call to a new function access_all() to which we associate the object defining the new group.

From there we create an SQL query that allows us to associate in one line all the categories to a group or more precisely, a group to all the categories.


The main interest of this solution, besides its simplicity, is that it allows to improve PrestaShop without preventing to keep the basic functioning of the solution, it leaves the choice and it is the best solution in my opinion.