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.

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

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.

With Smarty:

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:

Try it yourself.

, , ,

About Richard Smaizys

I am a freelance PHP developer working mostly on projects using PrestaShop for ecommerce (modules, theme integration, projects from 0 etc.) and WordPress as CMS solution.

11 Responses to “How to create custom PrestaShop hook”

  1. senkal December 9, 2010 at 6:40 am # Reply

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

  2. Jhon September 28, 2011 at 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

  3. glowingsword January 22, 2012 at 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).

  4. devil March 25, 2012 at 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 6, 2012 at 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?

  5. Lucas Fogolin July 2, 2012 at 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

  6. pkr July 19, 2012 at 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 27, 2013 at 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.

  7. Javier April 24, 2013 at 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

  8. Bruna Cunha April 27, 2013 at 5:13 pm # Reply

    Do you have an example of the whole code?

    Because i don’t know where to put this lines…

    Thanks a lot!

Leave a Reply