Back to Top

Custom hook in PrestaShop

Updated 29 April 2022

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.

Custom hook in PrestaShop
Custom hook in PrestaShop
  • 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,

Searching for an experienced
Prestashop Company ?
Find out More
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 file
prestashop/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].

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home