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:
- You can either use the override-mechanism and overload the necessary function;
- 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:
- Action. These hooks are triggered by specific events that take place in PrestaShop;
- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Hook::exec('actionObjectAddBefore', array('object' => $this)); Hook::exec('actionObject'.get_class($this).'AddBefore', array('object' => $this)); … Hook::exec('actionObjectAddAfter', array('object' => $this)); Hook::exec('actionObject'.get_class($this).'AddAfter', array('object' => $this)); … Hook::exec('actionObjectUpdateBefore', array('object' => $this)); Hook::exec('actionObject'.get_class($this).'UpdateBefore', array('object' => $this)); … Hook::exec('actionObjectUpdateAfter', array('object' => $this)); Hook::exec('actionObject'.get_class($this).'UpdateAfter', array('object' => $this)); … Hook::exec('actionObjectDeleteBefore', array('object' => $this)); Hook::exec('actionObject'.get_class($this).'DeleteBefore', array('object' => $this)); … Hook::exec('actionObjectDeleteAfter', array('object' => $this)); Hook::exec('actionObject'.get_class($this).'DeleteAfter', array('object' => $this)); |
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:
1 2 3 4 |
public function hookActionObjectAddAfter($params) { return $this->writeLog(get_class($params['object']), $this->l('Add #') . $params['object']->id); } |
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.
1 2 3 4 5 6 7 8 |
public static function getHooks($position = false) //Return Hooks List public static function getIdByName($hook_name) //Return hook ID from name public static function getHookAliasList() //Get list of hook alias public static function getRetroHookName($hook_name) //Return retrocompatible hook name public static function getHookModuleList() //Get list of all registered hooks with modules public static function getModulesFromHook($id_hook, $id_module = null) // Return Hooks List public static function getHookModuleExecList($hook_name = null) //Get list of modules we can execute per hook public static function exec($hook_name, $hookArgs = array(), $id_module = null) //Execute modules for specified hook |
There are available certain methods which allow a store administrator to control hooks: Backend > Modules > Positions.
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:
PrestaShop Development
Take your online store to the next level with BelVG PrestaShop Development
Visit the page
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
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.
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.