How to create custom PrestaShop hook

PrestaShop hooks are great way to insert or add data at the most important places or actions of this great e-commerce platform. However, I am not satisfied with the quantity of default hooks and I sometimes use custom hooks with my custom modules. So how to implement custom hook and why not hard code all code without it?

Why hook is the right choice?

There is one big problem in my opinion with custom hooks because you need to hard code their position into the files that might be affected by CMS updates or etc. So it is just more easier to add one line of code than to copy and paste a bunch of code or even all page of it.

Secondly, hooks are easy used in modules which lets you save time and avoid headaches. You do not need to think about default page code as you just add addition to it, for example, you will not delete necessary code by accident.

Thirdly, it is just a good practice and manner of good and professional programmer.

How to create custom hook?

All active hooks are hold in Prestashop database table called ps_hook (ps_ is default database prefix and it will be on this article too), so you have to add it on module installation.

        DB::getInstance()->execute("INSERT INTO `"._DB_PREFIX_."hook`
                                    SET `name`= 'nameYourHook',
                                        `title`= 'Hook Title',
                                        `description`= 'Hook description'
                                   ");

After you added hook information to the database, you must register it for later you which you do by calling this method:

$this->registerHook('nameYourHook');

Now you can easily insert your hook anywhere in the code.

However, it depends if you want it to execute raw PHP code or it will return a template file. There are two options and both are shown below.

Module::hookExec('nameYourHook');

With Smarty:

$smarty->assign(array(
        'MY_HOOK' => Module::hookExec('cat')));
$smarty->display(_PS_THEME_DIR_.'template_file.tpl');

If you use the example with smarty, you must add {$MY_HOOK} variable where you want into your .tpl file that gets this assign. That’s about it.

Now you can add whatever you want to hooks function which you write like this:

function hookNameYourHook($params) {
         // Your hook actions comes here
}

Try it yourself.

Leave a comment

Comments

  • senkal
    December 9th, 2010 / 6:40 AM
    Reply

    Really helpful post. I am also unsatisfied with default hooks.
    Thanks.

  • Jhon
    September 28th, 2011 / 7:49 PM
    Reply

    thanks for this helpful post. It’s been really useful to understand hook system in Prestashop.

    Great help thanks for sharing us

  • glowingsword
    January 22nd, 2012 / 8:05 PM
    Reply

    Thank you for this post. It is very helpful post for me. Add +1 button for this post, please. I’m don’t use this favorite social bookmarking sites. But I’m use +1 from google, and want to share this post for my friend(he is freelancer and work with prestashop too).

  • devil
    March 25th, 2012 / 11:46 AM
    Reply

    you blog is nice it will relief solution. But in prestashop 1.4.7.0 it does not works. While I execute in header.tpl. But it shows and undefined MY_HOOK

    • Richard Smaizys
      April 6th, 2012 / 9:28 AM
      Reply

      It should work just fine on 1.4.7 You did something wrong. Have you looked at the naming and have you properly added the hook to the database?

  • Lucas Fogolin
    July 2nd, 2012 / 6:10 AM
    Reply

    Great post!

    I’ve been working on it for two weeks now and can’t manage to get it right without having to go to MySQL and insert the values by myself…

    The best post I could find was this one: http://blogs.callosmart.com/2011/03/30/creating-a-prestashop-1-4-theme-from-scratch-part-5-delighttheme-module/

    But, despite he whows almost everything, I coud not make it work, and I like the way you treated it, simple, light, you know? Seems better… But being new to prestashop I don’t understand where these codes go: in one file? Where? A whole new module? Could you send me the compiled file?

    Thank you for all!

    Kind regards

  • pkr
    July 19th, 2012 / 10:16 PM
    Reply

    in which file should I put this code?

    $smarty->assign(array(
    ‘MY_HOOK’ => Module::hookExec(‘cat’)));
    $smarty->display(_PS_THEME_DIR_.’template_file.tpl’);

    I want to create a hook and assign blockcategories to this hook. I must use Smarty to put categories menu into right place corectly. Can you help me?

    • Richard Smaizys
      April 27th, 2013 / 6:35 PM
      Reply

      If it is in a module, then you should add this to the module function that is responsible for the hook.

  • Javier
    April 24th, 2013 / 5:21 PM
    Reply

    How can I add a hook for a special action like the user updates his/her profile?
    I can not find any information on how to do it.

    Thanks

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. Cookies are only used for statistical purposes.

Close