Skip to content

← Modules and development

Building your first module

From empty folder to an installable module that hooks in and shows something on the front.

5 slides 5 min read
  1. PrestaShop · Mòduls

    Your first module

    From empty folder to something visible on the front.

    PrestaShop · Modules and development arlaf.dev
  2. The smallest module that does something

    class HelloBlock extends Module
    {
        public function __construct()
        {
            $this->name = 'helloblock';
            $this->version = '1.0.0';
            parent::__construct();
            $this->displayName = $this->l('Hello block');
        }
    
        public function install()
        {
            return parent::install()
                && $this->registerHook('displayHome');
        }
    
        public function hookDisplayHome()
        {
            return $this->display(__FILE__, 'views/templates/hook/home.tpl');
        }
    }
    
    PrestaShop · Modules and development arlaf.dev
  3. Installing it

    Upload the folder to modules/, go to the back office, find it and click Install. PrestaShop calls install() and registers it on the displayHome hook.

    PrestaShop · Modules and development arlaf.dev
  4. The template

    hookDisplayHome returns a Smarty .tpl template. It's HTML with variables; here goes whatever you want to show on the home.

    PrestaShop · Modules and development arlaf.dev
  5. Giving it settings

    By implementing getContent() and a form, the module gains a settings screen in the back office. The client can then change texts without touching code.

    PrestaShop · Modules and development arlaf.dev
Read the full note

La millor manera d’entendre els mòduls és fer-ne un que faci una cosa visible. La fita: un bloc que aparegui a la home.

El mòdul mínim que fa alguna cosa

Amb la classe principal, un install() que registri el hook displayHome i un mètode hookDisplayHome() que retorni una plantilla, ja tens un mòdul que pinta HTML a la portada. És literalment això: registrar-se a un hook i respondre quan PrestaShop el crida.

Instal·lar-lo

Puja la carpeta a modules/, entra al back-office, busca el mòdul a la llista i prem Instal·lar. En aquell moment PrestaShop executa el teu install(), que crida parent::install() i registra el hook. Si tot va bé, recarrega la home i hi veuràs el bloc.

La plantilla

hookDisplayHome() retorna un .tpl de Smarty: HTML amb variables. Aquí decideixes què mostres. Mantén la lògica a PHP i deixa el .tpl només per pintar.

El següent pas: configuració

Implementant getContent() i un formulari (amb HelperForm), el mòdul guanya una pantalla de configuració al back-office. És el que converteix un mòdul rígid en una eina que el client pot ajustar sol —canviar un títol, una imatge, un enllaç— sense trucar-te. Aquest petit esforç és el que fa que un mòdul sigui realment útil en un encàrrec real.