Skip to main content
Skip to main content

CartService

Hierarchy

  • TransactionBaseService

    CartService

Constructors

constructor

new CartService(«destructured»)

Parameters

Name
«destructured»

Overrides

TransactionBaseService.constructor

Defined in

medusa/src/services/cart.ts:141

Properties

__configModule__

Protected Optional Readonly __configModule__: Record<string, unknown>

Inherited from

TransactionBaseService.__configModule__

Defined in

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


__container__

Protected Readonly __container__: any

Inherited from

TransactionBaseService.__container__

Defined in

medusa/src/interfaces/transaction-base-service.ts:13


__moduleDeclaration__

Protected Optional Readonly __moduleDeclaration__: Record<string, unknown>

Inherited from

TransactionBaseService.__moduleDeclaration__

Defined in

medusa/src/interfaces/transaction-base-service.ts:15


addressRepository_

Protected Readonly addressRepository_: Repository<Address>

Defined in

medusa/src/services/cart.ts:114


cartRepository_

Protected Readonly cartRepository_: Repository<Cart> & { findOneWithRelations: Method findOneWithRelations ; findWithRelations: Method findWithRelations }

Defined in

medusa/src/services/cart.ts:113


customShippingOptionService_

Protected Readonly customShippingOptionService_: CustomShippingOptionService

Defined in

medusa/src/services/cart.ts:133


customerService_

Protected Readonly customerService_: CustomerService

Defined in

medusa/src/services/cart.ts:125


discountService_

Protected Readonly discountService_: DiscountService

Defined in

medusa/src/services/cart.ts:128


eventBus_

Protected Readonly eventBus_: EventBusService

Defined in

medusa/src/services/cart.ts:117


featureFlagRouter_

Protected Readonly featureFlagRouter_: FlagRouter

Defined in

medusa/src/services/cart.ts:136


giftCardService_

Protected Readonly giftCardService_: GiftCardService

Defined in

medusa/src/services/cart.ts:129


lineItemAdjustmentService_

Protected Readonly lineItemAdjustmentService_: LineItemAdjustmentService

Defined in

medusa/src/services/cart.ts:135


lineItemRepository_

Protected Readonly lineItemRepository_: Repository<LineItem> & { findByReturn: Method findByReturn }

Defined in

medusa/src/services/cart.ts:116


lineItemService_

Protected Readonly lineItemService_: LineItemService

Defined in

medusa/src/services/cart.ts:123


manager_

Protected manager_: EntityManager

Inherited from

TransactionBaseService.manager_

Defined in

medusa/src/interfaces/transaction-base-service.ts:5


newTotalsService_

Protected Readonly newTotalsService_: NewTotalsService

Defined in

medusa/src/services/cart.ts:132


paymentProviderService_

Protected Readonly paymentProviderService_: PaymentProviderService

Defined in

medusa/src/services/cart.ts:124


paymentSessionRepository_

Protected Readonly paymentSessionRepository_: Repository<PaymentSession>

Defined in

medusa/src/services/cart.ts:115


priceSelectionStrategy_

Protected Readonly priceSelectionStrategy_: IPriceSelectionStrategy

Defined in

medusa/src/services/cart.ts:134


pricingService_

Protected Readonly pricingService_: PricingService

Defined in

medusa/src/services/cart.ts:139


productService_

Protected Readonly productService_: ProductService

Defined in

medusa/src/services/cart.ts:119


productVariantInventoryService_

Protected Readonly productVariantInventoryService_: ProductVariantInventoryService

Defined in

medusa/src/services/cart.ts:138


productVariantService_

Protected Readonly productVariantService_: ProductVariantService

Defined in

medusa/src/services/cart.ts:118


regionService_

Protected Readonly regionService_: RegionService

Defined in

medusa/src/services/cart.ts:122


salesChannelService_

Protected Readonly salesChannelService_: SalesChannelService

Defined in

medusa/src/services/cart.ts:121


shippingMethodRepository_

Protected Readonly shippingMethodRepository_: Repository<ShippingMethod>

Defined in

medusa/src/services/cart.ts:112


shippingOptionService_

Protected Readonly shippingOptionService_: ShippingOptionService

Defined in

medusa/src/services/cart.ts:126


shippingProfileService_

Protected Readonly shippingProfileService_: ShippingProfileService

