Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

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.png

The 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

FieldWhat it means
nameThe technical name. Must match the folder and file name
tabThe section in the module list: front_office_features, administration, payments_gateways, shipping_logistics
versionThe module version. The engine uses it to see that the module needs an upgrade
authorThe author, shown in the module list
need_instance0 — do not create the module object when the module list is shown. Almost always 0
ps_versions_compliancyWhich PrestaShop versions the module works with
bootstraptrue — the settings page uses the back office styles
displayNameThe human-readable name
descriptionThe 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 acmehello

The 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.

ActionWhat it does
installInstalls: calls install()
uninstallRemoves: calls uninstall()
resetRemoves and installs again
enable, disableTurns 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'.

Thanks for reading!

Donate to the site — we work hard for you.