Skip to main content

Class: TotalsService

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

ImplementsCopy to Clipboard

Hierarchy

  • TransactionBaseServiceCopy to Clipboard

    TotalsServiceCopy to Clipboard

Constructors

constructor

new TotalsService(__namedParametersCopy to Clipboard)

Parameters

NameType
__namedParametersCopy to ClipboardTotalsServicePropsCopy to Clipboard

Overrides

TransactionBaseService.constructor

Defined in

packages/medusa/src/services/totals.ts:113

Properties

__configModule__

ProtectedCopy to Clipboard OptionalCopy to Clipboard ReadonlyCopy to Clipboard __configModule__: RecordCopy to Clipboard<stringCopy to Clipboard, unknownCopy to Clipboard>

Inherited from

TransactionBaseService.__configModule__

Defined in

packages/medusa/src/interfaces/transaction-base-service.ts:10


__container__

ProtectedCopy to Clipboard ReadonlyCopy to Clipboard __container__: anyCopy to Clipboard

Inherited from

TransactionBaseService.__container__

Defined in

packages/medusa/src/interfaces/transaction-base-service.ts:9


__moduleDeclaration__

ProtectedCopy to Clipboard OptionalCopy to Clipboard ReadonlyCopy to Clipboard __moduleDeclaration__: RecordCopy to Clipboard<stringCopy to Clipboard, unknownCopy to Clipboard>

Inherited from

TransactionBaseService.__moduleDeclaration__

Defined in

packages/medusa/src/interfaces/transaction-base-service.ts:11


featureFlagRouter_

ProtectedCopy to Clipboard ReadonlyCopy to Clipboard featureFlagRouter_: FlagRouterCopy to Clipboard

Defined in

packages/medusa/src/services/totals.ts:111


manager_

ProtectedCopy to Clipboard manager_: EntityManagerCopy to Clipboard

Overrides

TransactionBaseService.manager_

Defined in

packages/medusa/src/services/totals.ts:105


newTotalsService_

ProtectedCopy to Clipboard ReadonlyCopy to Clipboard newTotalsService_: NewTotalsServiceCopy to Clipboard

Defined in

packages/medusa/src/services/totals.ts:109


taxCalculationStrategy_

ProtectedCopy to Clipboard ReadonlyCopy to Clipboard taxCalculationStrategy_: ITaxCalculationStrategyCopy to Clipboard

Defined in

packages/medusa/src/services/totals.ts:110


taxProviderService_

ProtectedCopy to Clipboard ReadonlyCopy to Clipboard taxProviderService_: TaxProviderServiceCopy to Clipboard

Defined in

packages/medusa/src/services/totals.ts:108


transactionManager_

ProtectedCopy to Clipboard transactionManager_: EntityManagerCopy to Clipboard

Overrides

TransactionBaseService.transactionManager_

Defined in

packages/medusa/src/services/totals.ts:106

Methods

atomicPhase_

ProtectedCopy to Clipboard atomicPhase_<TResultCopy to Clipboard, TErrorCopy to Clipboard>(workCopy to Clipboard, isolationOrErrorHandler?Copy to Clipboard, maybeErrorHandlerOrDontFail?Copy to Clipboard): PromiseCopy to Clipboard<TResultCopy to Clipboard>

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

Name
TResultCopy to Clipboard
TErrorCopy to Clipboard

Parameters

NameTypeDescription
workCopy to Clipboard(transactionManagerCopy to Clipboard: EntityManagerCopy to Clipboard) => PromiseCopy to Clipboard<TResultCopy to Clipboard>the transactional work to be done
isolationOrErrorHandler?Copy to ClipboardIsolationLevelCopy to Clipboard | (errorCopy to Clipboard: TErrorCopy to Clipboard) => PromiseCopy to Clipboard<voidCopy to Clipboard | TResultCopy to Clipboard>the isolation level to be used for the work.
maybeErrorHandlerOrDontFail?Copy to Clipboard(errorCopy to Clipboard: TErrorCopy to Clipboard) => PromiseCopy to Clipboard<voidCopy to Clipboard | TResultCopy to Clipboard>Potential error handler

Returns

PromiseCopy to Clipboard<TResultCopy to Clipboard>

the result of the transactional work

Inherited from

TransactionBaseService.atomicPhase_

Defined in

packages/medusa/src/interfaces/transaction-base-service.ts:50


calculateDiscount_

calculateDiscount_(lineItemCopy to Clipboard, variantCopy to Clipboard, variantPriceCopy to Clipboard, valueCopy to Clipboard, discountTypeCopy to Clipboard): LineDiscountCopy to Clipboard

Calculates either fixed or percentage discount of a variant

Parameters

NameTypeDescription
lineItemCopy to ClipboardLineItemCopy to Clipboardid of line item
variantCopy to ClipboardstringCopy to Clipboardid of variant in line item
variantPriceCopy to ClipboardnumberCopy to Clipboardprice of the variant based on region
valueCopy to ClipboardnumberCopy to Clipboarddiscount value
discountTypeCopy to ClipboardDiscountRuleTypeCopy to Clipboardthe type of discount (fixed or percentage)

Returns

LineDiscountCopy to Clipboard

triples of lineitem, variant and applied discount

Defined in

packages/medusa/src/services/totals.ts:629


getAllocationItemDiscounts

getAllocationItemDiscounts(discountCopy to Clipboard, cartCopy to Clipboard): LineDiscountCopy to Clipboard[]

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

NameTypeDescription
discountCopy to ClipboardDiscountCopy to Clipboardthe discount to which we do the calculation
cartCopy to ClipboardCartCopy to Clipboard | OrderCopy to Clipboardthe cart to calculate discounts for

Returns

LineDiscountCopy to Clipboard[]

array of triples of lineitem, variant and applied discount

Defined in

packages/medusa/src/services/totals.ts:671


getAllocationMap

getAllocationMap(orderOrCartCopy to Clipboard, options?Copy to Clipboard): PromiseCopy to Clipboard<LineAllocationsMapCopy to Clipboard>

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

NameTypeDescription
orderOrCartCopy to ClipboardObjectCopy to Clipboardthe order or cart to get an allocation map for
orderOrCart.claims?Copy to ClipboardClaimOrderCopy to Clipboard[]-
orderOrCart.discounts?Copy to ClipboardDiscountCopy to Clipboard[]-
orderOrCart.itemsCopy to ClipboardLineItemCopy to Clipboard[]-
orderOrCart.swaps?Copy to ClipboardSwapCopy to Clipboard[]-
optionsCopy to ClipboardAllocationMapOptionsCopy to Clipboardcontrols what should be included in allocation map

Returns

PromiseCopy to Clipboard<LineAllocationsMapCopy to Clipboard>

the allocation map for the line items in the cart or order.

Defined in

packages/medusa/src/services/totals.ts:438


getCalculationContext

getCalculationContext(calculationContextDataCopy to Clipboard, options?Copy to Clipboard): PromiseCopy to Clipboard<TaxCalculationContextCopy to Clipboard>

Prepares the calculation context for a tax total calculation.

Parameters

NameTypeDescription
calculationContextDataCopy to ClipboardCalculationContextDataCopy to Clipboardthe calculationContextData to get the calculation context for
optionsCopy to ClipboardCalculationContextOptionsCopy to Clipboardoptions to gather context by

Returns

PromiseCopy to Clipboard<TaxCalculationContextCopy to Clipboard>

the tax calculation context

Defined in

packages/medusa/src/services/totals.ts:1025


getDiscountTotal

getDiscountTotal(cartOrOrderCopy to Clipboard): PromiseCopy to Clipboard<numberCopy to Clipboard>

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

Parameters

NameTypeDescription
cartOrOrderCopy to ClipboardCartCopy to Clipboard | OrderCopy to Clipboardthe cart or order to calculate discounts for

Returns