Defined in

medusa/src/services/cart.ts:127


storeService_

Protected Readonly storeService_: StoreService

Defined in

medusa/src/services/cart.ts:120


taxProviderService_

Protected Readonly taxProviderService_: TaxProviderService

Defined in

medusa/src/services/cart.ts:130


totalsService_

Protected Readonly totalsService_: TotalsService

Defined in

medusa/src/services/cart.ts:131


transactionManager_

Protected transactionManager_: undefined | EntityManager

Inherited from

TransactionBaseService.transactionManager_

Defined in

medusa/src/interfaces/transaction-base-service.ts:6


Events

Static Readonly Events: Object

Type declaration

NameType
CREATEDstring
CUSTOMER_UPDATEDstring
UPDATEDstring

Defined in

medusa/src/services/cart.ts:106

Accessors

activeManager_

Protected get activeManager_(): EntityManager

Returns

EntityManager

-EntityManager:

Inherited from

TransactionBaseService.activeManager_

Defined in

medusa/src/interfaces/transaction-base-service.ts:8

Methods

addLineItem

addLineItem(cartId, lineItem, config?): Promise<void>

Adds a line item to the cart.

Parameters

NameDefault valueDescription
cartIdstringthe id of the cart that we will add to
lineItemLineItemthe line item to add.
configobjectvalidateSalesChannels - should check if product belongs to the same sales channel as cart (if cart has associated sales channel)
config.validateSalesChannelsbooleantrue

Returns

Promise<void>

-Promise: the result of the update operation

Deprecated

Use addOrUpdateLineItems instead.

Defined in

medusa/src/services/cart.ts:652


addOrUpdateLineItems

addOrUpdateLineItems(cartId, lineItems, config?): Promise<void>

Adds or update one or multiple line items to the cart. It also update all existing items in the cart to have has_shipping to false. Finally, the adjustments will be updated.

Parameters

NameDefault valueDescription
cartIdstringthe id of the cart that we will add to
lineItemsLineItem | LineItem[]the line items to add.
configobjectvalidateSalesChannels - should check if product belongs to the same sales channel as cart (if cart has associated sales channel)
config.validateSalesChannelsbooleantrue

Returns

Promise<void>

-Promise: the result of the update operation

Defined in

medusa/src/services/cart.ts:792


addShippingMethod

addShippingMethod(cartOrId, optionId, data?): Promise<Cart>

Adds the shipping method to the list of shipping methods associated with the cart. Shipping Methods are the ways that an order is shipped, whereas a Shipping Option is a possible way to ship an order. Shipping Methods may also have additional details in the data field such as an id for a package shop.

Parameters

NameDescription
cartOrIdstring | Cart
optionIdstring
dataRecord<string, unknown>

Returns

Promise<Cart>

-Promise: the result of the update operation -Cart:

Defined in

medusa/src/services/cart.ts:2177


adjustFreeShipping_

Protected adjustFreeShipping_(cart, shouldAdd): Promise<void>

Ensures shipping total on cart is correct in regards to a potential free shipping discount If a free shipping is present, we set shipping methods price to 0 if a free shipping was present, we set shipping methods to original amount

Parameters

NameDescription
cartCart
shouldAddboolean

Returns

Promise<void>

-Promise: void

Defined in

medusa/src/services/cart.ts:1111


applyDiscount

applyDiscount(cart, discountCode): Promise<void>

Updates the cart's discounts. If discount besides free shipping is already applied, this will be overwritten Throws if discount regions does not include the cart region

Parameters

NameDescription
cartCart
discountCodestring

Returns

Promise<void>

-Promise:

Defined in

medusa/src/services/cart.ts:1522


applyDiscounts

applyDiscounts(cart, discountCodes): Promise<void>

Updates the cart's discounts. If discount besides free shipping is already applied, this will be overwritten Throws if discount regions does not include the cart region

Parameters

NameDescription
cartCart
discountCodesstring[]

Returns

Promise<void>

-Promise:

Defined in

medusa/src/services/cart.ts:1534


applyGiftCard_

Protected applyGiftCard_(cart, code): Promise<void>

Parameters

Name
cart
code

Returns

Promise<void>

-Promise:

Defined in

medusa/src/services/cart.ts:1487


atomicPhase_

Protected atomicPhase_<TResult, TError>(work, isolationOrErrorHandler?, maybeErrorHandlerOrDontFail?): Promise<TResult>

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.

Name
TResult
TError

Parameters

NameDescription
work(transactionManager: EntityManager) => Promise<TResult>
isolationOrErrorHandler?IsolationLevel | (error: TError) => Promise<void | TResult>
maybeErrorHandlerOrDontFail?(error: TError) => Promise<void | TResult>

Returns

Promise<TResult>

-Promise: the result of the transactional work

Inherited from

TransactionBaseService.atomicPhase_

Defined in

medusa/src/interfaces/transaction-base-service.ts:56


authorizePayment

authorizePayment(cartId, context?): Promise<Cart>

Authorizes a payment for a cart. Will authorize with chosen payment provider. This will return a payment object, that we will use to update our cart payment with. Additionally, if the payment does not require more or fails, we will set the payment on the cart.

Parameters

NameDescription
cartIdstring
contextRecord<string, unknown> & { cart_id: string }

Returns

Promise<Cart>

-Promise: the resulting cart -Cart:

Defined in

medusa/src/services/cart.ts:1701


create

create(data): Promise<Cart>

Creates a cart.

Parameters

NameDescription
dataCartCreateProps

Returns

Promise<Cart>

-Promise: the result of the create operation -Cart:

Defined in

medusa/src/services/cart.ts:350


createOrFetchGuestCustomerFromEmail_

Protected createOrFetchGuestCustomerFromEmail_(email): Promise<Customer>

Creates or fetches a user based on an email.

Parameters

NameDescription
emailstring

Returns

Promise<Customer>

-Promise: the resultign customer object -Customer:

Defined in

medusa/src/services/cart.ts:1373


createTaxLines

createTaxLines(cartOrId): Promise<void>

Parameters

Name
cartOrId

Returns

Promise<void>

-Promise:

Defined in

medusa/src/services/cart.ts:2628


decorateTotals

decorateTotals(cart, totalsConfig?): Promise<WithRequiredProperty<Cart, "total">>

Parameters

Name
cart
totalsConfig

Returns

Promise<WithRequiredProperty<Cart, "total">>

-Promise: -WithRequiredProperty: -Cart: -"total": (optional)

Defined in

medusa/src/services/cart.ts:2680


decorateTotals_

Protected decorateTotals_(cart, totalsToSelect, options?): Promise<Cart>

Parameters

Name
cart
totalsToSelect
options

Returns

Promise<Cart>

-Promise: -Cart:

Deprecated

Use decorateTotals instead

Defined in

medusa/src/services/cart.ts:2863


delete

delete(cartId): Promise<Cart>

Deletes a cart from the database. Completed carts cannot be deleted.

Parameters

NameDescription
cartIdstring

Returns

Promise<Cart>

-Promise: the deleted cart or undefined if the cart was not found. -Cart:

Defined in

medusa/src/services/cart.ts:2547


deletePaymentSession

deletePaymentSession(cartId, providerId): Promise<void>

Removes a payment session from the cart.

Parameters

NameDescription
cartIdstring
providerIdstring

Returns

Promise<void>

-Promise: the resulting cart.

Defined in

medusa/src/services/cart.ts:2070


deleteTaxLines

deleteTaxLines(id): Promise<void>

Parameters

Name
id

Returns

Promise<void>

-Promise:

Defined in

medusa/src/services/cart.ts:2660


findCustomShippingOption

findCustomShippingOption(cartCustomShippingOptions, optionId): undefined | CustomShippingOption

Finds the cart's custom shipping options based on the passed option id. throws if custom options is not empty and no shipping option corresponds to optionId

Parameters

NameDescription
cartCustomShippingOptionsCustomShippingOption[]
optionIdstring

Returns

undefined | CustomShippingOption

-undefined \| CustomShippingOption: (optional) custom shipping option

Defined in

medusa/src/services/cart.ts:2310


getTotalsRelations

Private getTotalsRelations(config): string[]

Parameters

Name
config

Returns

string[]

-string[]: -string: (optional)

Defined in

medusa/src/services/cart.ts:2914


getValidatedSalesChannel

Protected getValidatedSalesChannel(salesChannelId?): Promise<SalesChannel>

Parameters

Name
salesChannelId?

Returns

