Creating a custom module in PrestaShop allows you to extend its functionality without modifying the core files. In this tutorial, we will go step by step to create a basic custom module for PrestaShop 1.7 and later versions.

Step 1: Create the Module Folder and Files

Navigate to the modules directory of your PrestaShop installation and create a new folder for your module. Let’s name it mycustommodule.

Inside the mycustommodule folder, create a PHP file named mycustommodule.php. This will be the main file for your module.

Step 2: Define the Module Class

Open mycustommodule.php and add the following code:

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

class MyCustomModule extends Module
{
    public function __construct()
    {
        $this->name = 'mycustommodule';
        $this->tab = 'front_office_features';
        $this->version = '1.0.0';
        $this->author = 'Your Name';
        $this->need_instance = 0;
        
        parent::__construct();

        $this->displayName = $this->l('My Custom Module');
        $this->description = $this->l('A simple custom module for PrestaShop.');
    }
}

Step 3: Install and Uninstall Methods

Add the following methods inside the MyCustomModule class to handle installation and uninstallation:

public function install()
{
    return parent::install() && Configuration::updateValue('MYCUSTOMMODULE_TEXT', 'Default Value');
}

public function uninstall()
{
    return parent::uninstall() && Configuration::deleteByName('MYCUSTOMMODULE_TEXT');
}

Step 4: Register a Hook

To display content in a specific part of your PrestaShop store, register a hook. Let’s hook into the displayHome hook to show a message on the homepage:

public function install()
{
    return parent::install() && $this->registerHook('displayHome');
}

public function hookDisplayHome($params)
{
    return '<p>Welcome to my custom module!</p>';
}

Step 5: Create a Basic Configuration Page

To allow users to modify module settings, add the getContent method for an admin configuration form:

public function getContent()
{
    $output = '';
    if (Tools::isSubmit('submitMyCustomModule')) {
        $customText = Tools::getValue('MYCUSTOMMODULE_TEXT');
        Configuration::updateValue('MYCUSTOMMODULE_TEXT', $customText);
        $output .= $this->displayConfirmation('Settings updated');
    }
    
    return $output . $this->renderForm();
}

private function renderForm()
{
    $fieldsForm = [
        'form' => [
            'legend' => [
                'title' => $this->l('Settings'),
            ],
            'input' => [
                [
                    'type' => 'text',
                    'label' => $this->l('Custom Text'),
                    'name' => 'MYCUSTOMMODULE_TEXT',
                    'size' => 20,
                    'required' => true,
                ]
            ],
            'submit' => [
                'title' => $this->l('Save'),
                'class' => 'btn btn-default pull-right'
            ]
        ]
    ];

    $helper = new HelperForm();
    $helper->table = $this->table;
    $helper->default_form_language = (int) Configuration::get('PS_LANG_DEFAULT');
    $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
    $helper->identifier = $this->identifier;
    $helper->submit_action = 'submitMyCustomModule';
    $helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
    $helper->token = Tools::getAdminTokenLite('AdminModules');
    
    $helper->fields_value['MYCUSTOMMODULE_TEXT'] = Configuration::get('MYCUSTOMMODULE_TEXT');

    return $helper->generateForm([$fieldsForm]);
}

Step 6: Install the Module

  1. Zip the mycustommodule folder.
  2. Go to PrestaShop Back Office > Modules > Module Manager.
  3. Click Upload a module and select your zip file.
  4. Install and activate the module.
  5. Configure the module in the admin panel by modifying the custom text field.

Now, your module should display a message on the homepage!

Conclusion

This tutorial covered the basics of creating a PrestaShop module, including an admin configuration page. You can extend it further by adding more hooks, a database table, or advanced settings to enhance the module’s functionality.