Hooks in Prestashop

Hooks in Prestashop

Hook is a very interesting and important part of Prestаshоp system. To certain extent this is an implementation of the Observer pattern. Hooks are well described in the standard documentation.

Therefore today I will try to give some examples of use and describe the benefits you get when using hooks. Prestаshоp would never become so popular if not for its extensive number of modules and addons which help to extend default features.

Suppose you want to extend some standard functionality Prestashop offers 2 ways:

  1. You can either use the override-mechanism and overload the necessary function;
  2. Use hooks.

The second option is preferable, since in this case it is very unlikely that after you update the system to a new version your update will stop working. I can’t say outright what hooks have been deleted, but each update modifies a lot of functions. Therefore, when designing a new module I always pay attention to the controllers which are using hooks.

There are 2 types of hooks:

  1. Action. These hooks are triggered by specific events that take place in PrestaShop;
  2. Display. These hooks result in something being displayed, either in the front-end or the back-end.

Particularly useful may be those hooks which are invoked in the class ObjeсtMоdel:

These hooks can help to control events when updating, deleting or creating any types of objects (provided that they are inherited from the class ObjeсtMоdel), for example:

  • actionObjectProductAddAfter;
  • actionObjectProductDeleteBefore;
  • actionCarrierUpdate;
  • actionObjectFeatureValueAddAfter;
  • actionObjectAddAfter and so on.

In order to use a hook in its own module you need to call the function $this->registerHook($hook) in the function install( ). And then implement the method which will be triggered when the hook is called. For example:

Quite simple, isn’t it?

For better and profound understanding of the way hooks are functioning in Prestashop you can explore the class Hook. Here are the main methods of this class.

There are available certain methods which allow a store administrator to control hooks: Backend > Modules > Positions.

1

On this page you can change the position of a module inside a hook, remove a module from a hook or add a module as an exception for a specific page:

Hooks in Prestashop



3 comments

  1. Dear Alex,

    I have removed my “new products” hook from my shop. How I can rehook it? I want to have it back. I have searched alot on the internet but can not find anything in this regard :(

    Please help me out Thanks.

  2. Din,

    How have you removed the hook? New Products – this is not the hook, it is a module. Most likely you just need to reinstall it.

  3. Hi Alex,

    First of all, big thanks for your posts. They help me a lot :D

    I’m trying to install a “hookPaymentReturn” on my module in order to get some specifics parameter values returned by any other payment module that could be executed.

    I can see my module hook on the “ps_hook_module” table at 2nd position, the activated payment module executes its own “hookDisplayPaymentReturn”, but the one I’ve installed is not being executed.

    Thank you boss

Post a new comment

top