Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Action hooks

An action hook reports an event: an order is placed, a product is saved, a customer registers. The names of such hooks start with action. Unlike a display hook, the handler returns nothing to the page — it does a job: writes to the log, sends an email, updates its own table.

Subscribing

The subscription is the same as for display hooks — in install() of the main class:

// modules/acmehello/acmehello.php
public function install(): bool
{
    return parent::install()
        && $this->registerHook('displayHome')
        && $this->registerHook('displayFooterProduct')
        && $this->registerHook('actionValidateOrder');
}

The actionValidateOrder hook fires at the moment an order is created.

The handler

// modules/acmehello/acmehello.php
public function hookActionValidateOrder(array $params): void
{
    $order = $params['order'];
 
    PrestaShopLogger::addLog('New order ' . $order->reference, 1);
}

The engine passes the event data in $params. Here it is the order object, from which the reference is taken. PrestaShopLogger::addLog() writes a line to the shop log; the second argument is the severity: 1 is a message, 3 is an error.

How to see the result

Reset the module so that the subscription takes effect:

php bin/console prestashop:module reset acmehello

Place a test order in the front office and open "Advanced Parameters → Logs" in the back office: a "New order" entry with the reference appears in the list.

What comes in the parameters

Every hook has its own set of keys in $params.

HookWhen it firesKeys in $params
actionValidateOrderAn order is placedcart, order, customer, currency, orderStatus
actionOrderStatusPostUpdateThe order status changednewOrderStatus, id_order
actionCustomerAccountAddA customer registerednewCustomer
actionCartSaveThe cart is savedcart
actionProductUpdateA product is updatedid_product, product
actionProductDeleteA product is deletedid_product, product

If the keys are unknown, find the hook call in the engine sources: a text search for 'actionProductDelete' in the classes folder shows the line with Hook::exec() and the array of parameters.

Several values from the event

// modules/acmehello/acmehello.php
public function hookActionValidateOrder(array $params): void
{
    $order = $params['order'];
    $customer = $params['customer'];
 
    PrestaShopLogger::addLog(
        sprintf('Order %s from %s, total %.2f', $order->reference, $customer->email, $order->total_paid),
        1,
    );
}

Now the log shows who placed the order and for what amount.

Object hooks

The engine itself calls hooks whenever any core object is saved. Their names follow a pattern.

Name patternWhen
actionObject<Class>AddAfterThe object is created
actionObject<Class>UpdateAfterThe object is changed
actionObject<Class>DeleteAfterThe object is deleted

For example, actionObjectCustomerUpdateAfter fires on any change of a customer. The object itself is in $params['object'].

Common mistake

An action hook handler calls die(), echo or performs a redirect. The hook fires in the middle of someone else's operation — the checkout, for example — and such a call cuts it short: the order is created, but the customer sees a blank page. A handler must quietly do its job and return control, and write errors to the log.

Thanks for reading!

Donate to the site — we work hard for you.