Prestashop consists of many modules, each having a different functionality to the other. These modules are made of a number of files stored in a folder with the name of the module. These module folders are in turn stored in the /modules folder inside Prestashop.

For your module to function properly, the three files, main file, icon file and the cache configuration file are compulsory. But since the cache configuration file is automatically generated when you create Prestashop module, it will be out of your concern. Any other additional files can be created as per requirement.

Steps to build Prestashop module:

  1. Create a folder for your module inside the /modules folder assign a name to it.

Note – The folder should have the same name as the module with only lowercase alphanumeric characters, underscores, hyphens and no spaces.

Ex: /modulename

  1. Create the main file, a PHP file with the same name as the module inside this folder.

Ex: PHP file -modulename.php

  1. Add the necessary coding to the PHP file in order to make your module function.

Although these steps seem quite easy, the coding procedure in the module’s PHP file is somewhat complex. Prestashop has its own coding standard that should be followed throughout the creation of the module. Therefore before starting with the coding, it is best to learn the coding standards to avoid any complications.

The main file:

Constant Test

It is compulsory that the main file or the PHP file start with the following code when you create Prestashop module. This portion of code is called the Constant Test and it is used to prevent threats to the module’s security from unwanted access.

<? php   
if (!defined('_PS_VERSION_'))
exit;

<strong>Main Class</strong>

The PHP file consists of the main class with any other necessary classes. The main class should have the name of the module in Camel Case and should extend the Module class in order to inherit its properties and functionalities.

<?php
if (!defined('_PS_VERSION_'))
  exit;
 
class MyModule extends Module
{
}

After the complete addition of the above content to the PHP file, the module will become visible and accessible from the back-office’s ‘Other Modules’ section but will not contain a name or an icon.

<strong>Constructor Method</strong>

The constructor method is the first lines of code that will be called when the Prestashop module starts. These lines of code define many important attributes of the module such as its name, the tab where it appears in the back-office, the name of the author etc. It assigns the warnings and other necessary text strings as well to the module.

<?php
if (!defined('_PS_VERSION_'))
  exit; 
class MyModule extends Module
{
  public function __construct()
  {
    $this->name = 'mymodule';
    $this->tab = 'front_office_features';
    $this->version = '1.0.0';
    $this->author = 'Firstname Lastname';
    $this->need_instance = 0;
    $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_); 
    $this->bootstrap = true; 
    parent::__construct(); 
    $this->displayName = $this->l('My module');
    $this->description = $this->l('Description of my module.'); 
    $this->confirmUninstall = $this->l('Are you sure you want to uninstall?'); 
    if (!Configuration::get('MYMODULE_NAME'))      
      $this->warning = $this->l('No name provided');
  }
}

The install and uninstall methodsAfter the constructor method is completed, the module’s details will be visible from the back-office as defined in the constructor. The module can also be installed though it still does not contain any functionality. When the install button is clicked a message will be displayed indicating that your module is Untrusted.  You can make your module trusted by registering as a Prestashop partner or by the distribution of your module via the Add-ons website.

These methods can be used to store the functions that should be carried out when the module is being installed or uninstalled. As an example if you need to store the module’s settings in the website database or even to check the existing settings during the install process, it can be done using the install method. If the module is being uninstalled, the stored settings or other details will become unnecessary and should be removed. This process can be done using the uninstall method.

Install Method

The code snippet below is the basic install method that checks if the module has been installed correctly or not. This method will return either true or false depending on the state of the installation.

public function install()
{
  if (!parent::install())
    return false;
  return true;
}

But since our purpose of having an install method is to make sure if the necessary settings are available or to add new settings, the basic snippet above can be enhanced as follows. The following code returns true only of all the conditions are fulfilled.

public function install()
{
  if (Shop::isFeatureActive())
    Shop::setContext(Shop::CONTEXT_ALL);
 
  if (!parent::install() ||
    !$this->registerHook('leftColumn') ||
    !$this->registerHook('header') ||
    !Configuration::updateValue('MYMODULE_NAME', 'my friend')
  )
    return false;
 
  return true;
}

Uninstall Method

public function uninstall()
{
  if (!parent::uninstall())
    return false;
  return true;
}

Given below is the enhanced version of the basic code snippet for the uninstall method given above. While the basic code only checks if the module has been uninstalled successfully, the enhanced version makes sure that all the module’s configuration is removed from the Prestashop installation.

public function uninstall()
{
  if (!parent::uninstall() ||
    !Configuration::deleteByName('MYMODULE_NAME')
  )
    return false;
 
  return true;
}

Configuration Object

The configuration object is used in all three construct, install and uninstall methods used when you develop Prestashop module. This object is a specific object for Prestashop and it acts as an alternative to SQL queries in communicating with the database. It is used to handle the data in the ps_configuration table of your website’s database.

Some example methods of the configuration object are,

Configuration::get(‘variable’) – retrieve a value from db.

Configuration::updateValue(‘variable’, $val)- update value in db.

Configuration::deleteByName(‘variable’) – delete a value from db.

Shop Object

if (Shop::isFeatureActive())
    Shop::setContext(Shop::CONTEXT_ALL);

The shop object can be found in the install method and is used to check whether the multistore feature is active in the website. If the multistore feature is enabled, it sets the present context to all shops in your website.

Icon file

As the final step of creating the Prestashop module, an icon should be added to be displayed as the module’s thumbnail. This icon should be related to the module or the company who created the module since it will act as an identifying factor for the module. It should be a 32*32 PNG image named as logo.png and should be placed inside the module’s main folder.

After the icon is added, the process conducted to create Prestashop module is completed and thereby you can install and work with your module through the back-office of your website.