Skip to main content
Skip to main content

TotalsService

A service that calculates total and subtotals for orders, carts etc..

Implements

constructor

Parameters

__namedParametersTotalsServicePropsRequired

Properties

manager_EntityManagerRequired
transactionManager_undefined | EntityManagerRequired
__container__anyRequired
taxProviderService_TaxProviderServiceRequired
newTotalsService_NewTotalsServiceRequired
taxCalculationStrategy_ITaxCalculationStrategyRequired
featureFlagRouter_FlagRouterRequired
__configModule__Record<string, unknown>
__moduleDeclaration__Record<string, unknown>

Accessors

activeManager_

Returns

EntityManagerEntityManagerRequired

Methods

withTransaction

Parameters

transactionManagerEntityManager

Returns

thisthisRequired

shouldRetryTransaction_

Parameters

errRecord<string, unknown> | objectRequired

Returns

booleanbooleanRequired

atomicPhase_

Wraps some work within a transactional block. If the service already has a transaction manager attached this will be reused, otherwise a new transaction manager is created.

Type Parameters

TResultobjectRequired
TErrorobjectRequired

Parameters

work(transactionManager: EntityManager) => Promise<TResult>Required
the transactional work to be done
isolationOrErrorHandlerIsolationLevel | (error: TError) => Promise<void | TResult>
the isolation level to be used for the work.
maybeErrorHandlerOrDontFail(error: TError) => Promise<void | TResult>
Potential error handler

Returns

PromisePromise<TResult>Required
the result of the transactional work

getTotal

Calculates total of a given cart or order.

Parameters

cartOrOrderOrder | CartRequired
object to calculate total for
optionsGetTotalsOptionsRequired
options to calculate by

Default: {}

Returns

PromisePromise<number>Required
the calculated subtotal

getPaidTotal

Gets the total payments made on an order

Parameters

orderOrderRequired
the order to calculate paid amount for

Returns

numbernumberRequired
the total paid amount

getSwapTotal

The total paid for swaps. May be negative in case of negative swap difference.

Parameters

orderOrderRequired
the order to calculate swap total for

Returns

numbernumberRequired
the swap total

getShippingMethodTotals

Gets the totals breakdown for a shipping method. Fetches tax lines if not already provided.

Parameters

shippingMethodShippingMethodRequired
the shipping method to get totals breakdown for.
cartOrOrderOrder | CartRequired
the cart or order to use as context for the breakdown
optsGetShippingMethodTotalsOptionsRequired
options for what should be included

Default: {}

Returns

PromisePromise<ShippingMethodTotals>Required
An object that breaks down the totals for the shipping method

getSubtotal

Calculates subtotal of a given cart or order.

Parameters

cartOrOrderOrder | CartRequired
cart or order to calculate subtotal for
optsSubtotalOptionsRequired
options

Default: {}

Returns

PromisePromise<number>Required
the calculated subtotal

getShippingTotal

Calculates shipping total

Parameters

cartOrOrderOrder | CartRequired
cart or order to calculate subtotal for

Returns

PromisePromise<number>Required
shipping total

getTaxTotal

Calculates tax total Currently based on the Danish tax system

Parameters

cartOrOrderOrder | CartRequired
cart or order to calculate tax total for
forceTaxesbooleanRequired
whether taxes should be calculated regardless of region settings

Default: false

Returns

PromisePromise<null | number>Required
tax total

getAllocationMap

Gets a map of discounts and gift cards that apply to line items in an order. The function calculates the amount of a discount or gift card that applies to a specific line item.

Parameters

orderOrCartobjectRequired
the order or cart to get an allocation map for
orderOrCart.itemsLineItem[]Required
optionsAllocationMapOptionsRequired
controls what should be included in allocation map

Default: {}

orderOrCart.discountsDiscount[]
orderOrCart.swapsSwap[]
orderOrCart.claimsClaimOrder[]

Returns

PromisePromise<LineAllocationsMap>Required
the allocation map for the line items in the cart or order.

getRefundedTotal

Gets the total refund amount for an order.

Parameters

orderOrderRequired
the order to get total refund amount for.

Returns

numbernumberRequired
the total refunded amount for an order.

getLineItemRefund

The amount that can be refunded for a given line item.

Parameters

orderOrderRequired
order to use as context for the calculation
lineItemLineItemRequired
the line item to calculate the refund amount for.

Returns

PromisePromise<number>Required
the line item refund amount.

