In this blog, we are going to learn how we can create and use a custom hook in PrestaShop. PrestaShop already has various hooks on different locations of code, which helps to customize or perform any action under certain circumstances.
- action
- display
action: Action hook is written like "actionDoSomething"
here, prefix action indicates hook is triggered by an event.
display: Display hook is written like "displaySomething"
here, prefix display indicates hook is displayed on something, either in the back office or front office.
For creating a custom hook in PrestaShop we follow the same method.
Firstly, declare the function in the module main class file as written below,
public function hookDisplayBelowCustomerLoginForm() { return $this->fetch('module:demo/views/templates/hook/cusLogin.tpl'); } public function hookActionAdminLoginSubmit() { // do something return true; }
Here we are showing you an example of a display hook used to show some extra content below the fields of customer login and doing some action on admin login.
Secondly, you have to register this hook in your module while installing the module as given below
public function install() { $this->registerHook('displayBelowCustomerLoginForm'); $this->registerHook('actionAdminLoginSubmit'); return true; }
Finally, you have to call this hook from the location where you want to display it. In this example, we are adding a display block between customer login form so we added hook calling in the below fileprestashop/themes/classic/templates/customer/_partials/login-form.tpl
.......... <div class="forgot-password"> <a href="{$urls.pages.password}" rel="nofollow"> {l s='Forgot your password?' d='Shop.Theme.Customeraccount'} </a> </div> </div> <strong>{hook h='displayBelowCustomerLoginForm'}</strong> {block name='login_form_footer'} <footer class="form-footer text-sm-center clearfix"> .......
We want to execute the action hook while the admin login so we added it below file as
prestashop/controllers/admin/AdminLoginController.php
public function processLogin() {..... <strong>Hook::exec('actionAdminLoginSubmit');</strong> if (!count($this->errors)) { ....
You can also pass parameters to these hooks as given below
In tpl {hook h='displayBelowCustomerLoginForm' id_module=$id_module}
In twig {{renderhook('
‘displayBelowCustomerLoginForm
', {id_module': id_module
})}}
and fetch it in function as
public function hookDisplayBelowCustomerLoginForm($params) { $idModule = $params['id_module']; return $this->fetch('module:demo/views/templates/hook/cusLogin.tpl'); } public function hookActionAdminLoginSubmit($params) { $idModule = $params['id_module']; $idEmployee = $params['id_empoloyee']; // do something return true; }
That’s, all for the custom hook in PrestaShop.
If you are facing any issues or doubts in the above process, please feel free to contact us through the comment section.
I would be happy to help.
Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.
For any doubt contact us at [email protected].
Be the first to comment.