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
contextNormalizerContextContext which contains getFacetType
facetAggregationMagento Facet

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 addCustomFields API.

Source

facet.ts
import type { Aggregation } from "@vue-storefront/magento-types";
import type { GetFacetTypeFn } from "@vue-storefront/unified-data-model";
import { 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((context, facet) => {
  const { getFacetType = defaultGetFacetType } = context;

  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),
  };
});