getRefundTotal

Calculates refund total of line items. If any of the items to return have been discounted, we need to apply the discount again before refunding them.

Parameters

orderOrderRequired
cart or order to calculate subtotal for
lineItemsLineItem[]Required
the line items to calculate refund total for

Returns

PromisePromise<number>Required
the calculated subtotal

calculateDiscount_

Calculates either fixed or percentage discount of a variant

Parameters

lineItemLineItemRequired
id of line item
variantstringRequired
id of variant in line item
variantPricenumberRequired
price of the variant based on region
valuenumberRequired
discount value
discountTypeDiscountRuleTypeRequired
the type of discount (fixed or percentage)

Returns

LineDiscountLineDiscountRequired
triples of lineitem, variant and applied discount

Deprecated

  • in favour of DiscountService.calculateDiscountForLineItem

getAllocationItemDiscounts

If the rule of a discount has allocation="item", then we need to calculate discount on each item in the cart. Furthermore, we need to make sure to only apply the discount on valid variants. And finally we return ether an array of percentages discounts or fixed discounts alongside the variant on which the discount was applied.

Parameters

discountDiscountRequired
the discount to which we do the calculation
cartOrder | CartRequired
the cart to calculate discounts for

Returns

LineDiscount[]LineDiscount[]Required
array of triples of lineitem, variant and applied discount

getLineItemDiscountAdjustment

Parameters

lineItemLineItemRequired
discountDiscountRequired

Returns

numbernumberRequired

getLineItemAdjustmentsTotal

Parameters

cartOrOrderOrder | CartRequired

Returns

numbernumberRequired

getLineDiscounts

Returns the discount amount allocated to the line items of an order.

Parameters

cartOrOrderobjectRequired
the cart or order to get line discount allocations for
cartOrOrder.itemsLineItem[]Required
cartOrOrder.swapsSwap[]
cartOrOrder.claimsClaimOrder[]
discountDiscount
the discount to use as context for the calculation

Returns

LineDiscountAmount[]LineDiscountAmount[]Required
the allocations that the discount has on the items in the cart or order

getLineItemTotals

Breaks down the totals related to a line item; these are the subtotal, the amount of discount applied to the line item, the amount of a gift card applied to a line item and the amount of tax applied to a line item.

Parameters

lineItemLineItemRequired
the line item to calculate totals for
cartOrOrderOrder | CartRequired
the cart or order to use as context for the calculation
optionsLineItemTotalsOptionsRequired
the options to evaluate the line item totals for

Default: {}

Returns

PromisePromise<LineItemTotals>Required
the breakdown of the line item totals

getLineItemTotal

Gets a total for a line item. The total can take gift cards, discounts and taxes into account. This can be controlled through the options.

Parameters

lineItemLineItemRequired
the line item to calculate a total for
cartOrOrderOrder | CartRequired
the cart or order to use as context for the calculation
optionsGetLineItemTotalOptionsRequired
the options to use for the calculation

Default: {}

Returns

PromisePromise<number>Required
the line item total

getGiftCardableAmount

Gets the amount that can be gift carded on a cart. In regions where gift cards are taxable this amount should exclude taxes.

Parameters

cartOrOrderOrder | CartRequired
the cart or order to get gift card amount for

Returns

PromisePromise<number>Required
the gift card amount applied to the cart or order

getGiftCardTotal

Gets the gift card amount on a cart or order.

Parameters

cartOrOrderOrder | CartRequired
the cart or order to get gift card amount for
optsobjectRequired

Default: {}

opts.gift_cardablenumber

Returns

PromisePromise<object>Required
the gift card amount applied to the cart or order

getDiscountTotal

Calculates the total discount amount for each of the different supported discount types. If discounts aren't present or invalid returns 0.

Parameters

cartOrOrderOrder | CartRequired
the cart or order to calculate discounts for

Returns

PromisePromise<number>Required
the total discounts amount

getCalculationContext

Prepares the calculation context for a tax total calculation.

Parameters

calculationContextDataCalculationContextDataRequired
the calculationContextData to get the calculation context for
optionsCalculationContextOptionsRequired
options to gather context by

Default: {}

Returns

PromisePromise<TaxCalculationContext>Required
the tax calculation context

rounded

Rounds a number using Math.round.

Parameters

valuenumberRequired
the value to round

Returns

numbernumberRequired
the rounded value
Was this section helpful?