Prestashop 1.7 module
I’ve installed a Prestashop store because… It’s free and open source. I need some modules like EU cookie banner and I don’t want to pay 30+€ just for that.
I looked at the community solution but I didn’t find a solution that transcended me… And in fact, it’s a good "hello world" example for me to learn how to create modules for Prestashop.
The full implementation of the module can be found in this GitHub project.
Environment
For this tutorial, I use a fresh installed Prestashop 1.7.2.1 and PHP 7.1.1.
I use a symbolic link to add my git source folder to prestashopFolder/modules/z_cookiesalert
Initialization
For initializing the module files, you can follow the Prestashop documentation.
But it’s full of naming convention and specific folder organization. Your module folder must be lowercase (z_cookiesalert). The first php file name must be lowercase too (z_cookiesalert.php). The class name in this file must be camlCase (class Z_Cookiesalert extends Module). And don’t forget everytime you add a folder, you must add an index.php file.
The easiest way I found on community forum is to start from an existing module (get an official one from Prestashop’s GitHub) and modify the names.
Another tip is to target 1.7+ compatibility and forget 1.6 and 1.5. There are too many differences between well-organized modules for 1.7 and for preview versions.
The module
The z_cookiesalert.php content must start by
|
|
After that, we need to create the main class with the module description. All information on the __construct method will be used by Prestashop to generate the config.xml file.
|
|
The front office
For displaying the ribbon message, we will add a hook to the "displayFooter" hook. And we will add all css and js required on the "header" hook. At installation, we will also declare a default content message for the ribbon.
|
|
To get the hooked content, Prestashop will look at the method named hookNameOfTheHook. In our case, it will be hookDisplayFooter and hookDisplayHeader.
We will register the module’s stylesheet and the module’s javascript files on the header with :
|
|
For the footer, if the cookie usage has not been accepted yet ( check on the cookie =D ), we will add two parameters to smarty and display the cookiealert template.
The back office
The content of the ribbon must be customizable by the end user. For this, we must declare a new displayConfigForm method. For having a uniform render, Prestashop advice to use HelperForm.
For this, we will create a $fields_form with all required fields and use the helper to configure the render.
|
|
The ajax call
When the user closes the ribbon, we want to create an ajax call to update the cookie on the server side. For this, we must create a front controller. We will add a cookies.php file on z_cookiealert/controllers/front/. This file will declare a ModuleFrontController. The naming convention will force the name to Z_CookiesalertCookiesModuleFrontController class.
When the class will be called, it will set the cookiealert to accepted and response true.
|
|
For calling the controller, we must resolve the url to the controller. The easiest way to do that is to use url method on the template. For this, we will add a data-url attribute to cookie_alert div.
|
|
|
|