How to Add Hooks to Prestashop 1.5 (the new way)

Prestashop 1.5 introduced a new way to add hooks, which doesn’t involve overrides or database queries. Unfortunately, it’s not yet been documented in the official Docs, so let’s see how to take advantage of it!

Watch the screencast

Text version

The old VS the new hooks syntax

If you were used to adding hooks to Prestashop 1.4, you might remember having to write something like this

INSERT INTO `ps_hook` (`name`, `title`, `description`) 
VALUES ('category', 'Category', 'Hooks before the product list in a category');

Into your database to add the hook, and then override one of the controllers to register its execution, like this:

Class FrontController extends FrontControllerCore
{

    public function displayHeader()
    {
        if (!self::$initialized)
             $this->init();

            self::$smarty->assign('HOOK_CATEGORY', Module::hookExec('category'));

        parent::displayHeader();        
    }

}

And this was SO bad, forcing us to use overrides for almost every place which didn’t have its own hook already.

Luckily, but “secretly” for some reason, a new and simpler way has been introduced, and that is, substantially, adding this to a template file:

	{hook h='nameOfTheHook'}

I discovered it by chance actually, scrabbing throughout the forums, from a post of one the Prestashop Core’s developers: Fabien Serny (reference)

Example: adding a new hook for the first time

For the sake of this example, we’ll just be writing some piece of text above the footer. Therefore, open up themes/default/footer.tpl, and right before {$HOOK_FOOTER}, add the following

	{hook h='beforeFooter'}

The name is, of course, discretional. Nothing else has to be done actually, but registering the hook as any other existent hook. Open up any module you have installed, for example blocknewsletter.php. I’ll be using this for demonstrational purposes only, you can use any. At the end of the file, add a simple hooking function:

	
public function hookBeforeFooter($params)
{
	return 'hi there!';
}

The function is now there, but this module is not registered as being part of this hook. We won’t be able to position it to the new hook from the back office yet. The first time you create the hook, it has to be registered at least once, from a module. We can use a regular registerHook for this, as any other hook. In the install() function of the module, add the registration, so it looks like this:

	if (parent::install() == false || $this->registerHook('leftColumn') == false ||  $this->registerHook('beforeFooter') == false || $this->registerHook('header') == false)
		return false;

Again, this is just an example, the important part is the following one (it will look familiar if you know how to create modules and register hooks; if it doesn’t, have a look at my previous tutorial about Prestashop Hooks, as the basic ideas are still valid):

	$this->registerHook('beforeFooter')

Go back to the modules panel and reset the module (or install it, if it wasn’t). Refresh the front office, and you’ll see the “Hi there!” text appear where it’s supposed to be.

Isn’t this a HUGE improvement? Yay, it is. But if you still use Prestashop 1.4, you can go the old way with my module EZ Hooks, and create new hooks in 1 click.

Looking for quality PrestaShop Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , | Posted under PrestaShop | RSS 2.0

Author Spotlight

Fabio Porta

Fabio Porta

Fabio has been involved in web development and design since 2005, when launched his first website at the age of 16. He’s now highly skilled in both client and server side development, along with design, and since August 2012 runs a successful website about PrestaShop tutorials and Prestashop Modules called Nemo’s Post Scriptum, at http://nemops.com

2 Comments on How to Add Hooks to Prestashop 1.5 (the new way)

  1. Amazzing says:

    This one is great.
    code
    {hook h=’nameOfTheHook’}
    /code
    but how can you check wether this hook is used ot not?
    The standard check used in other tpls doesn’t work
    code
    {if $HOOK_EXTRA_LEFT}{$HOOK_EXTRA_LEFT}{/if}
    /code

Leave a Reply

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


× 4 = 4

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>