> ## 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.

# $wsProductRating - Product ratings

> Reference for $wsProductRating: load, check and display product ratings in templates to strengthen purchasing decisions and customer trust.

With the `$wsProductRating` module, you can load, check, and display product ratings in the frontend.

Product ratings are an important element for purchasing decisions. They show customers the experiences of other buyers and increase trust in products.

***

## Module overview

**Example / excerpt of** `$wsProductRating`

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

**JSON output**

```json theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{
  "checkRatingExistence": "ƒ()",
  "loadAllProductRatings": "ƒ()",
  "loadLatestRatingForAccount": "ƒ()",
  "loadRatingByAccount": "ƒ()",
  "loadRatingStatistics": "ƒ()",
  "loadSingleRating": "ƒ()"
}
```

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

**Methods overview**

| **Method**                | **Return type** | **Description**                                                           |
| ------------------------- | --------------- | ------------------------------------------------------------------------- |
| `checkRatingExistence()`  | bool            | Checks whether a rating exists for a product in connection with an order. |
| `loadAllProductRatings()` | array           | Loads all ratings of a product.                                           |
| `loadRatingStatistics()`  | map             | Loads statistics for the ratings of a product.                            |
| `loadSingleRating()`      | map             | Loads a single rating based on product and order ID.                      |
| `loadLatestRating()`      | map             | Loads the latest rating of the currently logged-in customer.              |
| `loadRatingByAccount()`   | map             | Loads a product rating of the currently logged-in customer.               |

***

## Templates

Product ratings are typically displayed on the product detail page (product.htm). They can also be included on category pages or in the order history to prompt customers to leave a rating.

***

## Variables

No variables are available for `$wsProductRating`.

***

## Methods

### \$wsProductRating.checkRatingExistence()

Checks whether a rating already exists for a product in connection with an order.

**Signature**\
`$wsProductRating.checkRatingExistence(productId, orderId)`

**Return value**\
`bool` - `true` if a rating exists, otherwise `false`.

**Parameters**

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

**Example** that checks whether a rating exists.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ if $wsProductRating.checkRatingExistence(productId, orderId) }}
   // Rating exists
{{ /if }}
```

### \$wsProductRating.loadAllProductRatings()

Loads all ratings of a product.

**Signature**\
`$wsProductRating.loadAllProductRatings(productId)`

**Return value**\
`array` - List with all ratings of the product.

**Parameters**

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

**Example** that loads all ratings of a product.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $myProductRatings = $wsProductRating.loadAllProductRatings(productId) }}
```

### \$wsProductRating.loadRatingStatistics()

Loads statistics for the ratings of a product.

**Signature**\
`$wsProductRating.loadRatingStatistics(productId)`

**Return value**\
`map` - Map with rating statistics (e.g. average, count).

**Parameters**

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

**Example** that loads the statistics of a product.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $myRatingStatistics = $wsProductRating.loadRatingStatistics(productId) }}
```

### \$wsProductRating.loadSingleRating()

Loads a single rating based on product and order ID.

**Signature**\
`$wsProductRating.loadSingleRating(productId, orderId)`

**Return value**\
`map` - Map with the rating data.

**Parameters**

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

**Example that loads a single rating.**

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $myRating = $wsProductRating.loadSingleRating(productId, orderId) }}
```

### \$wsProductRating.loadLatestRatingForAccount()

Loads the latest rating of the currently logged-in customer.

**Signature**\
`$wsProductRating.loadLatestRatingForAccount()`

**Return value**\
`map` - Map with the rating data.

**Example** that loads the customer's latest rating.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $myLatestRating = $wsProductRating.loadLatestRatingForAccount() }}
```

### \$wsProductRating.loadRatingByAccount()

Loads a product rating of the currently logged-in customer.

**Signature**\
`$wsProductRating.loadRatingByAccount(productId)`

**Return value**\
`map` - Map with the customer's rating data.

**Parameters**

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

**Example** that loads the customer's rating for a product.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $myProductRating = $wsProductRating.loadRatingByAccount(productId) }}
```

***

## Actions

No actions are available for `$wsProductRating`.

***

## Examples

### Display average rating

This example displays the average rating and the number of ratings of a product.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $statistics = $wsProductRating.loadRatingStatistics($product.id) }}
{{ if $statistics.totalCount > 0 }}
  Rating: {{= $statistics.averageRating }} / 5 ({{= $statistics.totalCount }} ratings)
{{ /if }}
```

### List all ratings of a product

This example loads and displays all ratings of a product.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $ratings = $wsProductRating.loadAllProductRatings($product.id) }}
{{ if $ratings }}
  {{ foreach $rating in $ratings }}
    <p><strong>{{= $rating.author }}</strong>: {{= $rating.rating }} stars</p>
    <p>{{= $rating.text }}</p>
  {{ /foreach }}
{{ else }}
  <p>No ratings available yet.</p>
{{ /if }}
```

### Check whether the customer has already rated

This example checks whether the customer has already rated a product before the rating form is displayed.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ if !$wsProductRating.checkRatingExistence($product.id, $order.id) }}
  
{{ else }}
  <p>You have already rated this product.</p>
{{ /if }}
```

***

## Related links

* [Storefront API customer ratings](/en/schnittstellen/storefront-api/storefront-api-kundenbewertungen)
