Child pages
  • Understanding and using hooks

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

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

What is a

...

"hook"?

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’s display or events.

...

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

  • Action "hooks" (letting you, for example, send out a mail when a client creates a user account)
  • View "hooks" (enabling you, for example, to display a module in a column)

...

Tip

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

...

.

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

...

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

...

Next, it is important when installing your module to stick it to various “hooks” "hooks" desired. To do this, use the “registerHook” "registerHook" method, which also only accepts one parameter as well—the name of the hook.

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

...

Tip

There is no need to use the

...

module's "uninstall" method to remove the "hook".

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

...

Code Block
$params = array(
    'param_1' => 'value_1',
    'param_2' => 'value_2',
);
Module::hookExec('NameOfHook', $params);

...

Code Block
Hook::updateProduct(new Product(/* ... */));

...

Note

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?

How to add new one?

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

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”hook". (See 0 and 1 if it is compatible with LiveEdit or not)

...