- PrestaShop Module Development
- Fabien Serny
- 355字
- 2021-08-05 17:12:27
Registering our module on hooks
In your module, you will have to create an install
method and register all the hooks you want your module to be attached to in the method. In the case of the module we started in the previous chapter, we want to display grades and comments on product pages, so we have to attach the module to one of the hooks that are available on the product page, such as displayProductTabContent
.
Note
The displayProductTabContent
hook is a hook that permits to display content at the end of the product page. The exhaustive list of native hooks is available at the end of this book.
We will add the displayProductTabContent
hook with the help of the following code:
public function install() { parent::install(); $this->registerHook('displayProductTabContent'); return true; }
The parent install
method is doing some pretty important processes, such as adding the module in the ps_module
SQL table. So if you don't call it in your own install
method, your module won't be installable anymore.
Moreover, the registerHook
method needs the id_module
method of the installed module. That's why you have to make the parent install
method call before registerHook
calls. The return value of the install
method will indicate to PrestaShop whether the installation was successful. For now, we will return true
in all cases.
Then, you will need to write a function in your module named hook{hookName}
. In our case, it will be hookDisplayProductTabContent
(a display type hook).
The display type hook methods generally return HTML code that will be displayed on the hook location. For example, in this case, the return value will be displayed on the product page. Just to make a test, we will return the Display me on product page
string, as follows:
public function hookDisplayProductTabContent($params) { return '<b>Display me on product page</b>'; }
Note
We will see later what the $params
variable passed in the parameter contains.
Now, go to your back office and reset (install and reinstall) your module. Then go to your front office on the product page; you should see Display me on product page at the bottom of your product page:
