Skip to main content
With the $wsActions module you can trigger actions in the frontend, for example adding a product to the basket, submitting a form, or logging a customer in or out. Afterwards you evaluate the result (success or error). This page is about triggering and evaluating actions via the $wsActions module. Which actions exist and which parameters a single action (e.g. BasketItemAdd or Login) expects is described in the actions reference. There you will find the functional meaning of the individual action names.

Basic concept

An action always goes through the same steps: prepare the action → embed → trigger → read the result → react. You prepare the action in the template, either as a link with the url() function or as a form object with the create() function. The customer then triggers the action by clicking or submitting. The shop executes it and rebuilds the target page. On this new page you find the result under $wsActions.current and can react accordingly, for example with a success message or by displaying errors. Ways to trigger an action
For a simple click, for example on the “Add to basket” button, you create a link with the url() function. If the customer should enter something (e.g. a login with username and password), you create an action object with create() and embed it in a form. The difference therefore lies in whether the customer still enters data or not.
Execution time: The template code runs during page rendering. $wsActions.current therefore contains the result of the action that was executed in the request with which this page was generated. If no action was executed, current is null – always check for existence first before accessing results.

CSRF protection (applies to all actions)

Every action must send a valid CSRF protection token, otherwise the shop rejects the request. This protects against actions being triggered unintentionally from external pages. When you create actions via url() or create(), the token is embedded automatically. Only if you assemble a request by hand do you have to pass the token from $wsActions.csrfToken yourself as the wscsrf parameter.

Availability of current

$wsActions.current is only filled if an action was executed in the same request. On a normally called page, the value is null. That is why the result examples below start with a check: {{ if $wsActions.current }}. Without this check, you would be accessing null.

Module overview

Example / excerpt of $wsActions
JSON output
Note: "ƒ()" denotes a function (method). current is null if no action was executed. Variables and methods overview

Variables

$wsActions.csrfToken

Contains the CSRF protection token of the current session. You only need it if you assemble an action request manually. In that case it must be sent as the “wscsrf” parameter so that the shop accepts the request. With url() and create() this happens automatically.

$wsActions.current

Contains the result of the action executed in the current call, i.e. success or error together with messages. The value is null if no action was executed.

Properties of $wsActions.current

The tag (current.tag) is currently not evaluated. You can assign it via create() to distinguish between several similar actions on the same page.

Properties of an error (current.errors[])

Evaluate the name of the action Before processing the result, check which action was executed. That way you do not accidentally react to a different action.
Evaluate success
Display errors per field errorsByField assigns errors to an input field. Use this to output an error message directly next to the affected field instead of only a general message.
List all errors

Methods

$wsActions.create()

Creates an action object for use in forms. Use create() when the customer still enters data before triggering (e.g. login data). The object provides the values (among others id and csrf) that you can embed as hidden form fields. The object has the same properties as $wsActions.current. Signature
$wsActions.create(actionName, paramDefaults, target, tag)
Return value
map – the action object.
Parameters Example that creates a basket action:

$wsActions.url()

Creates a URL that executes a specific action. Use url() for a link where the customer does not enter any further data (e.g. “Add to basket” or “Log out”). Signature
$wsActions.url(action, target, params)
Return value
string – the URL that executes the action.
Parameters Example that creates a link for adding to the basket:
The function $wsViews.current.url() comes from the $wsViews module and returns the current page as the target. This keeps the customer on the same page after adding.

Examples

Add a product to the basket and evaluate the result

This example shows the complete flow: the link triggers the action, after the click the page is rebuilt, and current is evaluated.
productId is a placeholder and must be replaced with the ID of an existing product. A non-existent product ID does not result in an evaluable current.error; it may cause the request to fail on the server side.
Result
After the click the customer stays on the page and sees either a confirmation or the concrete error messages from the action.

Login form with field errors

Here the customer enters data, so the action is prepared with create() and embedded in a form. After submitting, errors are displayed directly at the affected field.
Result
If the data is incorrect, the error message appears directly below the affected field.

  • Actions (concept and reference) – what actions are, how they are secured, and which action names exist with which parameters. This page only shows access via $wsActions.
  • $wsViews – with current.url() and viewUrl() it provides the target pages that url() and create() expect as target.
