Hooks in PrestaShop: what is it and why is it needed?

Monday, 1 July 2019 15:37/ Posted by Misha Pyskur in Prestashop 1.6

What are the hooks?

 

If short — the hook is a function that allows you to modify certain blocks, add other elements to them without changing the root files of your site. In the e-commerce engine, PrestaShop hooks (other names are the hook, anchor) are special places in the PrestaShop template where events or certain actions of your modules will take place that runs in the background or are visible visually. This may be, for example, displaying a callback button in the header of your store, displaying the information you need on the product card, displaying the product slider, etc. Simply put, the system allows you to create PrestaShop modules that are designed to change the displayed content or respond to events.

 

In total, hooks in PrestaShop can be divided into 2 types:

 

  • Event handlers (for example, sending an email when registering a user or notifying about ordering);
  • Visual hooks (designed to display content, for example, in a header or footer).

 

In the standard theme PrestaShop 1.6 (we will use this version as an example) for the main page, we can distinguish the following visual hooks:

 

prestashop_hooks_table

 

 

How to use the hook in a modules?

 

To properly use the hook in the required class of our module, you need to create a non-static public method whose name begins with the keyword «hook» and the name of the hook being used (for example, for the hook «Footer» the method will be «hookFooter»). In this method, we will pass only one argument  the array of parameters that depend on the hook: 

 

public function hookDisplayMyHookName($params)
 {
   // your hook code here
 }

 

To call this method in a hook, you must register it with «registerHook ()». Usually, registration is done when the module is installed:

 

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

 

When you remove a module, PrestaShop will remove the registration itself.

 

 

How to use the hooks in the controller?

 

To call our hook in the controller, you just need to write its name with «Hook :: exec()»:

 

Hook :: exec ('MyHookName', $ params);

 

 

How to add your personal hook?

 

If you need to create your hook, you can create it when you install the module:

 

public function install()
 {
  $this->registerHook('MyNewHook');
 }

 

And add to the module function:

 

public function hookMyNewHook()
 {
  // your hook code here
 }

 

After this, in the right place of our template, paste this code:

 

{hook h='MyNewHook'}

 

 

Conclusion

 

As we can see, the use of hooks greatly enhances the functionality of your site. Working with hooks in our retailer shop is very convenient if you have basic programming knowledge. PrestaShop Modules with personal hooks perfectly emphasize the individuality of your store, allow you to interact with customers better and open up many opportunities. In the following articles, we will become more familiar with the modifications of our PrestaShop modules and PrestaShop templates, learn even more secrets of pre-party and learn how to use the engine's capabilities to the maximum.