PromiseCopy to Clipboard<numberCopy to Clipboard>

the total discounts amount

Defined in

packages/medusa/src/services/totals.ts:1005


getGiftCardTotal

getGiftCardTotal(cartOrOrderCopy to Clipboard, opts?Copy to Clipboard): PromiseCopy to Clipboard<{ tax_totalCopy to Clipboard: numberCopy to Clipboard ; totalCopy to Clipboard: numberCopy to Clipboard }>

Gets the gift card amount on a cart or order.

Parameters

NameTypeDescription
cartOrOrderCopy to ClipboardCartCopy to Clipboard | OrderCopy to Clipboardthe cart or order to get gift card amount for
optsCopy to ClipboardObjectCopy to Clipboard-
opts.gift_cardable?Copy to ClipboardnumberCopy to Clipboard-

Returns

PromiseCopy to Clipboard<{ tax_totalCopy to Clipboard: numberCopy to Clipboard ; totalCopy to Clipboard: numberCopy to Clipboard }>

the gift card amount applied to the cart or order

Defined in

packages/medusa/src/services/totals.ts:974


getGiftCardableAmount

getGiftCardableAmount(cartOrOrderCopy to Clipboard): PromiseCopy to Clipboard<numberCopy to Clipboard>

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

Parameters

NameTypeDescription
cartOrOrderCopy to ClipboardCartCopy to Clipboard | OrderCopy to Clipboardthe cart or order to get gift card amount for

Returns

PromiseCopy to Clipboard<numberCopy to Clipboard>

the gift card amount applied to the cart or order

Defined in

packages/medusa/src/services/totals.ts:957


getLineDiscounts

getLineDiscounts(cartOrOrderCopy to Clipboard, discount?Copy to Clipboard): LineDiscountAmountCopy to Clipboard[]

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

Parameters

NameTypeDescription
cartOrOrderCopy to ClipboardObjectCopy to Clipboardthe cart or order to get line discount allocations for
cartOrOrder.claims?Copy to ClipboardClaimOrderCopy to Clipboard[]-
cartOrOrder.itemsCopy to ClipboardLineItemCopy to Clipboard[]-
cartOrOrder.swaps?Copy to ClipboardSwapCopy to Clipboard[]-
discount?Copy to ClipboardDiscountCopy to Clipboardthe discount to use as context for the calculation

Returns

LineDiscountAmountCopy to Clipboard[]

the allocations that the discount has on the items in the cart or order

Defined in

packages/medusa/src/services/totals.ts:722


getLineItemAdjustmentsTotal

getLineItemAdjustmentsTotal(cartOrOrderCopy to Clipboard): numberCopy to Clipboard

Parameters

NameType
cartOrOrderCopy to ClipboardCartCopy to Clipboard | OrderCopy to Clipboard

Returns

numberCopy to Clipboard

Defined in

packages/medusa/src/services/totals.ts:699


getLineItemDiscountAdjustment

getLineItemDiscountAdjustment(lineItemCopy to Clipboard, discountCopy to Clipboard): numberCopy to Clipboard

Parameters

NameType
lineItemCopy to ClipboardLineItemCopy to Clipboard
discountCopy to ClipboardDiscountCopy to Clipboard

Returns

numberCopy to Clipboard

Defined in

packages/medusa/src/services/totals.ts:684


getLineItemRefund

getLineItemRefund(orderCopy to Clipboard, lineItemCopy to Clipboard): PromiseCopy to Clipboard<numberCopy to Clipboard>

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

Parameters

NameTypeDescription
orderCopy to ClipboardOrderCopy to Clipboardorder to use as context for the calculation
lineItemCopy to ClipboardLineItemCopy to Clipboardthe line item to calculate the refund amount for.

Returns

PromiseCopy to Clipboard<numberCopy to Clipboard>

the line item refund amount.

Defined in

packages/medusa/src/services/totals.ts:507


getLineItemTotal

