Vue Storefront is now Alokai! Learn More
Category normalizer

Category normalizer

The normalizeCategory function is used to map a Magento CategoryTree into the unified SfCategory data model.

Parameters

NameTypeDefault valueDescription
contextNormalizerContextcontext needed for the normalizer
categoryCategoryTreeWithParentUidMagento 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 }),
        ),
    ),
  };
});