Add New Hook In Prestashop

Home / Prestashop / Add New Hook In Prestashop

PrestaShop hooks are great way to insert or add data at the most important places or actions of this great e-commerce platform. But may be some time you want to use custom hooks with my custom modules. So here I show you how to implement custom hook?

How to create custom hook?

Step 1: Register a new hook in FrontController.php

open file \classes\FrontController.php and find below code

self::$smarty->assign(array(
'HOOK_HEADER' => Module::hookExec('header'),
'HOOK_TOP' => Module::hookExec('top'),
'HOOK_LEFT_COLUMN' => Module::hookExec('leftColumn')
));

Updated code

For add new hook I add this line of code

'HOOK_Top_Center' => Module::hookExec('topCenter') // New Hook 

So you code look like below.

self::$smarty->assign(array(
'HOOK_HEADER' => Module::hookExec('header'),
'HOOK_TOP' => Module::hookExec('top'),
'HOOK_LEFT_COLUMN' => Module::hookExec('leftColumn'),
'HOOK_Top_Center' => Module::hookExec('topCenter')
));

Step 2: All active hooks are hold in Prestashop database table called ps_hook. So I make query for new hook and run into database.

INSERT INTO 'yourdatabasename'.'ps_hook' (
'id_hook' ,
'name' ,
'title' ,
'description' ,
'position'
)
VALUES (
NULL , 'topCenter', 'Top of Center', NULL , '1'
);

What are field mean ps_hook.

`id_hook` = An auto increment value
`name` = Name of the new hook
`title` = This title is used in Modules tab in Back Office
`description` = The description of hook (optional)
`position` = Whether this hook observe the order of appearance with other module or not

Or you can add value manually from php myadmin.

open your “php_myadmin” and table “ps_hook” click on “insert” and insert ‘new’ hook name, ‘new hook’ in ‘title’

Step 3: Add code in your module:

Please create new function with name hooktopCenter in class of modules PHP file which you want to use.
‘hook’ is prefix function for hook
‘topCenter’ is name of hook postion

Here I call blockmanufacturer.tpl file in this funcation.

function hooktopCenter($params){
global $smarty, $link;

$smarty->assign(array(
'manufacturers' => Manufacturer::getManufacturers(),
'link' => $link,
'text_list' => Configuration::get('MANUFACTURER_DISPLAY_TEXT'),
'text_list_nb' => Configuration::get('MANUFACTURER_DISPLAY_TEXT_NB'),
'form_list' => Configuration::get('MANUFACTURER_DISPLAY_FORM'),
'display_link_manufacturer' => Configuration::get('PS_DISPLAY_SUPPLIERS'),
));
return $this->display(__FILE__, 'blockmanufacturer.tpl');
}

Step 4: Add the code {$HOOK_Top_Center} in theme/prestashop/header.tpl or any other tpl file where you want to use it.

<div id="header_right"> {$HOOK_Top_Center} </div>

Step 5: Then go to backend
=> Click on module top of link and configure any module which you want to change position and show in you new hook position.
=> Click position
=> Click on transplant module and select “topCenter” from dropdown and save.

Than look at your frontend at that position.

Leave a Reply

Your email address will not be published. Required fields are marked *