Creating a Prestashop 1.7 hook

Prestashop has rapidly established itself in the world of CMS for digital commerce. It has even become the reference in the field, thanks to a simple, intuitive and modular interface that always adapts to the needs of its users. With a large and passionate community of developers worldwide, it offers the assurance of a viable solution, combining agility and security. Among the myriad functions on offer, Hook Prestahop is one of the most remarkable.

What is a Prestahop hook?

The hooks support the user’s work in terms of content display, but also in terms of processes and function calls linked to the merchant site’s lifecycle. They can be used to set up event-driven management and improve the organization and display of the various modules.

Display hooks

Above all, these anchors simplify front-end development by managing the placement of modules on final pages. In just a few lines, the Display Hook avoids exponential code complexity. Too much native code quickly leads to rereading difficulties, which can quickly result in the loss of control over all sources, turning any maintenance operation into an exploratory challenge.

Event hooks

There’s also the Action Hook, which enables continuous automation of most of the processes required to run an e-commerce business. This event-driven programming system can, for example, optimize the processing of a customer order by triggering its preparation as soon as payment has been received, or delete data from the database as soon as an unsubscription has been validated. By enabling calls to management procedures to be triggered on the fly, the entire logistics value chain is considerably accelerated, reducing operating and maintenance costs. At last, we’re free from the nightly routines of heavy data updates, forcing us to make our merchant sites unavailable on a regular basis, out of legitimate concern for data integrity. Business activity has regained power over IT tools, which are now conjugated in the present tense. For customers, suppliers and merchants, all flows are processed in real time.

How to create and use a Prestashop 1.7 hook

As soon as Prestashop 1.7 is installed, visual hooks are already implemented within the base theme. Here is the list of native Prestashop hooks:

  • header
  • top
  • leftcolumn
  • rightcolumn
  • home
  • footer

Any Prestahop module can therefore hook into any of these native view hooks. To do this, all you need to do is add to the module’s class the definition of a public method whose name is that of the Hook you wish to use, preceded by the keyword “Hook”. The only argument to this method is an array containing all the context variables required for proper execution:

public function hookNOMDUHOOK($params){}

All that remains to be done is to add a call to the registerHook function from the Hook class to the module’s installation method, with the Hook name in string as argument:

public function install(){ 

[...]

return::parent::install() && $this->registerHook('NOMDUHOOK');

[...] }

The call during the application lifecycle is made in two stages. In fact, the necessary context elements are defined the first time Hook Prestashop 1.7 is used, by defining the direct method :

$params = array(

'parametre1' => 'valeur1', 

'parametre2' => 'valeur2' 

[...] // le nombre de paramètres n'est pas limité 

);

Module::hookExec('NomDuHook', $params);

To make the hook simple to use, we need to simplify its call, so as to obtain readable code and better control of its contents. To this end, we need to add a shortcut to the HookCore class, transforming the direct method call into a simple function:

class HookCore extends ObjectModel

{ [...]

static public function NomDuHook($tableauContexte)

{

$params = array('tableauContexte' => $tableauContexte);

return Module::hookExec('NomDuHook', $params);

}

// ...

}

Calling up the Hook is as simple as this:

 HookCore::NomDuHook(new Product( [...] ) );

Event-driven programming becomes easier with this type ofstreamlined, easy-to-understand call.

Create new, fully customized Prestashop hooks

It’s likely that the original seven Hooks won’t be enough to satisfy all the needs and experimentations of these great tools. This is because it is not possible to create new anchors via the Prestashop 1.7 graphical interface. However, if defining calling methods seems an incredibly simple process, once you’ve tried and understood it, adding other custom Hooks is at least ten times simpler. The process actually requires just one instruction to be sent to the database. The addition simply consists of adding the Hook’s record row to the ps_hook table, putting in its name, title and description, in other words, the following query:

INSERT INTO `ps_hook` (`name`, `title`, `description`) 

VALUES ('NomDuHook', 'Nom de mon Hook', 'Description du Hook');

To conclude on Prestahop 1.7 hook creation

Using the Hook Prestashop 1.7 system is easy, and saves a considerable amount of time on all tasks, from code development and maintenance to live data processing, interfacing directly with events linked to the life of the merchant site. It’s a smart way to grow your business, gain agility and freedom.