Skip to main content
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
{{= $wsProductRating | json }}
JSON output
{
  "checkRatingExistence": "ƒ()",
  "loadAllProductRatings": "ƒ()",
  "loadLatestRatingForAccount": "ƒ()",
  "loadRatingByAccount": "ƒ()",
  "loadRatingStatistics": "ƒ()",
  "loadSingleRating": "ƒ()"
}
Note: "ƒ()" denotes a function. Methods overview
MethodReturn typeDescription
checkRatingExistence()boolChecks whether a rating exists for a product in connection with an order.
loadAllProductRatings()arrayLoads all ratings of a product.
loadRatingStatistics()mapLoads statistics for the ratings of a product.
loadSingleRating()mapLoads a single rating based on product and order ID.
loadLatestRating()mapLoads the latest rating of the currently logged-in customer.
loadRatingByAccount()mapLoads 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
NameTypeRequiredDescription
productIdstringyesID of the product.
orderIdstringyesID of the order.
Example that checks whether a rating exists.
{{ 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
NameTypeRequiredDescription
productIdstringyesID of the product.
Example that loads all ratings of a product.
{{ 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
NameTypeRequiredDescription
productIdstringyesID of the product.
Example that loads the statistics of a product.
{{ 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
NameTypeRequiredDescription
productIdstringyesID of the product.
orderIdstringyesID of the order.
Example that loads a single rating.
{{ 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.
{{ 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
NameTypeRequiredDescription
productIdstringyesID of the product.
Example that loads the customer’s rating for a product.
{{ 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.
{{ 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.
{{ 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.
{{ if !$wsProductRating.checkRatingExistence($product.id, $order.id) }}
  
{{ else }}
  <p>You have already rated this product.</p>
{{ /if }}