CartService
Properties
__configModule__
Record<string, unknown>__container__
anyRequired__moduleDeclaration__
Record<string, unknown>cartRepository_
Repository<Cart> & { findOneWithRelations: Method findOneWithRelations ; findWithRelations: Method findWithRelations }RequiredgetTotalsRelations
anyRequiredmanager_
EntityManagerRequiredtransactionManager_
undefined | EntityManagerRequiredEvents
objectRequiredEvents.CREATED
stringRequiredEvents.CUSTOMER_UPDATED
stringRequiredEvents.UPDATED
stringRequiredAccessors
activeManager_
Protected
get
activeManager_(): EntityManager
Returns
EntityManager
EntityManager
EntityManagerRequiredInherited from
TransactionBaseService.activeManager_
Methods
addLineItem
addLineItem(cartId
, lineItem
, config?
): Promise
<void
>
Adds a line item to the cart.
Parameters
cartId
stringRequiredconfig
objectconfig.validateSalesChannels
booleanRequiredReturns
Promise
<void
>
Promise
Promise<void>RequiredDeprecated
Use addOrUpdateLineItems instead.
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
cartId
stringRequiredconfig
objectconfig.validateSalesChannels
booleanRequiredReturns
Promise
<void
>
Promise
Promise<void>RequiredaddShippingMethod
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
optionId
stringRequireddata
Record<string, unknown>Returns
Promise
<Cart
>
the result of the update operation
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
shouldAdd
booleanRequiredReturns
Promise
<void
>
Promise
Promise<void>RequiredapplyDiscount
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
discountCode
stringRequiredReturns
Promise
<void
>
Promise
Promise<void>RequiredapplyDiscounts
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
discountCodes
string[]RequiredReturns
Promise
<void
>
Promise
Promise<void>RequiredapplyGiftCard_
Protected
applyGiftCard_(cart
, code
): Promise
<void
>
Parameters
code
stringRequiredReturns
Promise
<void
>
Promise
Promise<void>RequiredatomicPhase_
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.
TResult
objectRequiredTError
objectRequiredParameters
work
(transactionManager: EntityManager) => Promise<TResult>RequiredisolationOrErrorHandler
IsolationLevel | (error: TError) => Promise<void | TResult>maybeErrorHandlerOrDontFail
(error: TError) => Promise<void | TResult>Returns
Promise
<TResult
>
Promise
Promise<TResult>RequiredInherited from
TransactionBaseService.atomicPhase_
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
cartId
stringRequiredcontext
Record<string, unknown> & { cart_id: string }Returns
Promise
<Cart
>
the resulting cart
create
create(data
): Promise
<Cart
>
Creates a cart.
Parameters
Returns
Promise
<Cart
>
the result of the create operation
createOrFetchGuestCustomerFromEmail_
Protected
createOrFetchGuestCustomerFromEmail_(email
): Promise
<Customer
>
Creates or fetches a user based on an email.
Parameters
email
stringRequiredReturns
Promise
<Customer
>
the resultign customer object
createTaxLines
createTaxLines(cartOrId
): Promise
<void
>
Parameters
Returns
Promise
<void
>
Promise
Promise<void>RequireddecorateTotals
decorateTotals(cart
, totalsConfig?
): Promise
<WithRequiredProperty
<Cart
, "total"
>>
Parameters
totalsConfig
TotalsConfigReturns
Promise
<WithRequiredProperty
<Cart
, "total"
>>
decorateTotals_
Protected
decorateTotals_(cart
, totalsToSelect
, options?
): Promise
<Cart
>
Parameters
Returns
Promise
<Cart
>
Deprecated
Use decorateTotals instead
delete
delete(cartId
): Promise
<Cart
>
Deletes a cart from the database. Completed carts cannot be deleted.
Parameters
cartId
stringRequiredReturns
Promise
<Cart
>
the deleted cart or undefined if the cart was not found.
deletePaymentSession
deletePaymentSession(cartId
, providerId
): Promise
<void
>
Removes a payment session from the cart.
Parameters
cartId
stringRequiredproviderId
stringRequiredReturns
Promise
<void
>
Promise
Promise<void>RequireddeleteTaxLines
deleteTaxLines(id
): Promise
<void
>
Parameters
id
stringRequiredReturns
Promise
<void
>
Promise
Promise<void>RequiredfindCustomShippingOption
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
optionId
stringRequiredReturns
undefined
| CustomShippingOption
undefined \| CustomShippingOption
undefined | CustomShippingOptiongetValidatedSalesChannel
Protected
getValidatedSalesChannel(salesChannelId?
): Promise
<SalesChannel
>
Parameters
salesChannelId
stringReturns
Promise
<SalesChannel
>
list
list(selector
, config?
): Promise
<Cart
[]>
Parameters
config
FindConfig<Cart>Returns
Promise
<Cart
[]>
the result of the find operation
onSalesChannelChange
Protected
onSalesChannelChange(cart
, newSalesChannelId
): Promise
<void
>
Remove the cart line item that does not belongs to the newly assigned sales channel
Parameters
newSalesChannelId
stringRequiredReturns
Promise
<void
>
Promise
Promise<void>RequiredrefreshAdjustments_
Protected
refreshAdjustments_(cart
): Promise
<void
>
Parameters
Returns
Promise
<void
>
Promise
Promise<void>RequiredrefreshPaymentSession
refreshPaymentSession(cartId
, providerId
): Promise
<void
>
Refreshes a payment session on a cart
Parameters
cartId
stringRequiredproviderId
stringRequiredReturns
Promise
<void
>
Promise
Promise<void>RequiredremoveDiscount
removeDiscount(cartId
, discountCode
): Promise
<Cart
>
Removes a discount based on a discount code.
Parameters
cartId
stringRequireddiscountCode
stringRequiredReturns
Promise
<Cart
>
the resulting cart
removeLineItem
removeLineItem(cartId
, lineItemId
): Promise
<Cart
>
Removes a line item from the cart.
Parameters
cartId
stringRequiredlineItemId
stringRequiredReturns
Promise
<Cart
>
the result of the update operation
retrieve
retrieve(cartId
, options?
, totalsConfig?
): Promise
<Cart
>
Gets a cart by id.
Parameters
cartId
stringRequiredoptions
FindConfig<Cart>totalsConfig
TotalsConfigReturns
Promise
<Cart
>
the cart document.
retrieveLegacy
Protected
retrieveLegacy(cartId
, options?
, totalsConfig?
): Promise
<Cart
>
Parameters
Returns
Promise
<Cart
>
Deprecated
retrieveWithTotals
retrieveWithTotals(cartId
, options?
, totalsConfig?
): Promise
<WithRequiredProperty
<Cart
, "total"
>>
Parameters
Returns
Promise
<WithRequiredProperty
<Cart
, "total"
>>
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
cartId
stringRequiredkey
stringRequiredvalue
string | numberRequiredReturns
Promise
<Cart
>
resolves to the updated result.
setPaymentSession
setPaymentSession(cartId
, providerId
): Promise
<void
>
Selects a payment session for a cart and creates a payment object in the external provider system
Parameters
cartId
stringRequiredproviderId
stringRequiredReturns
Promise
<void
>
Promise
Promise<void>RequiredsetPaymentSessions
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
Returns
Promise
<void
>
Promise
Promise<void>RequiredsetRegion_
Protected
setRegion_(cart
, regionId
, countryCode
): Promise
<void
>
Set's the region of a cart.
Parameters
regionId
stringRequiredcountryCode
null | stringRequiredReturns
Promise
<void
>
Promise
Promise<void>RequiredshouldRetryTransaction_
Protected
shouldRetryTransaction_(err
): boolean
Parameters
err
Record<string, unknown> | { code: string }RequiredReturns
boolean
boolean
booleanInherited from
TransactionBaseService.shouldRetryTransaction_
transformQueryForTotals_
Protected
transformQueryForTotals_(config
): FindConfig
<Cart
> & { totalsToSelect
: TotalField
[] }
Parameters
Returns
FindConfig
<Cart
> & { totalsToSelect
: TotalField
[] }
[`FindConfig`](/references/js-client/internal/interfaces/admin_discounts.internal.internal.FindConfig)<[`Cart`](/references/js-client/internal/classes/admin_collections.internal.Cart)\> & { `totalsToSelect`: [`TotalField`](/references/js-client/internal/modules/admin_discounts.internal.internal#totalfield)[] }
FindConfig<Cart> & { totalsToSelect: TotalField[] }update
update(cartOrId
, data
): Promise
<Cart
>
Parameters
Returns
Promise
<Cart
>
updateBillingAddress_
Protected
updateBillingAddress_(cart
, addressOrId
, addrRepo
): Promise
<void
>
Updates the cart's billing address.
Parameters
Returns
Promise
<void
>
Promise
Promise<void>RequiredupdateCustomerId_
Protected
updateCustomerId_(cart
, customerId
): Promise
<void
>
Sets the customer id of a cart
Parameters
customerId
stringRequiredReturns
Promise
<void
>
Promise
Promise<void>RequiredupdateLineItem
updateLineItem(cartId
, lineItemId
, update
): Promise
<Cart
>
Updates a cart's existing line item.
Parameters
cartId
stringRequiredlineItemId
stringRequiredReturns
Promise
<Cart
>
the result of the update operation
updatePaymentSession
updatePaymentSession(cartId
, update
): Promise
<Cart
>
Updates the currently selected payment session.
Parameters
cartId
stringRequiredupdate
Record<string, unknown>RequiredReturns
Promise
<Cart
>
the resulting cart
updateShippingAddress_
Protected
updateShippingAddress_(cart
, addressOrId
, addrRepo
): Promise
<void
>
Updates the cart's shipping address.
Parameters
Returns
Promise
<void
>
Promise
Promise<void>RequiredupdateUnitPrices_
Protected
updateUnitPrices_(cart
, regionId?
, customer_id?
): Promise
<void
>
Parameters
Returns
Promise
<void
>
Promise
Promise<void>RequiredvalidateLineItem
Protected
validateLineItem(sales_channel_id
, lineItem
): Promise
<boolean
>
Check if line item's variant belongs to the cart's sales channel.
Parameters
sales_channel_id
objectRequiredsales_channel_id.sales_channel_id
null | stringRequiredReturns
Promise
<boolean
>
Promise
Promise<boolean>Requireda boolean indicating validation result
Promise
Promise<boolean>RequiredvalidateLineItemShipping_
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
lineItemShippingProfiledId
stringRequiredReturns
boolean
boolean
booleanwithTransaction
withTransaction(transactionManager?
): CartService
Parameters
transactionManager
EntityManager