getLineItemTotal(lineItemCopy to Clipboard, cartOrOrderCopy to Clipboard, options?Copy to Clipboard): PromiseCopy to Clipboard<numberCopy to Clipboard>

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

NameTypeDescription
lineItemCopy to ClipboardLineItemCopy to Clipboardthe line item to calculate a total for
cartOrOrderCopy to ClipboardCartCopy to Clipboard | OrderCopy to Clipboardthe cart or order to use as context for the calculation
optionsCopy to ClipboardGetLineItemTotalOptionsCopy to Clipboardthe options to use for the calculation

Returns

PromiseCopy to Clipboard<numberCopy to Clipboard>

the line item total

Defined in

packages/medusa/src/services/totals.ts:930


getLineItemTotals

getLineItemTotals(lineItemCopy to Clipboard, cartOrOrderCopy to Clipboard, options?Copy to Clipboard): PromiseCopy to Clipboard<LineItemTotalsCopy to Clipboard>

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

NameTypeDescription
lineItemCopy to ClipboardLineItemCopy to Clipboardthe line item to calculate totals for
cartOrOrderCopy to ClipboardCartCopy to Clipboard | OrderCopy to Clipboardthe cart or order to use as context for the calculation
optionsCopy to ClipboardLineItemTotalsOptionsCopy to Clipboardthe options to evaluate the line item totals for

Returns

PromiseCopy to Clipboard<LineItemTotalsCopy to Clipboard>

the breakdown of the line item totals

Defined in

packages/medusa/src/services/totals.ts:778


getPaidTotal

getPaidTotal(orderCopy to Clipboard): numberCopy to Clipboard

Gets the total payments made on an order

Parameters

NameTypeDescription
orderCopy to ClipboardOrderCopy to Clipboardthe order to calculate paid amount for

Returns

numberCopy to Clipboard

the total paid amount

Defined in

packages/medusa/src/services/totals.ts:160


getRefundTotal

getRefundTotal(orderCopy to Clipboard, lineItemsCopy to Clipboard): PromiseCopy to Clipboard<numberCopy to Clipboard>

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

NameTypeDescription
orderCopy to ClipboardOrderCopy to Clipboardcart or order to calculate subtotal for
lineItemsCopy to ClipboardLineItemCopy to Clipboard[]the line items to calculate refund total for

Returns

PromiseCopy to Clipboard<numberCopy to Clipboard>

the calculated subtotal

Defined in

packages/medusa/src/services/totals.ts:586


getRefundedTotal

getRefundedTotal(orderCopy to Clipboard): numberCopy to Clipboard

Gets the total refund amount for an order.

Parameters

NameTypeDescription
orderCopy to ClipboardOrderCopy to Clipboardthe order to get total refund amount for.

Returns

numberCopy to Clipboard

the total refunded amount for an order.

Defined in

packages/medusa/src/services/totals.ts:492


getShippingMethodTotals

getShippingMethodTotals(shippingMethodCopy to Clipboard, cartOrOrderCopy to Clipboard, opts?Copy to Clipboard): PromiseCopy to Clipboard<ShippingMethodTotalsCopy to Clipboard>

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

Parameters

NameTypeDescription
shippingMethodCopy to ClipboardShippingMethodCopy to Clipboardthe shipping method to get totals breakdown for.
cartOrOrderCopy to ClipboardCartCopy to Clipboard | OrderCopy to Clipboardthe cart or order to use as context for the breakdown
optsCopy to ClipboardGetShippingMethodTotalsOptionsCopy to Clipboardoptions for what should be included

Returns

PromiseCopy to Clipboard<ShippingMethodTotalsCopy to Clipboard>

An object that breaks down the totals for the shipping method

Defined in

packages/medusa/src/services/totals.ts:194


getShippingTotal

getShippingTotal(cartOrOrderCopy to Clipboard): PromiseCopy to Clipboard<numberCopy to Clipboard>

Calculates shipping total

Parameters

