Wordpress Code & Research

Icon

All things about wordpress

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

1
2
3
4
5
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

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

So you code look like below.

1
2
3
4
5
6
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.

1
2
3
4
5
6
7
8
9
10
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
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.

1
<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.

Advertisements

Filed under: Prestashop

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: