DEV Community

Gofenice Technologies
Gofenice Technologies

Posted on

How to Build a Custom PrestaShop Module: A Step-by-Step Guide

PrestaShop’s modular architecture is one of its biggest strengths, allowing you to extend and customize your store to meet unique business needs. Whether you’re looking to add new features, integrate with external APIs, or simply enhance your shop’s functionality, building a custom module is the way to go.

In this in-depth tutorial, I’ll walk you through how to create a simple yet powerful custom PrestaShop module — step by step. By the end, you’ll have a solid foundation to build more complex modules tailored to your store’s requirements.

🔧 Step 1: Setting Up Your Module’s Directory
All PrestaShop modules reside in the /modules directory of your PrestaShop installation.

✅ Create a new folder for your module. For example:

bash
Copy
Edit
/modules/mycustommodule
✅ Inside this folder, create the main PHP file. Typically, it should have the same name as the folder:

bash
Copy
Edit
/modules/mycustommodule/mycustommodule.php
📦 Step 2: Defining the Module Class
The heart of your module is the PHP class extending PrestaShop’s Module class. Let’s start by defining basic metadata:

php
Copy
Edit
<?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 custom module for PrestaShop.');
    $this->ps_versions_compliancy = ['min' => '1.7', 'max' => _PS_VERSION_];
}
Enter fullscreen mode Exit fullscreen mode

}
Here’s what these properties do:

$name: The technical name (matches the folder name).

$tab: Module category in the back office.

$version, $author: Self-explanatory.

$need_instance: If the module needs to be loaded on every page.

$displayName, $description: Human-readable details.

$ps_versions_compliancy: PrestaShop version compatibility.

🛠️ Step 3: Installing and Uninstalling the Module
Next, add methods to handle installation and uninstallation logic:

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

public function uninstall()
{
return parent::uninstall();
}
install(): Called when installing the module. You can register hooks here (like displayHome) or create database tables.

uninstall(): Cleanup logic when uninstalling.

🎨 Step 4: Adding Functionality with Hooks
Hooks let you insert content at predefined points in your theme.

Here’s a basic example that displays “Hello from My Custom Module!” on the homepage:

php
Copy
Edit
public function hookDisplayHome($params)
{
return '

Hello from My Custom Module!

';
}
✅ Now, when the module is installed and hooked to displayHome, this message will appear on the homepage.

🖼️ Step 5: Adding a Configuration Form (Optional)
Most modules need a configuration page in the admin panel. PrestaShop’s HelperForm class makes this easier.

Add the following methods to your module:

php
Copy
Edit
public function getContent()
{
$output = '';
if (Tools::isSubmit('submit_'.$this->name)) {
$my_option = Tools::getValue('MY_OPTION');
Configuration::updateValue('MY_OPTION', $my_option);
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
return $output.$this->renderForm();
}

protected function renderForm()
{
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');

$fields_form[0]['form'] = [
    'legend' => [
        'title' => $this->l('Settings'),
    ],
    'input' => [
        [
            'type' => 'text',
            'label' => $this->l('My Option'),
            'name' => 'MY_OPTION',
            'size' => 20,
            'required' => true
        ]
    ],
    'submit' => [
        'title' => $this->l('Save'),
        'class' => 'btn btn-default pull-right'
    ]
];

$helper = new HelperForm();

$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;

$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;

$helper->fields_value['MY_OPTION'] = Configuration::get('MY_OPTION');

return $helper->generateForm($fields_form);
Enter fullscreen mode Exit fullscreen mode

}
This form lets admins update a simple text field (MY_OPTION). You can expand it to include checkboxes, selects, or other input types.

🔗 Step 6: Final Steps and Testing
✅ Zip your module folder or upload it directly in the PrestaShop back office under “Modules.”
✅ Install it via the back office and see your “Hello” message on the homepage.
✅ Check that your configuration form saves data correctly.

🧩 Going Further
Your first module is just the beginning! Here’s what you can explore next:

  1. Add translations for multilingual support (/translations folder).

  2. Use templates (/views/templates/hook) to insert more advanced HTML.

  3. Interact with the database to store and retrieve custom data.

  4. Create admin controllers for more complex settings.

  5. Extend it with JavaScript or CSS for dynamic functionality.

🚀 Conclusion
Building a custom PrestaShop module unlocks endless possibilities for tailoring your store to your unique needs. Start small, like we did here, then iterate and build more advanced features.

If you want to dive deeper or need help with a custom PrestaShop solution, let’s connect!

Top comments (3)

Collapse
 
gofenice_technologies profile image
Gofenice Technologies

💬 *We’d love to hear from you! Have you tried building your own PrestaShop module before? What challenges did you face, or what features are you most excited to add? Drop your thoughts or questions below — let’s learn and grow together! *

Collapse
 
nevodavid profile image
Nevo David

pretty cool seeing it broken down like this, makes me wanna mess around with my own tweaks - you think the hardest part is just getting started or staying motivated long enough to finish?

Collapse
 
gofenice_technologies profile image
Gofenice Technologies

Honestly, I think both can be tough—getting started feels overwhelming at first, but staying motivated long enough to see those final results is where most people get stuck.

For me, what really helps is breaking things down into small, doable steps (like how I laid it out in this post!). That way, you see progress early on and it’s easier to stay pumped to finish.

If you’re thinking about tweaking your own site, maybe just pick one thing (like image compression) and start there—it’s super satisfying to see even a small boost in site speed!