Message-ID: <1448362246.377320.1711621056119.JavaMail.root@confluence-doc2-production> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_377319_1661509904.1711621056114" ------=_Part_377319_1661509904.1711621056114 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html Understanding and using hooks

Understanding and using hooks

Un= derstanding and using hooks

=20

This article was written by Julien Breux, and published on May 5th, 2011 on the Pr= estaShop blog.

=20

What is a "h= ook"?

=20

As you've probably noticed, PrestaShop is a software that allows you to = create modules designed to interact directly with the heart of the solution= =E2=80=99s display or events.

=20

The "hooks" allow you to retrieve these events or change the display.=20

So there are actually two distinct types of "hooks".

=20 =20

Sometimes the "hook" view can also serve as an action "hook." You do not= need to do anything to display them. For example, you can make a recurring= task on the homepage with the home hook.

=20

If we take the example of the basic PrestaShop theme, on the home page, = the solution uses "fixation points" as follows:

=20
=20 =20 =20 =20 =20 =20 =20 =20 =20 =20 =20 =20 =20 =20 =20 =20 =20 =20 =20 =20 =20 =20

Hook name

header

top

leftColumn

home

rightColumn

footer

=20

As you can see, all the "hooks" used are "view hooks".

=20

This means that each of your modules can independently hang on to them a= nd display information.

=20

How do I use them?=20

First of all, to correctly use the "hook," you must go into your module'= s class and create a non-static, public method starting with the keyword "h= ook" and the name of "hook" used.
Then, only one single argument is pas= sed: the array of different context information sent to the "hook".

=20
=20
public function hookNameOfHook($params)
{
}
=20
=20

Next, it is important when installing your module to stick it to various= "hooks" desired. To do this, use the "registerHook" method, which also onl= y accepts one parameter as well=E2=80=94the name of the hook.

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

There is no need to use the module's "uninstall" method to remove the "h= ook".

=20

Finally, it is important to understand how to call these "hooks" in orde= r to create new ones later.

=20

There are two calls in PrestaShop for "hooks." The second call is based = on the first.

=20

The first call is the direct method. It takes two arguments: the name of= the "hook" and an array of different context information.

=20
=20
$params =3D array(
    'param_1' =3D> 'value_1',
    'param_2' =3D> 'value_2'
);
Module::hookExec('NameOfHook', $params);
=20
=20

The second call is a "shortcut" to the first in order to have a "cleaner= " display when making the call. The set of "shortcuts" are available in the= class "Hook."

=20
=20
class HookCore extends ObjectModel
{
    // ...
    static public function updateProduct($product)
    {
        $params =3D array('product' =3D> $product);
        return Module::hookExec('updateProduct', $params);
    }
    // ...
}
=20
=20

Calling the "hook" called "updateProduct" will therefore be as follows i= n the heart of PrestaShop.

=20
=20
Hook::updateProduct(new Product(/* ... */));
=20
=20

We have called the class "HookCore" using "Hook" as the class name. This= is due to the override that we will go over next time!
How to add new = one?

=20

How to add new one?=20

If so far you've followed how to use "hooks" and how to use them in Pres= taShop, then you'll surely you realize that you're missing some "hooks" lik= e "MakeCoffe", "MakeFood" or "RespondToClientsForMe". Don't panic!

=20

To create your own little personal "hook", you just simply save a row in= the database table "ps_hook" with the name of your "hook". (See 0 and 1 if= it is compatible with LiveEdit or not)

=20
=20
INSERT INTO `ps_hook` (`name`, `title`, `description`)=20
               VALUES ('nameOfHook', 'Name Of Hook', 'It is a custom hook !=
');
=20
=20

And finally, just use it as we have done in this article !

------=_Part_377319_1661509904.1711621056114--