Skip to main content
Skip to main content

TotalsService

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

Implements

Constructors

constructor

**new TotalsService**(«destructured»)

Parameters

__namedParametersTotalsServicePropsRequired

Properties

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

Accessors

activeManager_

Protected get**activeManager_**(): EntityManager

Returns

EntityManager

EntityManagerEntityManagerRequired

Methods

atomicPhase_

Protected **atomicPhase_**<TypeParameter TResult, TypeParameter TError>(work, isolationOrErrorHandler?, maybeErrorHandlerOrDontFail?): Promise&#60;TResult&#62;

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.

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

Promise<TResult>

PromisePromise<TResult>Required
the result of the transactional work

calculateDiscount_

**calculateDiscount_**(lineItem, variant, variantPrice, value, discountType): [LineDiscount](/references/services/types/LineDiscount)

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

LineDiscount

LineDiscountobjectRequired
Associates a line item and discount allocation.

Deprecated

  • in favour of DiscountService.calculateDiscountForLineItem

getAllocationItemDiscounts

**getAllocationItemDiscounts**(discount, cart): [LineDiscount](/references/services/types/LineDiscount)[]

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[]LineDiscount[]Required
array of triples of lineitem, variant and applied discount

getAllocationMap

**getAllocationMap**(orderOrCart, options?): Promise&#60;[LineAllocationsMap](/references/services/types/LineAllocationsMap)&#62;

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.claimsClaimOrder[]
orderOrCart.discountsDiscount[]
orderOrCart.itemsLineItem[]Required
orderOrCart.swapsSwap[]
optionsAllocationMapOptionsRequired
controls what should be included in allocation map

Default: {}

Returns

Promise<LineAllocationsMap>

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

getCalculationContext

**getCalculationContext**(calculationContextData, options?): Promise&#60;[TaxCalculationContext](/references/services/types/TaxCalculationContext)&#62;

Prepares the calculation context for a tax total calculation.

Parameters

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

Default: {}

Returns

Promise<TaxCalculationContext>

PromisePromise<TaxCalculationContext>Required
the tax calculation context

getDiscountTotal

**getDiscountTotal**(cartOrOrder): Promise&#60;number&#62;

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

Promise<number>

PromisePromise<number>Required
the total discounts amount

getGiftCardTotal

**getGiftCardTotal**(cartOrOrder, opts?): Promise&#60;&#123; tax_total: number ; total: number &#125;&#62;

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

Promise<{ tax_total: number ; total: number }>

PromisePromise<{ tax_total: number ; total: number }>Required
the gift card amount applied to the cart or order

getGiftCardableAmount

**getGiftCardableAmount**(cartOrOrder): Promise&#60;number&#62;

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

Promise<number>

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

getLineDiscounts

**getLineDiscounts**(cartOrOrder, discount?): [LineDiscountAmount](/references/services/types/LineDiscountAmount)[]

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.claimsClaimOrder[]
cartOrOrder.itemsLineItem[]Required
cartOrOrder.swapsSwap[]
discountDiscount
the discount to use as context for the calculation

Returns

LineDiscountAmount[]

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

getLineItemAdjustmentsTotal

**getLineItemAdjustmentsTotal**(cartOrOrder): number

Parameters

cartOrOrderOrder | CartRequired

Returns

number

numbernumber

getLineItemDiscountAdjustment

**getLineItemDiscountAdjustment**(lineItem, discount): number

Parameters

lineItemLineItemRequired
Line Items are created when a product is added to a Cart. When Line Items are purchased they will get copied to the resulting order, swap, or claim, and can eventually be referenced in Fulfillments and Returns. Line items may also be used for order edits.
discountDiscountRequired
A discount can be applied to a cart for promotional purposes.

Returns

number

numbernumber

getLineItemRefund

**getLineItemRefund**(order, lineItem): Promise&#60;number&#62;

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

Promise<number>

PromisePromise<number>Required
the line item refund amount.

getLineItemTotal

**getLineItemTotal**(lineItem, cartOrOrder, options?): Promise&#60;number&#62;

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
the options to use for the calculation

Default: {}

Returns

Promise<number>

PromisePromise<number>Required
the line item total

getLineItemTotals

**getLineItemTotals**(lineItem, cartOrOrder, options?): Promise&#60;[LineItemTotals](/references/services/types/LineItemTotals-1)&#62;

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

Promise<LineItemTotals>

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

getPaidTotal

**getPaidTotal**(order): number

Gets the total payments made on an order

Parameters

orderOrderRequired
the order to calculate paid amount for

Returns

number

numbernumber
the total paid amount

getRefundTotal

**getRefundTotal**(order, lineItems): Promise&#60;number&#62;

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

Promise<number>

PromisePromise<number>Required
the calculated subtotal

getRefundedTotal

**getRefundedTotal**(order): number

Gets the total refund amount for an order.

Parameters

orderOrderRequired
the order to get total refund amount for.

Returns

number

numbernumber
the total refunded amount for an order.

getShippingMethodTotals

**getShippingMethodTotals**(shippingMethod, cartOrOrder, opts?): Promise&#60;[ShippingMethodTotals](/references/services/types/ShippingMethodTotals-1)&#62;

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
options for what should be included

Default: {}

Returns

Promise<ShippingMethodTotals>

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

getShippingTotal

**getShippingTotal**(cartOrOrder): Promise&#60;number&#62;

Calculates shipping total

Parameters

cartOrOrderOrder | CartRequired
cart or order to calculate subtotal for

Returns

Promise<number>

PromisePromise<number>Required
shipping total

getSubtotal

**getSubtotal**(cartOrOrder, opts?): Promise&#60;number&#62;

Calculates subtotal of a given cart or order.

Parameters

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

Default: {}

Returns

Promise<number>

PromisePromise<number>Required
the calculated subtotal

getSwapTotal

**getSwapTotal**(order): number

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

Parameters

orderOrderRequired
the order to calculate swap total for

Returns

number

numbernumber
the swap total

getTaxTotal

**getTaxTotal**(cartOrOrder, forceTaxes?): Promise&#60;null \| number&#62;

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

Promise<null | number>

PromisePromise<null | number>Required
tax total

getTotal

**getTotal**(cartOrOrder, options?): Promise&#60;number&#62;

Calculates total of a given cart or order.

Parameters

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

Default: {}

Returns

Promise<number>

PromisePromise<number>Required
the calculated subtotal

rounded

**rounded**(value): number

Rounds a number using Math.round.

Parameters

valuenumberRequired
the value to round

Returns

number

numbernumber
the rounded value

shouldRetryTransaction_

Protected **shouldRetryTransaction_**(err): boolean

Parameters

errRecord<string, unknown> | { code: string }Required

Returns

boolean

booleanboolean

withTransaction

**withTransaction**(transactionManager?): [TotalsService](/references/services/classes/TotalsService)

Parameters

transactionManagerEntityManager

Returns

TotalsService

TotalsServiceTotalsServiceRequired
Was this section helpful?