Hooks in PrestaShop 9: how they work, which to use and how to create your own
Hooks are the clean way to extend PrestaShop without touching the core. We explain the two types, how to register them properly and the most common silent failures.
A hook is an attachment point that PrestaShop leaves in its code so that a module can add content or react to an event without modifying the core. It is the piece that lets a store run a hundred modules and still be updated. If you develop for PrestaShop, understanding hooks properly saves you half your problems.
Two families: display and action
- Display hooks (
display*): return HTML that is rendered in a specific place on the page. For exampledisplayProductAdditionalInfoon the product page ordisplayFooterin the footer. - Action hooks (
action*): render nothing; they run when something happens.actionValidateOrderwhen an order is created,actionObjectProductUpdateAfterwhen a product is saved.
There are also hooks that return data for the core to combine, such as paymentOptions, where each payment module hands over its PaymentOption objects.
Registering a hook correctly
Registration happens when the module is installed and is stored in the hook_module table:
public function install()
{
return parent::install()
&& $this->registerHook('displayProductAdditionalInfo')
&& $this->registerHook('actionValidateOrder');
}
public function hookDisplayProductAdditionalInfo(array $params)
{
$this->context->smarty->assign(['product' => $params['product']]);
return $this->fetch('module:' . $this->name . '/views/templates/hook/info.tpl');
}
The method is named hook + the hook name with its first letter capitalised. PrestaShop ignores case in the hook name, but the PHP method does have to exist.
install() is not run again. You have to do it in an upgrade/ script.Positions and exceptions
When several modules hang off the same hook, they run in the order of the position column. You change it in Design > Positions, where you can also exclude specific pages for a module. If a block appears where it should not, look there first before touching any code.
Creating your own hooks
A theme or a module can call a hook that does not exist in the core: {hook h='displayMiTiendaCabecera'} in Smarty or Hook::exec('displayMiTiendaCabecera') in PHP. The first time a module registers it, PrestaShop creates it in the hook table. It is the cleanest way to offer extension points in your own developments, and the one we use, for example, so that each section of a home page can be edited separately.
Why a module “renders nothing”
- The module is installed but not registered on that hook (check Positions).
- The theme does not call that hook. A Classic hook may not exist in Hummingbird, or may sit in a different area.
- There is a page exception configured.
- The module is disabled for that shop: in PrestaShop, “enabled” depends on the row in
module_shop, not just on theactivecolumn. - The module template is overridden in the theme and that copy is empty or out of date.
Good practice
- Return empty strings in display hooks when there is nothing to show: do not output empty containers that break the layout.
- In action hooks, do not throw uncaught exceptions: an error there can block an order from being saved.
- Cache in hooks that run on every page (
displayHeader,displayTop): they are multiplied by every visit.
Hooks used well let a module survive PrestaShop updates and theme changes. When someone suggests an override for something that can be done with a hook, be wary.