In real life Hooks are used to hang in our clothes in a wardrobe or to catch fish, Prestashop Hook(s) are the just the same, which we use to hang 😀 (transplant or display) modules. Prestashop provides a comprehensive list of hooks for modules. But sometimes you may need a module need to be placed in a specific place and for your bad luck, there is no hook available for that place. So what will you do now? The solution is easy and simple, all you need is a new Prestashop hook, alternatively you may work out some custom code placement which which is certainly a bad idea. So your only best option is to create a new Prestashop hook. So how your are going to add a new Prestashop hook, let me show it to you:
1) Suppose we want to create a Prestashop Hook called “welcomeHook”. For naming we don’t have any restriction, but it is a good practice to use CamelCase style for Prestashop hook names and avoid spaces. Now Open the Prestashop database, and find the “ps_hook” table. Please note that your Prestashop installation table prefix may be different than “ps_”, so find out the “hook” table.
2) Now make a new entry in the table using your choice MySQL tool like phpMyAdmin or SqlYog. For id_hook, leave it blank, as it will be auto generated by MySQL. For name use “welcomeHook“. This name is used to get the installed modules for that hook. For title use any of your choice title and so for description. For position keep it 1 and for live_edit place 0 or 1. Also you can just run the below query and it will insert the new prestashop hook for you in the database.
INSERT INTO ps_hook VALUES('', 'welcomeHook', 'Our Welcome Hook', 'This is our test welcome hook', 1, 0)
3) Now your Prestashop hook is ready to use. You can call this hook by the following code.
Now you have the hook data in the $welcomeHook, you can display it in your template files or you can insert some new feature to the existing code section where you have called this Prestashop Hook. The exec function gets all the module(s) installed for that hook.
So was it difficult adding a new Prestashop Hook ? 🙂