With the $wsActions module you can trigger actions in the frontend, for example adding a product to the basket, submitting a form, or logging a customer in or out. Afterwards you evaluate the result (success or error). This page is about triggering and evaluating actions via the $wsActions module. Which actions exist and which parameters a single action (e.g. BasketItemAdd or Login) expects is described in the actions reference. There you will find the functional meaning of the individual action names.

Basic concept

An action always goes through the same steps: prepare the action → embed → trigger → read the result → react. You prepare the action in the template, either as a link with the url() function or as a form object with the create() function. The customer then triggers the action by clicking or submitting. The shop executes it and rebuilds the target page. On this new page you find the result under $wsActions.current and can react accordingly, for example with a success message or by displaying errors. Ways to trigger an action
For a simple click, for example on the “Add to basket” button, you create a link with the url() function. If the customer should enter something (e.g. a login with username and password), you create an action object with create() and embed it in a form. The difference therefore lies in whether the customer still enters data or not.
Execution time: The template code runs during page rendering. $wsActions.current therefore contains the result of the action that was executed in the request with which this page was generated. If no action was executed, current is null – always check for existence first before accessing results.

CSRF protection (applies to all actions)

Every action must send a valid CSRF protection token, otherwise the shop rejects the request. This protects against actions being triggered unintentionally from external pages. When you create actions via url() or create(), the token is embedded automatically. Only if you assemble a request by hand do you have to pass the token from $wsActions.csrfToken yourself as the wscsrf parameter.

Availability of current

$wsActions.current is only filled if an action was executed in the same request. On a normally called page, the value is null. That is why the result examples below start with a check: {{ if $wsActions.current }}. Without this check, you would be accessing null.
Note: "ƒ()" denotes a function (method). current is null if no action was executed. Contains the CSRF protection token of the current session. You only need it if you assemble an action request manually. In that case it must be sent as the “wscsrf” parameter so that the shop accepts the request. With url() and create() this happens automatically.
Contains the result of the action executed in the current call, i.e. success or error together with messages. The value is null if no action was executed.

Properties of $wsActions.current

The tag (current.tag) is currently not evaluated. You can assign it via create() to distinguish between several similar actions on the same page.

Properties of an error (current.errors[])

Evaluate the name of the action Before processing the result, check which action was executed. That way you do not accidentally react to a different action.
Evaluate success
Display errors per field errorsByField assigns errors to an input field. Use this to output an error message directly next to the affected field instead of only a general message.
List all errors

Methods

$wsActions.create()

Creates an action object for use in forms. Use create() when the customer still enters data before triggering (e.g. login data). The object provides the values (among others id and csrf) that you can embed as hidden form fields. The object has the same properties as $wsActions.current. Signature
$wsActions.create(actionName, paramDefaults, target, tag)
Return value
map – the action object.
Parameters Example that creates a basket action:

$wsActions.url()

Creates a URL that executes a specific action. Use url() for a link where the customer does not enter any further data (e.g. “Add to basket” or “Log out”). Signature
$wsActions.url(action, target, params)
Return value
string – the URL that executes the action.
Parameters Example that creates a link for adding to the basket:
The function $wsViews.current.url()comes from the $wsViews module and returns the current page as the target. This keeps the customer on the same page after adding.

Examples

Add a product to the basket and evaluate the result

This example shows the complete flow: the link triggers the action, after the click the page is rebuilt, and current is evaluated.
productId is a placeholder and must be replaced with the ID of an existing product. A non-existent product ID does not result in an evaluable current.error; it may cause the request to fail on the server side.
Result
After the click the customer stays on the page and sees either a confirmation or the concrete error messages from the action.

Login form with field errors

Here the customer enters data, so the action is prepared with create() and embedded in a form. After submitting, errors are displayed directly at the affected field.
Result
If the data is incorrect, the error message appears directly below the affected field.
  • Actions (concept and reference) – what actions are, how they are secured, and which action names exist with which parameters. This page only shows access via $wsActions.
  • $wsViews – with current.url() and viewUrl() it provides the target pages that url() and create() expect as target.