ProductRating
Use ProductRating to show a product’s average star rating and approved review count on the product details page. For example, place it below the product name. The container renders the summary with the SDK StarRating component.
The summary comes from two Catalog Service product attributes:
ac_rating_summary: The average rating on a scale from0to100. The container divides this value by 20 to show a rating from0to5stars.ac_review_count: The number of approved reviews.
For how these attributes reach Catalog Service, see the Ratings and Reviews overview.
ProductRating receives initial product data during initialization. It then updates when it receives a pdp/data event in the same scope.
When the rating appears
Section titled “When the rating appears”The container stays hidden unless all of these conditions are met:
- Both
ac_rating_summaryandac_review_countare present in the product data with valid numeric values. The average rating must be between0and100. - Both attributes have the
visible_in_pdpstorefront role. The Product Details drop-in keeps only product attributes with this role. - The review count is a whole number greater than
0, so the product has at least one approved review.
ProductRating configurations
Section titled “ProductRating configurations”Pass these options when you render ProductRating:
The container also accepts standard div attributes, such as className.
Example
Section titled “Example”This example renders the product header and rating in your existing product details block. It runs inside the block’s decorate function, after Product Details initialization.
The example assumes that your block layout already has two elements: $header for the product header and $rating for the rating. Place $rating below $header in the layout. Use your block’s element variables in place of these names.
import { render as pdpRendered } from '@dropins/storefront-pdp/render.js';import ProductHeader from '@dropins/storefront-pdp/containers/ProductHeader.js';import ProductRating from '@dropins/storefront-pdp/containers/ProductRating.js';
// Inside decorate(): $header and $rating are elements in the block's layout.await Promise.all([ pdpRendered.render(ProductHeader, {})($header), // Shows the stars and approved review count, or nothing for unrated products. pdpRendered.render(ProductRating, {})($rating),]);The two render() calls use the same product data. ProductHeader shows the product name and SKU. ProductRating shows the stars and review count only when the display conditions are met.
- Numeric rating: To show text such as 4.5 (12), pass
valueDisplay: 'value'andcountDisplay: 'parentheses'toProductRating. - Scoped product data: If your product data uses a scope, pass that same
scopeto both containers.