> ## Documentation Index
> Fetch the complete documentation index at: https://dokumentation.websale.de/llms.txt
> Use this file to discover all available pages before exploring further.

# $wsInventory - Stock

> Reference for $wsInventory: load stock levels and product availability for traffic-light displays, product pages and basket reservation times.

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`

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $wsInventory | json }}
```

**JSON output**

```json theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{
  "load": "ƒ()",
  "loadReservation": "ƒ()"
}
```

Note: `"ƒ()"` denotes a function.

**Methods overview**

| **Method**          | **Return type** | **Description**                              |
| ------------------- | --------------- | -------------------------------------------- |
| `load()`            | map             | Loads the stock data of a product.           |
| `loadReservation()` | map             | Loads 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**

| **Name**    | **Type** | **Required** | **Description**   |
| ----------- | -------- | ------------ | ----------------- |
| `productId` | string   | yes          | ID of the product |

**Example** that loads and outputs stock data.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $myInventoryInfo = $wsInventory.load($product.id) }} 

{{ if $myInventoryInfo.active }} 
  {{= $myInventoryInfo.deliveryText }} 
  Available: {{= $myInventoryInfo.amount }} pieces
{{ /if }}
```

<Info>
  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**

  | **Variable**    | **Type** | **Description**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
  | --------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  | `active`        | bool     | `true` if stock management is active for this product.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
  | `amount`        | int      | Available quantity.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
  | `deliveryText`  | string   | Delivery status text (e.g. "Only a few items in stock").                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
  | `messageLimit`  | int      | Quantity from which the notice text (`deliveryText`) is displayed.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
  | `soldOut`       | bool     | `true` if the product is sold out.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
  | `splitDelivery` | bool     | `true` if the product can also be ordered with partial deliveries (e.g. when the quantity is not fully available).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
  | `state`         | string   | Traffic light status of availability: `green` (sufficient stock), `yellow` (few items), `red` (sold out or very scarce).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
  | `storage`       | string   | Optional parameter  <br />Used in connection with the [store finder](/en/frontend/referenz/module/wsstores) and/or Click & Collect to specifically include the stock of a specific store in the availability check.  <br />StorageID (stock ID) to consider store stocks in the store finder or for Click & Collect.  <br />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").  <br />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). |
</Info>

### \$wsInventory.loadReservation()

Loads the reservation data of a basket item.

**Signature**\
`$wsInventory.loadReservation(basketItemId)`

**Return value**\
`map` - Reservation data with the following attributes:

| **Attribute** | **Type** | **Description**                        |
| ------------- | -------- | -------------------------------------- |
| `duration`    | int      | Remaining reservation time in seconds. |

**Parameters**

| **Name**       | **Type** | **Required** | **Description**        |
| -------------- | -------- | ------------ | ---------------------- |
| `basketItemId` | string   | yes          | ID of the basket item. |

**Example** that loads reservation data.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ 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.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ 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.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
<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.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ 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.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ 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.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{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](/en/frontend/praxisbeispiele/produkte/lagerbestand)

***

## Related links

* [Storefront API stock](/en/schnittstellen/storefront-api/storefront-api-lagerbestand)
