Product review normalizer
The normalizeProductReview function maps Magento ProductReview into Unified SfProductReview.
Parameters
| Name | Type | Default value | Description |
|---|---|---|---|
context | NormalizerContext | context needed for the normalizer | |
review | ProductReview | Magento ProductReview |
Extending
The SfProductReview is returned from GetProductReviews Method. If the SfProductReview structure doesn't contain the information you need for your Storefront, you can extend its logic using the addCustomFields API. The following example demonstrates how to extend SfProductReview with a ratingsBreakdown field.
export const unifiedApiExtension = createUnifiedExtension({
normalizers: {
addCustomFields: [
{
normalizeProductReview: (context, review) => ({
ratingsBreakdown: review.ratings_breakdown
}),
},
],
},
config: {
...
},
});
Source
productReview.ts
import { maybe } from "@/helpers";
import { randomUUID } from "node:crypto";
import { defineNormalizer } from "../defineNormalizer";
export const normalizeProductReview = defineNormalizer.normalizeProductReview((_context, review) => {
return {
id: randomUUID(),
title: maybe(review.summary),
text: maybe(review.text),
rating:
review.ratings_breakdown?.[0]?.value == null
? null
: Number.parseInt(review.ratings_breakdown?.[0]?.value),
reviewer: maybe(review.nickname),
createdAt: new Date(review.created_at).toISOString(),
};
});