diff --git a/plugin/src/utils/get-typed-money-as-number.ts b/plugin/src/utils/get-typed-money-as-number.ts index 21e8f56..7e4f707 100644 --- a/plugin/src/utils/get-typed-money-as-number.ts +++ b/plugin/src/utils/get-typed-money-as-number.ts @@ -1,5 +1,11 @@ import { TypedMoney } from '@commercetools/platform-sdk'; +import logger from './log'; + +export const getTypedMoneyAsNumber = (money?: TypedMoney): number => { + if (!money) { + logger.info('No price found for product.'); + return 0; + } -export const getTypedMoneyAsNumber = (money: TypedMoney): number => { return money.centAmount / Math.pow(10, money.fractionDigits); }; diff --git a/plugin/src/utils/locale-currency-utils.spec.ts b/plugin/src/utils/locale-currency-utils.spec.ts index 4818c0f..42866d9 100644 --- a/plugin/src/utils/locale-currency-utils.spec.ts +++ b/plugin/src/utils/locale-currency-utils.spec.ts @@ -315,4 +315,14 @@ describe('getProductPriceByPriority', () => { currency: 'EUR', }); }); + + it('should return a single price with an amount of 0 (zero) when prices are not defined', async () => { + const price = getProductPriceByPriority([]); + + expect(price).toEqual({ + amount: 0, + country: undefined, + currency: undefined, + }); + }); });