-
Book Overview & Buying
-
Table Of Contents
PrestaShop Module Development
By :
First of all, we have to choose a public and technical name for the module. The technical name must be in lowercase, contain only letters and numbers, and begin with a letter. Let's call the module publicly My Module of product comments and technically mymodcomments.
You can choose whatever names you want as long as you stick to them for the rest of the book. However, since the technical name will be the directory name of your module, you won't be able to have two modules with the same technical name in a PrestaShop. Moreover, your technical name has to be unique if you want to sell it on marketplaces. One trick is to prefix it with your company name (in my case, my company name is Froggy Commerce, so I prefixed all my modules with froggy).
On the other hand, the public name has no restriction, so you can write whatever you want for the merchant.
Now, we will begin to create our first module.
Open the modules directory in the root of the PrestaShop directory, then create a new directory and name it with the technical name we chose: mymodcomments. Once done, in this new directory, create a new blank PHP file and name it with the technical name as well (in our case, mymodcomments.php). This is the main file of the module. The following screenshot depicts the file and folder structure:

Now open the
mymodcomments.php file, and write the class of your module. You must name it with your technical name. To make it easier to read, you can use CamelCase. The class must extend PrestaShop's Module class. This class contains all the needed methods to make a module work; without it, your module won't work. In our case, the class of the module will be:
<?php
class MyModComments extends Module
{
}Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Additionally, the code for this book is part of a Git repository, which is available on GitHub at https://github.com/FabienSerny/mymodcomments, https://github.com/FabienSerny/mymodcarrier, and https://github.com/FabienSerny/mymodpayment.
In order to have a working module, we just have to add the __construct method. In this method, you must write the following three mandatory lines:
$this->name = 'mymodcomments';
$this->displayName = 'My Module of product comments';
parent::__construct();
You can add some of the following optional lines to give information about the module:
Other Modules category. You should set one of the values shown in the following table:|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
These possible values are associated with the module's categories search filter in the back office of your PrestaShop:
$this->tab = 'front_office_features';
$this->version = '0.1';
$this->author = 'Fabien Serny';
$this->description = 'With this module, your customers will be able to grade and comments your products';
Here's what your code should look like now:
<?php
class MyModComments extends Module
{
public function __construct()
{
$this->name = 'mymodcomments';
$this->tab = 'front_office_features';
$this->version = '0.1';
$this->author = 'Fabien Serny';
$this->displayName = 'My Module of product comments';
$this->description = 'With this module, your customers will be able to grade and comments your products.';
parent::__construct();
}
}At this point, you should be able to see your module in the back office of the modules section, as shown in the following screenshot:

The question mark icon
This is the default logo for all modules. If you want a custom picture, you just have to add a logo.png picture of 32 x 32 pixels and a logo.gif picture of 16 x 16 pixels (for PrestaShop 1.4 compliancy) at the root of your module directory.
The module is now a working module; you can install it by clicking on the Install button. Here is what you should see:

For the moment, your module does nothing and doesn't have any configuration options. The only actions available are:
All these core functions are handled by the Module class and can be overridden by the module (we will see this later).
Change the font size
Change margin width
Change background colour