Quick Tip: Adding 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 offiacial 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.

You like the tuts and want to say "thank you"? Well, you can always feel free to donate:

  • Bara Anton Florian

    This was very useful! Thank you for taking your time to share this knowledge.

  • Brewal

    I recommend using this method in order to override module’s bootstrap php :

    https://github.com/FlYos/Prestashop-override-module

    way better that directly modifying the php of the module itself.
    In my opinion, prestashop positionning system really sucks by the way… We should be able to place any module anywhere directly into the admin, and just create hooks in templates.

  • lui1969

    hello Nemo, do you think it is possible to do such a thing in the top menu?

    I’ll leave you the link of the forum prestashop

    http://www.prestashop.com/forums/topic/300907-add-new-hook-in-top/

    thanks

  • http://www.ebene-magazine.com Baron

    Hi all!!!!!!
    I just change the Homeslider hook.
    It return a value, but i can’t see any pics.

    Any idea?

  • http://openglized.blogspot.com Dr. V

    Thanks a lot for your tutorial!!!
    I went a bit further with this.
    You can make it show on the “positions” of your Prestashop, so you can add more modules easily to your hook.
    Just add :

    public function install()
    {
    if (!parent::install() || !$this->registerHook(‘top’) || !$this->registerHook(‘beforeFooter’) || !$this->registerHook(‘header’) || !$this->registerHook(‘displayMobileTopSiteMap’))
    return false;

    //register the hook on positions
    Db::getInstance()->execute(‘UPDATE ‘._DB_PREFIX_.’hook SET position=”1″ WHERE name = “beforeFooter”‘);

    return true;
    }

    This will modify your database and set the “position” value to 1.
    If you want to make it appear on the “live edit” just update “live_edit” to 1 on the same table.

    Cheers.

    • http://openglized.blogspot.com Dr. V

      By the way, don’t forget to reset the module on the backend, or it won’t work :-)

  • http://bigredradish.com BigRedRadish

    Hi

    I’m fairly new to Prestashop. Thanks for the great tip! I have successfully created a new hook, but I am not able to transplant modules in to it. Is there something else I need to do to be able to transplant modules into the hook?

    Thanks for all you great tutorials, they have saved me a lot of time!

  • http://none.org m.winkler

    adding css or js to the hook is not working;

    $this->context->controller->addJS( …
    $this->context->controller->addCSS(…

    doesn’t show up in the page anymore

    • http://nemops.com Nemo

      THose functions should only be used in hookHeader actually

  • Yasic

    Hi, i’ve tried this with the newsletter module and i get the “hi there” message and the module is planted in the hook (i called it before footer too) but i dont see how the newsletter module itself is being loaded. all i have is the “hi there”.
    how do i get the newsletter box to show up?

    thanks

  • Michael K

    Hello,
    I followed the video tut and it works well. However, I came across an issue where I wanted to transplant the default Layered Navigation module into the categories.tpl file. The location of the new hook is above the product list, and it is located inside a table > tr >div. But when I refresh the page, it shows up, but not where it supposed to be. It shows outside the table.

    Any ideas?

  • http://www.connectcase.nl Cees Rijken

    Being a 1.5 newbie, where do I put this line:

    $this->registerHook(‘beforeFooter’)

    The other lines of code I figured out, but this one confuses me….

    Cheers!

    • http://nemops.com Nemo

      That should be put in the install function of the module :)

  • http://pelechano.es Enrique

    Thanks for the tutorial and the screencast. You are doing a great job

  • Xezus

    Hi Nemo,

    is this way of creating hooks still working? Because I tried to do this in my prestashop 1.5.4 (before in 1.5.3.1), but it didn’t work. The hook was created successfully but I couldn’t hook nothing insite.

    • http://nemops.com Nemo

      I haven’t tested it on 1.5.4 but it would b really stupid from them to remove a new feature already! Maybe they added another bug, sad

      • Xezus

        No it’s still work. I found my error. I just missed something. :)

  • http://www.lancastrian-it.co.uk/ shoulders

    what is your favourite editor you are using in this video?

    thanks

  • http://www.toweringmedia.com Kris

    You did not explain what to do after you get the hi there! message.

  • http://eurekawebsitedesign.com David

    Brilliant lesson for adding in hooks. Presta shop changes so fast it can take hours of looking on their forums for modsand that’s if you can find it.
    This site is oustanding and I highly recommend it. Added to my favs.

    Thank you. Much appreciated!

You like the tuts and want to say "thank you"? Well, you can always feel free to donate:

Need Help?

Hire me