Understanding how PrestaShop hooks work
Hooks are the core mechanism that lets a module add behaviour to PrestaShop without touching the core code. Understanding how they work changes how you assess whether a need can be met simply, or requires heavier intervention.
How it works in practice
PrestaShop runs its code normally, and at certain precise points in the flow, it calls every module registered on the corresponding hook, giving them the chance to add content or run an action. A display hook such as displayHeader lets a module insert CSS or JavaScript into the header of every page, for instance. An action hook such as actionValidateOrder fires right after an order is validated, letting a module send a notification, sync external stock or trigger a webhook.
Each hook corresponds to a precise moment and an expected type of content or action: some are meant to display HTML at a spot in the template, others to run an action without displaying anything. Using a display hook to run a heavy action, or the reverse, is a common source of bugs.
The list of available hooks changes from one PrestaShop version to the next: some hooks that exist in an older version disappear or get renamed in a major upgrade, which is why an old module can stop working after an update.
Identifying the right hook for a given need
-
Describe the exact moment you need
First of all: is it when a page loads, after an order is validated, or when a product is added to the cart? Pinning down the exact trigger points directly to the relevant family of hooks.
-
Distinguish display from action
A display need (adding a block, a message, a widget) and an action need (sending an email, calling an external API, changing data) don't call for the same type of hook.
-
Check the official documentation for the target hook
Each hook expects and provides different parameters (for example the order object for an order-related hook), which you need to know before writing the module's code.
-
Test the actual trigger
A hook that looks right on paper can fire at a slightly different moment than expected; a concrete check (for example via a log or debug mode) confirms the actual behaviour before you build all your logic around it.
public function install()
{
return parent::install()
&& $this->registerHook('displayHeader')
&& $this->registerHook('actionValidateOrder');
}
public function hookDisplayHeader($params)
{
return $this->context->smarty->fetch('module:mon-module/views/templates/hook/header.tpl');
}
Signs of a hook-related problem
- A block added by a module that appears twice or in the wrong spot on a page
- An action that's supposed to fire after order validation but never happens
- A module that worked fine before a PrestaShop update and has stopped displaying
- Two modules that seem to conflict over the same display slot
Frequently asked questions
Can the same hook be used by several modules at once?
What happens if no hook matches the need?
How can you tell which hooks a module is already registered on?
Describe your need in one minute
A few targeted questions so I can reply with an estimate rather than another questionnaire.