NameTypeDescription
cartOrOrderCopy to ClipboardCartCopy to Clipboard | OrderCopy to Clipboardcart or order to calculate subtotal for

Returns

PromiseCopy to Clipboard<numberCopy to Clipboard>

shipping total

Defined in

packages/medusa/src/services/totals.ts:322


getSubtotal

getSubtotal(cartOrOrderCopy to Clipboard, opts?Copy to Clipboard): PromiseCopy to Clipboard<numberCopy to Clipboard>

Calculates subtotal of a given cart or order.

Parameters

NameTypeDescription
cartOrOrderCopy to ClipboardCartCopy to Clipboard | OrderCopy to Clipboardcart or order to calculate subtotal for
optsCopy to ClipboardSubtotalOptionsCopy to Clipboardoptions

Returns

PromiseCopy to Clipboard<numberCopy to Clipboard>

the calculated subtotal

Defined in

packages/medusa/src/services/totals.ts:286


getSwapTotal

getSwapTotal(orderCopy to Clipboard): numberCopy to Clipboard

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

Parameters

NameTypeDescription
orderCopy to ClipboardOrderCopy to Clipboardthe order to calculate swap total for

Returns

numberCopy to Clipboard

the swap total

Defined in

packages/medusa/src/services/totals.ts:175


getTaxTotal

getTaxTotal(cartOrOrderCopy to Clipboard, forceTaxes?Copy to Clipboard): PromiseCopy to Clipboard<nullCopy to Clipboard | numberCopy to Clipboard>

Calculates tax total Currently based on the Danish tax system

Parameters

NameTypeDefault valueDescription
cartOrOrderCopy to ClipboardCartCopy to Clipboard | OrderCopy to ClipboardundefinedCopy to Clipboardcart or order to calculate tax total for
forceTaxesCopy to ClipboardbooleanCopy to ClipboardfalseCopy to Clipboardwhether taxes should be calculated regardless of region settings

Returns

PromiseCopy to Clipboard<nullCopy to Clipboard | numberCopy to Clipboard>

tax total

Defined in

packages/medusa/src/services/totals.ts:349


getTotal

getTotal(cartOrOrderCopy to Clipboard, options?Copy to Clipboard): PromiseCopy to Clipboard<numberCopy to Clipboard>

Calculates total of a given cart or order.

Parameters

NameTypeDescription
cartOrOrderCopy to ClipboardCartCopy to Clipboard | OrderCopy to Clipboardobject to calculate total for
optionsCopy to ClipboardGetTotalsOptionsCopy to Clipboardoptions to calculate by

Returns

PromiseCopy to Clipboard<numberCopy to Clipboard>

the calculated subtotal

Defined in

packages/medusa/src/services/totals.ts:137


rounded

rounded(valueCopy to Clipboard): numberCopy to Clipboard

Rounds a number using Math.round.

Parameters

NameTypeDescription
valueCopy to ClipboardnumberCopy to Clipboardthe value to round

Returns

numberCopy to Clipboard

the rounded value

Defined in

packages/medusa/src/services/totals.ts:1055


shouldRetryTransaction_

ProtectedCopy to Clipboard shouldRetryTransaction_(errCopy to Clipboard): booleanCopy to Clipboard

Parameters

NameType
errCopy to ClipboardRecordCopy to Clipboard<stringCopy to Clipboard, unknownCopy to Clipboard> | { codeCopy to Clipboard: stringCopy to Clipboard }

Returns

booleanCopy to Clipboard

Inherited from

TransactionBaseService.shouldRetryTransaction_

Defined in

packages/medusa/src/interfaces/transaction-base-service.ts:31


withTransaction

withTransaction(transactionManager?Copy to Clipboard): TotalsServiceCopy to Clipboard

Parameters

NameType
transactionManager?Copy to ClipboardEntityManagerCopy to Clipboard

Returns

TotalsServiceCopy to Clipboard

Inherited from

TransactionBaseService.withTransaction

Defined in

packages/medusa/src/interfaces/transaction-base-service.ts:14