Skip to main content
The $wsStore module is a key-value store for the frontend. Through it, arbitrary data can be stored under a freely chosen key, read out again later, or deleted in a targeted way. It is the right tool wherever you need information that the shop system does not provide by default.

Module overview

Example / excerpt of $wsStore
{{= $wsStore | json }}
JSON output
{
  "set": "ƒ()",
  "get": "ƒ()",
  "delete": "ƒ()",
  "increment": "ƒ()"
}
Note: "ƒ()" denotes a function. Methods overview
NameReturn typeDescription
set()-Stores a value under the specified key.
get()any / nullReturns the stored value for the specified
key.
delete()-Deletes the entry for the specified key.
increment()-Increments a counter value under the specified key.

Methods

$wsStore.set()

Stores an arbitrary value under a key. The value can later be retrieved via get() . Optionally, a validity duration (ttl) can be specified in seconds, after which the entry is automatically removed. Without ttl, a default value of 365 days applies. Signature
$wsStore.set(key, value, ttl)

Return value

Parameters
NameTypeRequiredDescription
keystringyesThe key under which the value is stored.
Freely chosen.
Recommendation:
use meaningful, unique names with a prefix
(e.g. 'productViews-' + $cProduct.id) to rule out collisions
with other modules.
valueanyyesThe value to be stored. (integer, string, boolean, object,
array)
ttlintnoValidity duration in seconds. After expiration, the entry is
automatically removed. Without specification, a default value
of 365 days applies.
Example - Set a session marker so that a product view is counted only once per visitor To implement the display “X times viewed in the last 24 hours” on the product page, you must prevent the same visitor from falsifying the counter by repeatedly reloading the page. To do this, a marker is set per visitor – as long as this marker exists, the product view is not counted again for this visitor. $wsSession.id is the unique session ID of the current visitor. It is automatically provided by the shop system and does not have to be assigned manually. 14400 seconds equal 4 hours – the marker is valid for this long.
{{ $wsStore.set('productViewSession-' + $cProduct.id + $wsSession.id, true, 14400) }}
Result
From the time of setting, $wsStore.get('productViewSession-<ProductID><SessionID>') returns the value true for 4 hours. The actual counter that outputs the display “X times viewed in the last 24 hours” to the visitor is maintained via increment(). Only after the 4 hours have elapsed may the same visitor’s product view flow into the counter again.

$wsStore.get()

Returns the value that was previously stored with set() under the specified key. If no entry exists or the entry has expired, null is returned. Signature
$wsStore.get(key)
Return value
any - The stored value, or null, if no entry exists or the entry has expired.
Parameters
NameTypeRequiredDescription
keystringyesThe key whose value is to be retrieved.
Example - Display “X times viewed in the last 24 hours” on the product page To output to the visitor on a product detail page a display like “X times viewed in the last 24 hours”, the value previously incremented with increment() is read out. If no entry exists yet (because the product has not yet been viewed in the last 24 hours), get() returns null and the display is skipped.
{{ var $productViews = $wsStore.get('productViews-' + $cProduct.id) }}
{{ if $productViews }}
    {{= $productViews }} times viewed in the last 24 hours
{{ /if }}
Result, if $productViews contains the value 10
10 times viewed in the last 24 hours

$wsStore.delete()

Deletes the entry for the specified key from the store. After deletion, get() returns null for this key. Signature
$wsStore.delete(key)

Return value\

Parameters
NameTypeRequiredDescription
keystringyesThe key whose entry is to be deleted.

$wsStore.increment()

Increments a counter value under the specified key by the specified amount. If the key does not yet exist, it is created with the value of amount. This method should be preferred over a combination of get() and set() when a counter is to be incremented.

This is because get() and set() perform the increment in two steps - first read the current value, then store the incremented value. If two visitors open the page at the same time, both read the same value in step 1, for example 5. Both then store 6. The counter ends up at 6 even though it should be at 7.
With increment(), reading and incrementing happen in a single step, so both calls are counted correctly. Signature
$wsStore.increment(key, amount, ttl)
Parameters
NameTypeRequiredDescription
keystringyesThe key whose counter value is to be incremented.
amountintnoThe value by which the counter is incremented.
Default is 1.
ttlintnoValidity duration in seconds. After expiration,
the entry is automatically removed.
If ttl is omitted, a default value
of 365 days applies.
Example - Count product views, once per session The goal is to display “X times viewed in the last 24 hours” on the product page. So that the same visitor does not falsify the counter by repeatedly reloading, a marker is additionally set per session that prevents the same visitor from counting the product multiple times. $wsSession.id is the unique session ID of the current visitor. It is automatically provided by the shop system and does not have to be assigned manually.
{{ var $myViewKey = 'productViews-' + $cProduct.id }}
{{ var $mySessionKey = 'productViewSession-' + $cProduct.id + $wsSession.id }}

{{ if not $wsStore.get($mySessionKey) }}
    {{ $wsStore.increment($myViewKey, 1, 86400) }}
    {{ $wsStore.set($mySessionKey, true, 14400) }}
{{ /if }}
Result
The counter productViews-<ProductID> is incremented at most once every 4 hours per visitor. The subsequent display with get() shows the visitor for example: “7 times viewed in the last 24 hours”. After 24 hours, the counter automatically restarts at zero.