Skip to main content
With the $wsInventory module, you can access stock levels and product availability. Typical use cases are traffic-light displays (green/yellow/red), availability checks on product pages, or reservation times in the basket. In this section, you will learn how to load and display stock data.

Module overview

Example / excerpt of $wsInventory
{{= $wsInventory | json }}
JSON output
{
  "load": "ƒ()",
  "loadReservation": "ƒ()"
}
Note: "ƒ()" denotes a function. Methods overview
MethodReturn typeDescription
load()mapLoads the stock data of a product.
loadReservation()mapLoads the reservation data of a basket item.

Templates

Typically, information on stock levels and availability is loaded together with the products – for example on the home page, the category list, the search results, or in the basket.

Variables

No direct variables are available for $wsInventory. The data is loaded via methods.

Methods

$wsInventory.load()

Loads the stock data of a product. Signature
$wsInventory.load(productId)
Return value
map - Map with stock data.
Parameters
NameTypeRequiredDescription
productIdstringyesID of the product
Example that loads and outputs stock data.
{{ var $myInventoryInfo = $wsInventory.load($product.id) }} 

{{ if $myInventoryInfo.active }} 
  {{= $myInventoryInfo.deliveryText }} 
  Available: {{= $myInventoryInfo.amount }} pieces
{{ /if }}
By using the $wsInventory.load() function, various variables are available to retrieve and output stock data. Below is an overview of which variables are available.Stock data (return value of $wsInventory.load() )First, it is necessary to assign the map with the stock data, as shown in the example above, to a local variable. This can then be used at various places in the template.Stock data overview
VariableTypeDescription
activebooltrue if stock management is active for this product.
amountintAvailable quantity.
deliveryTextstringDelivery status text (e.g. “Only a few items in stock”).
messageLimitintQuantity from which the notice text (deliveryText) is displayed.
soldOutbooltrue if the product is sold out.
splitDeliverybooltrue if the product can also be ordered with partial deliveries (e.g. when the quantity is not fully available).
statestringTraffic light status of availability: green (sufficient stock), yellow (few items), red (sold out or very scarce).
storagestringOptional parameter
Used in connection with the store finder and/or Click & Collect to specifically include the stock of a specific store in the availability check.
StorageID (stock ID) to consider store stocks in the store finder or for Click & Collect.
If the parameter is specified, the quantities of the specified store are taken into account in addition to the regular stock in the availability/stock check (e.g. “available in store”).
If the parameter is not specified, only the stock of the current subshop is loaded – as before (same behavior as before, no template adjustments required).

$wsInventory.loadReservation()

Loads the reservation data of a basket item. Signature
$wsInventory.loadReservation(basketItemId)
Return value
map - Reservation data with the following attributes:
AttributeTypeDescription
durationintRemaining reservation time in seconds.
Parameters
NameTypeRequiredDescription
basketItemIdstringyesID of the basket item.
Example that loads reservation data.
{{ var $reservation = $wsInventory.loadReservation($basketItem.id) }}

{{ if $reservation }}
  {{ if $reservation.duration > 0 }}
    <p>Reserved for {{= $reservation.duration }} seconds</p>
  {{ else }}
    <p>Reservation expired</p>
  {{ /if }}
{{ /if }}

Actions

No actions are available for $wsInventory.

Examples for displaying stock

Check for stock management

This example checks whether stock management is active for this product.
{{ var $inventoryInfo = $wsInventory.load($product.id) }}
{{ if $inventoryInfo.active }}
  <p>Stock management is active for this product</p>
{{ /if }}

Stock data

This example displays the product-specific data.
<p>Delivery status text: {{= $inventoryInfo.deliveryText }}</p>
<p>Quantity: {{= $inventoryInfo.amount }}</p>
<p>Orderable: {{ if $inventoryInfo.soldOut }} No {{ else }} Yes {{ /if }}</p>

Display traffic light status

This example checks the product’s availability so that the customer can immediately see whether it is available, almost sold out, or not deliverable.
{{ var $inventoryInfo = $wsInventory.load($product.id) }}
{{ if $inventoryInfo.active }}
   Traffic light: {{= $inventoryInfo.state }}
   {{ switch $inventoryInfo.state }}
      {{ case "red"}}
         <span style="background-color: red;">RED</span>
      {{ case "yellow"}}
         <span style="background-color: yellow;">YELLOW</span>
      {{ case "green"}}
         <span style="background-color: green;">GREEN</span>
   {{ /switch }}
{{ /if }}

Display for reservation time

After a product has been added to the basket, the product can be reserved for a certain time. In this example, the reservation time is displayed in the basket.
{{ if $reservation }}
   {{ if $reservation.duration > 0 }}
      {{ var $sec = $reservation.duration % 60 }}
      {{ var $min= ( $reservation.duration - $sec ) / 60 }}
      The product is still reserved for you for
      {{ if $min > 0 }}
         <strong>{{= $min }} minutes</strong> and
      {{ /if }}
      <strong>{{= $sec }} seconds</strong>
   {{ else }}
      Unfortunately, your reservation has expired.
   {{ /if}}
{{ /if }}

Extend reservation time

To give the customer a bit more time to decide, the reservation time can be extended in the basket.
{{var $reservation = $wsInventory.loadReservation($basketItem.id)}}
{{ if $reservation }}
   {{ var $inventoryReserveAction = $wsActions.create("InventoryReserve", tag=$basketItem.id) }}
   {{ if $reservation.duration > 0 }}
      The product is reserved for you
   {{ else }}
      Unfortunately, your reservation has expired.
      <form method="post" action="{{ $wsViews.viewUrl('basket.htm') }}">
         <input type="hidden" name="wscsrf" value="{{= $inventoryReserveAction.csrf }}">
         <input type="hidden" name="wsact" value="{{= $inventoryReserveAction.id }}">
         <input type="hidden" name="wstarget" value="{{= $wsViews.viewUrl('basket.htm') }}">
         <input type="hidden" name="basketItemId" value="{{= $basketItem.id }}">
         <button>Reserve again</button>
      </form>
   {{ /if}}
   {{ if $inventoryReserveAction.success }}
      <p>Quantity was successfully re-reserved.</p>
   {{ /if }}
   {{ if $inventoryReserveAction.error }}
      <p>The reservation could not be extended</p>
   {{ /if }}
{{ /if }}
You can find more practical examples here: Stock