Product Lifecycle Hooks
The eight hooks over creating, updating, deleting and switching a product.
Overview
A product travels three steps: it is created, updated and deleted. Each has a gate you can stop at in front of it and an event that reports it behind.
Two status hooks sit beside them. The single one fires per product; the bulk one fires once when an action reaches many products from the list.
Reference
Stopping a product being created
Runs before a new product is created.
Hook::add('gate:product.create', 10, function ($input, $state) {
if (!Acme::namingOk($input['name'] ?? '')) return 'The product name breaks the naming rule.';
return null;
});Following a product being created
Runs after a product is created.
Hook::add('action:product.created', 10, function ($new_id, $input) {
// ZERO = no product was created.
if (!$new_id) return;
Acme::registerCatalogItem($new_id, $input['name'] ?? '');
});Stopping a product update
Runs before a product is updated. You hold both the new data and the old record, so you can compare what changed.
Hook::add('gate:product.update', 10, function ($id, $input, $detail) {
// You hold both records: measure the difference.
if (($detail['module'] ?? '') !== ($input['module'] ?? '') && Acme::hasLiveServices($id))
return 'The module cannot change while live services run on it.';
return null;
});Following a product update
Runs after the product is updated.
Hook::add('action:product.updated', 10, function ($id, $input, $detail) {
Acme::syncCatalog($id, $input, $detail);
});Stopping a product deletion
Runs before a product is deleted.
Hook::add('gate:product.delete', 10, function ($id, $detail) {
if (Acme::hasLiveServices($id)) return 'Live services still run on this product.';
return null;
});Following a product deletion
Runs after the product is deleted.
Hook::add('action:product.deleted', 10, function ($product_id, $detail) {
Acme::dropCatalogItem($product_id);
});Following a product status change
Runs when a product is opened for sale or closed.
active or inactive.Hook::add('action:product.status_changed', 10, function ($product_id, $status) {
Acme::setCatalogVisible($product_id, $status === 'active');
});Following a bulk action
Runs when an action is applied to several products at once from the list. Unlike the single status event, this is one call, not one per product.
active or inactive.Hook::add('action:product.bulk_action_applied', 10, function ($action, $ids) {
// One call, many products: the single status event does not fire here.
Acme::bulkVisibility($ids, $action === 'active');
});Pitfalls
The new product id can arrive as zero, which means no product was created. A listener that uses the id straight away then works on a record that is not there. Check it on the first line.
When an action reaches many products from the list, only the bulk event fires. Work hung on the single status hook never runs on that path; listen on both.
Related Articles
- Product Hooks
- Service Hooks
- How Hooks Work
Merci pour votre retour !
Notre équipe d'assistance est disponible 24h/24 pour tout ce que vous ne trouvez pas ci-dessus.