Category normalizer
The normalizeCategory function is used to map a Magento CategoryTree into the unified SfCategory data model.
Parameters
| Name | Type | Default value | Description |
|---|---|---|---|
context | NormalizerContext | context needed for the normalizer | |
category | CategoryTreeWithParentUid | Magento CategoryTree with information about categories' parent uid |
Extending
The SfCategory model is returned from the Unified Methods such as SearchProducts, and GetCategories. If the SfCategory 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 SfCategory with a description field.
export const unifiedApiExtension = createUnifiedExtension({
normalizers: {
addCustomFields: [
{
normalizeCategory: (context, category) => ({
description: category.description,
}),
},
],
},
config: {
...
},
});
Source
category.ts
import { maybe, slugify } from "@/helpers";
import { defineNormalizer } from "../defineNormalizer";
export const normalizeCategory = defineNormalizer.normalizeCategory((context, category) => {
return {
id: category.uid as string,
name: category.name ?? category.uid!,
parentCategoryId: maybe(category.parentCategoryId),
slug: category.url_key ?? slugify(category.name ?? ""),
subcategories: maybe(
category.children
?.filter(Boolean)
.map((element) =>
context.normalizers.normalizeCategory({ ...element, parentCategoryId: category.uid }),
),
),
};
});