Promise<SalesChannel>

-Promise: -SalesChannel:

Defined in

medusa/src/services/cart.ts:488


list

list(selector, config?): Promise<Cart[]>

Parameters

NameDescription
selectorFilterableCartProps
configFindConfig<Cart>

Returns

Promise<Cart[]>

-Promise: the result of the find operation -Cart[]: -Cart:

Defined in

medusa/src/services/cart.ts:207


onSalesChannelChange

Protected onSalesChannelChange(cart, newSalesChannelId): Promise<void>

Remove the cart line item that does not belongs to the newly assigned sales channel

Parameters

NameDescription
cartCart
newSalesChannelIdstring

Returns

Promise<void>

-Promise: void

Defined in

medusa/src/services/cart.ts:1319


refreshAdjustments_

Protected refreshAdjustments_(cart): Promise<void>

Parameters

Name
cart

Returns

Promise<void>

-Promise:

Defined in

medusa/src/services/cart.ts:2787


refreshPaymentSession

refreshPaymentSession(cartId, providerId): Promise<void>

Refreshes a payment session on a cart

Parameters

NameDescription
cartIdstring
providerIdstring

Returns

Promise<void>

-Promise: the resulting cart.

Defined in

medusa/src/services/cart.ts:2122


removeDiscount

removeDiscount(cartId, discountCode): Promise<Cart>

Removes a discount based on a discount code.

Parameters

NameDescription
cartIdstring
discountCodestring

Returns

Promise<Cart>

-Promise: the resulting cart -Cart:

Defined in

medusa/src/services/cart.ts:1612


removeLineItem

removeLineItem(cartId, lineItemId): Promise<Cart>

Removes a line item from the cart.

Parameters

NameDescription
cartIdstring
lineItemIdstring

Returns

Promise<Cart>

-Promise: the result of the update operation -Cart:

Defined in

medusa/src/services/cart.ts:520


retrieve

retrieve(cartId, options?, totalsConfig?): Promise<Cart>

Gets a cart by id.

Parameters

NameDescription
cartIdstring
optionsFindConfig<Cart>
totalsConfigTotalsConfig

Returns

Promise<Cart>

-Promise: the cart document. -Cart:

Defined in

medusa/src/services/cart.ts:224


retrieveLegacy

Protected retrieveLegacy(cartId, options?, totalsConfig?): Promise<Cart>

Parameters

Name
cartId
options
totalsConfig

Returns

Promise<Cart>

-Promise: -Cart:

Deprecated

Defined in

medusa/src/services/cart.ts:287


retrieveWithTotals

retrieveWithTotals(cartId, options?, totalsConfig?): Promise<WithRequiredProperty<Cart, "total">>

Parameters

Name
cartId
options
totalsConfig

Returns

Promise<WithRequiredProperty<Cart, "total">>

-Promise: -WithRequiredProperty: -Cart: -"total": (optional)

Defined in

medusa/src/services/cart.ts:315


setMetadata

setMetadata(cartId, key, value): Promise<Cart>

Dedicated method to set metadata for a cart. To ensure that plugins does not overwrite each others metadata fields, setMetadata is provided.

Parameters

NameDescription
cartIdstring
keystring
valuestring | number

Returns

Promise<Cart>

-Promise: resolves to the updated result. -Cart:

Defined in

medusa/src/services/cart.ts:2588


setPaymentSession

setPaymentSession(cartId, providerId): Promise<void>

Selects a payment session for a cart and creates a payment object in the external provider system

Parameters

NameDescription
cartIdstring
providerIdstring

Returns

Promise<void>

-Promise:

Defined in

medusa/src/services/cart.ts:1775


setPaymentSessions

setPaymentSessions(cartOrCartId): Promise<void>

Creates, updates and sets payment sessions associated with the cart. The first time the method is called payment sessions will be created for each provider. Additional calls will ensure that payment sessions have correct amounts, currencies, etc. as well as make sure to filter payment sessions that are not available for the cart's region.

Parameters

NameDescription
cartOrCartIdstring | Cart

Returns

Promise<void>

-Promise: the result of the update operation.

Defined in

medusa/src/services/cart.ts:1891


setRegion_

Protected setRegion_(cart, regionId, countryCode): Promise<void>

Set's the region of a cart.

Parameters

