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

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:

主站蜘蛛池模板: 阿尔山市| 镇远县| 应城市| 洮南市| 克拉玛依市| 祁连县| 吉林市| 泗阳县| 武强县| 远安县| 芒康县| 南城县| 南丹县| 徐水县| 霞浦县| 陆河县| 大埔县| 靖远县| 宣城市| 昔阳县| 丰宁| 泾阳县| 霞浦县| 嘉黎县| 铜山县| 克山县| 安徽省| 宁武县| 桑植县| 沧州市| 喀喇沁旗| 唐山市| 盱眙县| 海淀区| 准格尔旗| 六枝特区| 新密市| 广昌县| 武川县| 庆城县| 广丰县|