官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 都江堰市| 阳朔县| 游戏| 舒城县| 乌兰浩特市| 呈贡县| 咸阳市| 金沙县| 财经| 安康市| 明溪县| 囊谦县| 赣榆县| 汕头市| 和林格尔县| 曲麻莱县| 建阳市| 元谋县| 宁国市| 临潭县| 德清县| 大方县| 加查县| 内丘县| 英山县| 六枝特区| 诸城市| 黔西县| 胶南市| 安陆市| 城固县| 四平市| 肥西县| 许昌县| 岱山县| 虎林市| 浮梁县| 耒阳市| 松江区| 勃利县| 拉萨市|