Vue Storefront is now Alokai! Learn More
Facet normalizer

Facet normalizer

Concept of facets exists in both Unified Data Layer world and Magento. The normalizeFacet function maps Magento Aggregation options into Unified SfFacet.

Parameters

NameTypeDefault valueDescription
facetAggregationMagento Facet
ctxNormalizerContextContext which contains getFacetType

Extending

The SfFacet is returned from SearchProducts Method. If the SfFacet structure doesn't contain the information you need for your Storefront, you can extend its logic using the defineNormalizers function.

Source

facet.ts
import type { Aggregation } from "@vue-storefront/magento-types";
import { GetFacetTypeFn, SfFacetTypes, type SfFacetItem } from "@vue-storefront/unified-data-model";
import { defineNormalizer } from "../defineNormalizer";

const defaultGetFacetType: GetFacetTypeFn<Aggregation> = () => SfFacetTypes.MULTI_SELECT;

export const normalizeFacet = defineNormalizer.normalizeFacet((facet, ctx) => {
  const { getFacetType = defaultGetFacetType } = ctx;

  const sfFacetItems: SfFacetItem[] =
    facet.options?.filter(Boolean).map((option) => ({
      label: option.label || option.value,
      value: option.value,
      productCount: Number(option.count ?? 0),
    })) || [];

  if (sfFacetItems.length === 0) {
    return null;
  }

  return {
    label: facet.label || facet.attribute_code,
    name: facet.attribute_code,
    values: sfFacetItems,
    type: getFacetType(facet),
  };
});