WEBDEVELOP - BLOG

PrestaShop modules and hooks.

Modules are integral part of PrestaShop e-commerce store. In default layout we can find variety of them for expanding the functionality of website. In every case there is dedicated place to display module and we call it hooks. You can easily manage modules and modules position.

In order to change module positions, you should go to Modules -> Positions. You will see hooks (positions) there. Just click on the arrow and drag the module down. The module position has been changed.

If you pay attention to hooks name you can easily noticed that we can count ‘Display’ and ‘Action’ type.

Display hooks (show something – for example show a module in a column)
Action hooks (do something – for example send an email after an action)

PrestaShop allow to create custom hooks what is really useful.

How to create custom hooks

Adding a display hook is very simple, and can be placed anywhere in a .tpl file.

{hook h='displayMyHookName'}

Next, within the code of your module, you will need to follow the standard practice of registering a hook on install of the module. In most cases You should only to extend existing method to your part.

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

In order for a module to respond to a hook call, the hook must be registered within PrestaShop. Hook registration is done using the registerHook() method. Registration is usually done during the module’s installation.

To attach your code to a hook, you must create a non-static public method, starting with the “hook” keyword followed by either “display” or “action“, and the name of the hook you want to use.

This method receives one (and only one) argument: an array of the contextual information sent to the hook.

function hookDisplayNameOfHook($params) {
// Your code.
}

Now, when you install your module, your content will be showing where you placed your custom hook within the .tpl. Don’t panic if your content is not showing up straight away, it is most likely a caching issue – We need to recompile the cache so our changes are shown.

First, navigate to your Dashboard and press on Modules and Services tab:

Select the Modules and Services item and search for Your module. And press on arrow next to “Configure” button in order to menu settings.

Here you can find “Clear” item, thanks to that you will be able to reinstall module.

A very simple process for truly customising your e-commerce site!