Vue Storefront is now Alokai! Learn More
Money normalizer

Money normalizer

The normalizeMoney function maps Commercetools BaseMoney into Unified SfMoney.

Parameters

NameTypeDefault valueDescription
contextNormalizerContextcontext needed for the normalizer
moneyBaseMoneyCommercetools money

Extending

The SfMoney is returned as a part of multiple models, as for example SfProduct, SfProductCatalogItem, and SfCart. If you want to extend the SfMoney with custom fields, you should use the addCustomFields API.

export const unifiedApiExtension = createUnifiedExtension({
  normalizers: {
    addCustomFields: [
      {
        normalizeMoney: (context, money) => ({
          type: money.type,
        }),
      },
    ],
  },
  config: {
    ...
  },
});

Source

money.ts
import { defineNormalizer } from "../defineNormalizer";

export const normalizeMoney = defineNormalizer.normalizeMoney((_context, money) => {
  // Invalid types for BaseMoney - fractionDigits are optional
  const fractionDigits = money.fractionDigits ?? 2;
  const divider = Math.pow(10, fractionDigits);
  const amount = money.centAmount / divider;

  return {
    currency: money.currencyCode,
    amount,
    precisionAmount: amount.toFixed(fractionDigits),
  };
});