CartService
Properties
__configModule__Record<string, unknown>__container__anyRequired__moduleDeclaration__Record<string, unknown>cartRepository_Repository<Cart> & { findOneWithRelations: Method findOneWithRelations ; findWithRelations: Method findWithRelations }RequiredgetTotalsRelationsanyRequiredmanager_EntityManagerRequiredtransactionManager_undefined | EntityManagerRequiredEventsobjectRequiredEvents.CREATEDstringRequiredEvents.CUSTOMER_UPDATEDstringRequiredEvents.UPDATEDstringRequiredAccessors
activeManager_
Protected get activeManager_(): EntityManager
Returns
EntityManager
EntityManagerEntityManagerRequiredInherited from
TransactionBaseService.activeManager_
Methods
addLineItem
addLineItem(cartId, lineItem, config?): Promise<void>
Adds a line item to the cart.
Parameters
cartIdstringRequiredconfigobjectconfig.validateSalesChannelsbooleanRequiredReturns
Promise<void>
PromisePromise<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
cartIdstringRequiredconfigobjectconfig.validateSalesChannelsbooleanRequiredReturns
Promise<void>
PromisePromise<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
optionIdstringRequireddataRecord<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
shouldAddbooleanRequiredReturns
Promise<void>
PromisePromise<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
discountCodestringRequiredReturns
Promise<void>
PromisePromise<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
discountCodesstring[]RequiredReturns
Promise<void>
PromisePromise<void>RequiredapplyGiftCard_
Protected applyGiftCard_(cart, code): Promise<void>
Parameters
codestringRequiredReturns
Promise<void>
PromisePromise<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.
TResultobjectRequiredTErrorobjectRequiredParameters
work(transactionManager: EntityManager) => Promise<TResult>RequiredisolationOrErrorHandlerIsolationLevel | (error: TError) => Promise<void | TResult>maybeErrorHandlerOrDontFail(error: TError) => Promise<void | TResult>Returns
Promise<TResult>
PromisePromise<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
cartIdstringRequiredcontextRecord<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
emailstringRequiredReturns
Promise<Customer>
the resultign customer object
createTaxLines
createTaxLines(cartOrId): Promise<void>
Parameters
Returns
Promise<void>
PromisePromise<void>RequireddecorateTotals
decorateTotals(cart, totalsConfig?): Promise<WithRequiredProperty<Cart, "total">>
Parameters
totalsConfigTotalsConfigReturns
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
cartIdstringRequiredReturns
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
cartIdstringRequiredproviderIdstringRequiredReturns
Promise<void>
PromisePromise<void>RequireddeleteTaxLines
deleteTaxLines(id): Promise<void>
Parameters
idstringRequiredReturns
Promise<void>
PromisePromise<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
optionIdstringRequiredReturns
undefined | CustomShippingOption
undefined \| CustomShippingOptionundefined | CustomShippingOptiongetValidatedSalesChannel
Protected getValidatedSalesChannel(salesChannelId?): Promise<SalesChannel>
Parameters
salesChannelIdstringReturns
Promise<SalesChannel>
list
list(selector, config?): Promise<Cart[]>
Parameters
configFindConfig<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
newSalesChannelIdstringRequiredReturns
Promise<void>
PromisePromise<void>RequiredrefreshAdjustments_
Protected refreshAdjustments_(cart): Promise<void>
Parameters
Returns
Promise<void>
PromisePromise<void>RequiredrefreshPaymentSession
refreshPaymentSession(cartId, providerId): Promise<void>
Refreshes a payment session on a cart
Parameters
cartIdstringRequiredproviderIdstringRequiredReturns
Promise<void>
PromisePromise<void>RequiredremoveDiscount
removeDiscount(cartId, discountCode): Promise<Cart>
Removes a discount based on a discount code.
Parameters
cartIdstringRequireddiscountCodestringRequiredReturns
Promise<Cart>
the resulting cart
removeLineItem
removeLineItem(cartId, lineItemId): Promise<Cart>
Removes a line item from the cart.
Parameters
cartIdstringRequiredlineItemIdstringRequiredReturns
Promise<Cart>
the result of the update operation
retrieve
retrieve(cartId, options?, totalsConfig?): Promise<Cart>
Gets a cart by id.
Parameters
cartIdstringRequiredoptionsFindConfig<Cart>totalsConfigTotalsConfigReturns
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
cartIdstringRequiredkeystringRequiredvaluestring | 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
cartIdstringRequiredproviderIdstringRequiredReturns
Promise<void>
PromisePromise<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>
PromisePromise<void>RequiredsetRegion_
Protected setRegion_(cart, regionId, countryCode): Promise<void>
Set's the region of a cart.
Parameters
regionIdstringRequiredcountryCodenull | stringRequiredReturns
Promise<void>
PromisePromise<void>RequiredshouldRetryTransaction_
Protected shouldRetryTransaction_(err): boolean
Parameters
errRecord<string, unknown> | { code: string }RequiredReturns
boolean
booleanbooleanInherited 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>
PromisePromise<void>RequiredupdateCustomerId_
Protected updateCustomerId_(cart, customerId): Promise<void>
Sets the customer id of a cart
Parameters
customerIdstringRequiredReturns
Promise<void>
PromisePromise<void>RequiredupdateLineItem
updateLineItem(cartId, lineItemId, update): Promise<Cart>
Updates a cart's existing line item.
Parameters
cartIdstringRequiredlineItemIdstringRequiredReturns
Promise<Cart>
the result of the update operation
updatePaymentSession
updatePaymentSession(cartId, update): Promise<Cart>
Updates the currently selected payment session.
Parameters
cartIdstringRequiredupdateRecord<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>
PromisePromise<void>RequiredupdateUnitPrices_
Protected updateUnitPrices_(cart, regionId?, customer_id?): Promise<void>
Parameters
Returns
Promise<void>
PromisePromise<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_idobjectRequiredsales_channel_id.sales_channel_idnull | stringRequiredReturns
Promise<boolean>
PromisePromise<boolean>Requireda boolean indicating validation result
PromisePromise<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
lineItemShippingProfiledIdstringRequiredReturns
boolean
booleanbooleanwithTransaction
withTransaction(transactionManager?): CartService
Parameters
transactionManagerEntityManager