How create a custom prestashop module

How create a custom prestashop module

first module prestashop custom module

This guide will help you create a simple "Hello World" module for PrestaShop.

Step 1: Set Up Your Module Directory

  1. Create Module Directory:

    • Navigate to your PrestaShop installation directory.
    • Go to the modules directory.
    • Create a new directory for your module, e.g., customhelloworld.
  2. Create Main PHP File:

    • Inside your module directory, create a PHP file named customhelloworld.php.

Step 2: Define the Module

Edit the customhelloworld.php file to define your module. Add the following code:

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

class CustomHelloWorld extends Module
{
    public function __construct()
    {
        $this->name = 'customhelloworld';
        $this->tab = 'others';
        $this->version = '1.0.0';
        $this->author = 'Your Name';
        $this->need_instance = 0;

        parent::__construct();

        $this->displayName = $this->l('Custom Hello World');
        $this->description = $this->l('A simple Hello World module.');
    }

    public function install()
    {
        return parent::install();
    }

    public function uninstall()
    {
        return parent::uninstall();
    }

    public function hookDisplayHome($params)
    {
        return $this->display(__FILE__, 'views/templates/hook/displayhome.tpl');
    }
}
?>

Step 3: Create a Template

Create Template Directory:

  1. Inside your module directory, create the following directory structure: views/templates/hook. Create Template File:
  2. Inside the hook directory, create a file named displayhome.tpl. Add Template Content:
  3. Edit displayhome.tpl and add the following content:
<div class="hello-world">
    <h1>Hello, World!</h1>
</div>

Step 4: Register the Hook

To display the "Hello World" message on the homepage, you need to register the hookDisplayHome in the module class.

Add the following code to the install method in customhelloworld.php:

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

Step 5: Install the Module

  1. Upload the Module:

    • Compress the customhelloworld directory into a ZIP file.
    • Install via PrestaShop Admin:
  2. Log in to your PrestaShop admin panel.

    • Navigate to Modules -> Module Manager.
    • Click on Upload a module.
    • Upload the ZIP file you created.
    • Install the module.