NameDescription
cartCart
regionIdstring
countryCodenull | string

Returns

Promise<void>

-Promise: the result of the update operation

Defined in

medusa/src/services/cart.ts:2408


shouldRetryTransaction_

Protected shouldRetryTransaction_(err): boolean

Parameters

Name
err

Returns

boolean

-boolean: (optional)

Inherited from

TransactionBaseService.shouldRetryTransaction_

Defined in

medusa/src/interfaces/transaction-base-service.ts:37


transformQueryForTotals_

Protected transformQueryForTotals_(config): FindConfig<Cart> & { totalsToSelect: TotalField[] }

Parameters

Name
config

Returns

FindConfig<Cart> & { totalsToSelect: TotalField[] }

-`FindConfig<Cart> & { totalsToSelect: TotalField[] }`: (optional)

Defined in

medusa/src/services/cart.ts:2805


update

update(cartOrId, data): Promise<Cart>

Parameters

Name
cartOrId
data

Returns

Promise<Cart>

-Promise: -Cart:

Defined in

medusa/src/services/cart.ts:1152


updateBillingAddress_

Protected updateBillingAddress_(cart, addressOrId, addrRepo): Promise<void>

Updates the cart's billing address.

Parameters

NameDescription
cartCart
addressOrIdstring | AddressPayload | Partial<Address>
addrRepoRepository<Address>

Returns

Promise<void>

-Promise: the result of the update operation

Defined in

medusa/src/services/cart.ts:1400


updateCustomerId_

Protected updateCustomerId_(cart, customerId): Promise<void>

Sets the customer id of a cart

Parameters

NameDescription
cartCart
customerIdstring

Returns

Promise<void>

-Promise: the result of the update operation

Defined in

medusa/src/services/cart.ts:1355


updateLineItem

updateLineItem(cartId, lineItemId, update): Promise<Cart>

Updates a cart's existing line item.

Parameters

NameDescription
cartIdstring
lineItemIdstring
updateLineItemUpdate

Returns

Promise<Cart>

-Promise: the result of the update operation -Cart:

Defined in

medusa/src/services/cart.ts:999


updatePaymentSession

updatePaymentSession(cartId, update): Promise<Cart>

Updates the currently selected payment session.

Parameters

NameDescription
cartIdstring
updateRecord<string, unknown>

Returns

Promise<Cart>

-Promise: the resulting cart -Cart:

Defined in

medusa/src/services/cart.ts:1663


updateShippingAddress_

Protected updateShippingAddress_(cart, addressOrId, addrRepo): Promise<void>

Updates the cart's shipping address.

Parameters

NameDescription
cartCart
addressOrIdstring | AddressPayload | Partial<Address>
addrRepoRepository<Address>

Returns

Promise<void>

-Promise: the result of the update operation

Defined in

medusa/src/services/cart.ts:1438


updateUnitPrices_

Protected updateUnitPrices_(cart, regionId?, customer_id?): Promise<void>

Parameters

Name
cart
regionId?
customer_id?

Returns

Promise<void>

-Promise:

Defined in

medusa/src/services/cart.ts:2329


validateLineItem

Protected validateLineItem(sales_channel_id, lineItem): Promise<boolean>

Check if line item's variant belongs to the cart's sales channel.

Parameters

NameDescription
sales_channel_idobject
sales_channel_id.sales_channel_idnull | string
lineItemLineItemValidateData

Returns

Promise<boolean>

-Promise: a boolean indicating validation result -boolean: (optional)

Defined in

medusa/src/services/cart.ts:618


validateLineItemShipping_

Protected validateLineItemShipping_(shippingMethods, lineItemShippingProfiledId): boolean

Checks if a given line item has a shipping method that can fulfill it. Returns true if all products in the cart can be fulfilled with the current shipping methods.

Parameters

NameDescription
shippingMethodsShippingMethod[]
lineItemShippingProfiledIdstring

Returns

boolean

-boolean: (optional) boolean representing whether shipping method is validated

Defined in

medusa/src/services/cart.ts:589


withTransaction

withTransaction(transactionManager?): CartService

Parameters

Name
transactionManager?

Returns

CartService

-CartService:

Inherited from

TransactionBaseService.withTransaction

Defined in

medusa/src/interfaces/transaction-base-service.ts:20

Was this section helpful?