The first module
A module is a folder in modules with a PHP file of the same name. The file holds one class that extends Module: it describes the module and says what to do on install and uninstall. There is no console command that creates a module — the folder and the file are created by hand.
Folder and file
Create the folder modules/acmehello and the file acmehello.php in it. You can put a 32 by 32 pixel logo.png icon next to it — it is shown in the module list.
modules/acmehello/
├── acmehello.php
└── logo.pngThe folder name and the file name match and consist of lowercase Latin letters and digits. The acme prefix is the name of your company: this way the name does not clash with someone else's module.
The main class
<?php
// modules/acmehello/acmehello.php
if (!defined('_PS_VERSION_')) {
exit;
}
class AcmeHello extends Module
{
public function __construct()
{
$this->name = 'acmehello';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'Acme';
$this->need_instance = 0;
$this->ps_versions_compliancy = ['min' => '8.0.0', 'max' => _PS_VERSION_];
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->trans('Acme reviews', [], 'Modules.Acmehello.Admin');
$this->description = $this->trans('A greeting and product reviews.', [], 'Modules.Acmehello.Admin');
}
}Open "Modules → Module Manager" in the back office and type acme in the search: the module is in the list with an "Install" button. The _PS_VERSION_ check at the top of the file prevents opening it directly from a browser. The class name is the module name; letter case in it does not matter.
Constructor fields
| Field | What it means |
|---|---|
name | The technical name. Must match the folder and file name |
tab | The section in the module list: front_office_features, administration, payments_gateways, shipping_logistics |
version | The module version. The engine uses it to see that the module needs an upgrade |
author | The author, shown in the module list |
need_instance | 0 — do not create the module object when the module list is shown. Almost always 0 |
ps_versions_compliancy | Which PrestaShop versions the module works with |
bootstrap | true — the settings page uses the back office styles |
displayName | The human-readable name |
description | The description under the name |
displayName and description are set after parent::__construct(): the trans() method translates a string, and translations are available only after the parent call. Translations have their own lesson.
Install and uninstall
Add two methods to the class:
// modules/acmehello/acmehello.php
public function install(): bool
{
return parent::install();
}
public function uninstall(): bool
{
return parent::uninstall();
}install() runs once, when the module is installed. Later it will subscribe to hooks, create tables and save default settings. uninstall() cleans up. Both methods return true on success; on false the back office shows an error.
Installing from the console
Commands are run from the shop root:
php bin/console prestashop:module install acmehelloThe command prints a success message. In the Module Manager the "Install" button changes to "Disable". Nothing has changed in the front office yet: the module does not print anything so far.
| Action | What it does |
|---|---|
install | Installs: calls install() |
uninstall | Removes: calls uninstall() |
reset | Removes and installs again |
enable, disable | Turns on and off without deleting data |
Put the action in place of install in the command above. The buttons in the Module Manager do the same.
Common mistake
The name property is spelled differently from the folder: acme_hello or AcmeHello. The module does not appear in the list or does not install, and the log is empty. The folder, the file and name must match letter for letter: acmehello, acmehello.php, 'acmehello'.