📊 Aggregate Review Counts
This guide explains how to retrieve aggregate review counts and average ratings for products using the Alphabite Reviews Plugin. This is useful for displaying summary information like star ratings on product listings or detail pages.
✅ Prerequisites
- A Medusa backend with the Reviews plugin installed and configured.
- Existing reviews associated with products.
🔨 Step-by-Step Implementation
The aggregateCounts
endpoint provides the average rating, total review count, and the distribution of ratings (e.g., how many 1-star, 2-star reviews, etc.) for a given product.
1. Retrieve Aggregate Counts for a Product
To get the aggregate review data for a specific product:
- Using SDK
- Using REST (Medusa SDK or fetch)
const { average, counts, product_id, total_count } = await sdk.alphabite.reviews.aggregateCounts({
product_id: "prod_...",
// Optional filter:
// verified_purchase_only: true, // Only include reviews from verified purchases
});
console.log(`Product ID: ${product_id}`);
console.log(`Average Rating: ${average}`);
console.log(`Total Reviews: ${total_count}`);
console.log("Rating Distribution:");
counts.forEach((c) => {
console.log(` ${c.rating} stars: ${c.count} reviews`);
});
import { AggregateCountsInput, AggregateCountsOutput } from "@alphabite/medusa-sdk";
const { average, counts, product_id, total_count } = await sdk.client.fetch<AggregateCountsOutput>(
`/store/reviews/product/prod_.../aggregate-counts`,
{
method: "GET",
query: {
// Optional filter:
// verified_purchase_only: true,
} as AggregateCountsInput,
}
);
console.log(`Product ID: ${product_id}`);
console.log(`Average Rating: ${average}`);
console.log(`Total Reviews: ${total_count}`);
console.log("Rating Distribution:");
counts.forEach((c) => {
console.log(` ${c.rating} stars: ${c.count} reviews`);
});
API Reference
Learn more about our Medusa SDK Wrapper Alphabite Medusa SDK Wrapper 🔗
You can view the full endpoint documentation for Aggregate Counts 🔗
📝 Notes
- The
verified_purchase_only
filter can be applied toaggregateCounts
to get statistics only from reviews associated with verified purchases. - This endpoint is highly optimized for quick retrieval of summary review data.
🔗 Related
- Docs: Alphabite Medusa SDK Wrapper
- Docs: Aggregate Counts - GET /store/reviews/product/:product_id/aggregate-counts