Component hooks

Run your code when a salla-* web component loads, block an add to cart, or change the items a product list shows, with Salla.hooks.

Read this when you need to change how a salla-* web component behaves, from your theme or an app script.

Component hooks let your code step into a web component at fixed points: when it loads, before it adds to the cart, before it shows its products. They are not slots: a slot adds content to a page, a component hook changes a component.

const unsubscribe = Salla.hooks.registerHook('salla-add-product-button', 'componentDidLoad', (button) => {
  console.log('Add button for product', button.productId);
});

// later, to stop:
unsubscribe();

Register a hook

Salla.hooks.registerHook(tag, hookName, callback, options?) returns a function that removes the hook.

OptionDoes
priorityA number. Higher runs first. 0 by default.
oncetrue to run once, then remove itself. Salla.hooks.registerHookOnce(...) does the same.

The callback may be async: the component waits for it. An error in your callback is caught and logged, so it never breaks the component.

Components and hooks

Only these tags and hooks can be registered. Anything else is ignored with a console warning.

TagHookCallback receivesYou can change
salla-add-product-buttoncomponentDidLoadthe componentThe element
salla-add-product-buttonvalidate{ isValid, component }isValid: set it to false to stop the add
salla-products-listcomponentDidLoadthe componentThe element
salla-products-listbeforeBuildListUrl{ component, url }url, to change the request
salla-products-listbeforeAppendItems{ component, items }items, to filter or replace the products shown
salla-products-slidercomponentDidLoad, beforeAppendItemsas aboveas above
salla-menuafterFetchMenuItemsthe componentcomponent.menus, the loaded menu
salla-product-options, salla-hook, salla-scopes, salla-user-menucomponentDidLoadthe componentThe element

On salla-add-product-button, the component has productId, productType, productStatus and quantity.

Salla.hooks.registerHook('salla-products-list', 'beforeAppendItems', (ctx: { items: Array<{ is_available: boolean }> }) => {
  ctx.items = ctx.items.filter((item) => item.is_available);
});

Salla.hooks.registerHook('salla-products-list', 'beforeBuildListUrl', (ctx: { url: string }) => {
  const url = new URL(ctx.url);
  url.searchParams.set('custom_filter', 'my_value');
  ctx.url = url.toString();
});

Late registration

You do not have to register before the component loads. A new componentDidLoad hook also runs at once for every component of that tag already on the page, then for each one that loads later. Each element runs it only once.

Remove hooks

CallRemoves
The function registerHook returnedThat one hook
Salla.hooks.removeHook(tag, hookName, callback)That one hook
Salla.hooks.removeHook(tag, hookName)Every hook of that name on the tag
Salla.hooks.clear(tag?)Every hook on the tag, or every hook on the page

In a React component, register in useEffect and return the remove function as the cleanup.

Gotchas

  • validate runs only when the button is not a form's submit button and the product's salla-product-options is on the page. The engine's product page submits a form, so there validate does not run. To check an add there, use your own submit handler.
  • Register after the SDK has started: inside Salla.onReady(() => { ... }), or in useEffect.
  • Objects are passed by reference. Change the fields of ctx; do not replace ctx itself.

See also

On this page