Skip to main content
Skip to main content

CartService

constructor

Parameters

__namedParametersInjectedDependenciesRequired

Properties

__configModule__Record<string, unknown>
__container__anyRequired
__moduleDeclaration__Record<string, unknown>
addressRepository_Repository<Address>Required
cartRepository_Repository<Cart> & objectRequired
customShippingOptionService_CustomShippingOptionServiceRequired
customerService_CustomerServiceRequired
discountService_DiscountServiceRequired
eventBus_EventBusServiceRequired
featureFlagRouter_FlagRouterRequired
giftCardService_GiftCardServiceRequired
lineItemAdjustmentService_LineItemAdjustmentServiceRequired
lineItemRepository_Repository<LineItem> & objectRequired
lineItemService_LineItemServiceRequired
manager_EntityManagerRequired
newTotalsService_NewTotalsServiceRequired
paymentProviderService_PaymentProviderServiceRequired
paymentSessionRepository_Repository<PaymentSession>Required
priceSelectionStrategy_IPriceSelectionStrategyRequired
pricingService_PricingServiceRequired
productService_ProductServiceRequired
productVariantInventoryService_ProductVariantInventoryServiceRequired
productVariantService_ProductVariantServiceRequired
regionService_RegionServiceRequired
salesChannelService_SalesChannelServiceRequired
shippingMethodRepository_Repository<ShippingMethod>Required
shippingOptionService_ShippingOptionServiceRequired
shippingProfileService_ShippingProfileServiceRequired
storeService_StoreServiceRequired
taxProviderService_TaxProviderServiceRequired
totalsService_TotalsServiceRequired
transactionManager_undefined | EntityManagerRequired
EventsobjectRequired
Events.CREATEDstringRequired

Default: "cart.created"

Events.CUSTOMER_UPDATEDstringRequired

Default: "cart.customer_updated"

Events.UPDATEDstringRequired

Default: "cart.updated"


Accessors

activeManager_

Returns

EntityManagerEntityManagerRequired

Methods

addLineItem

Adds a line item to the cart.

Parameters

cartIdstringRequired
the id of the cart that we will add to
lineItemLineItemRequired
the line item to add.
configobjectRequired
validateSalesChannels - should check if product belongs to the same sales channel as cart (if cart has associated sales channel)
config.validateSalesChannelsbooleanRequired

Default: true

Returns

PromisePromise<void>Required
the result of the update operation

Deprecated

Use addOrUpdateLineItems instead.


addOrUpdateLineItems

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

cartIdstringRequired
the id of the cart that we will add to
lineItemsLineItem | LineItem[]Required
the line items to add.
configobjectRequired
validateSalesChannels - should check if product belongs to the same sales channel as cart (if cart has associated sales channel)
config.validateSalesChannelsbooleanRequired

Default: true

Returns

PromisePromise<void>Required
the result of the update operation

addShippingMethod

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

cartOrIdstring | CartRequired
the id of the cart to add shipping method to
optionIdstringRequired
id of shipping option to add as valid method
dataRecord<string, unknown>Required
the fulmillment data for the method

Default: {}

Returns

PromisePromise<Cart>Required
the result of the update operation

adjustFreeShipping_

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

cartCartRequired
the cart to adjust free shipping for
shouldAddbooleanRequired
flag to indicate, if we should add or remove

Returns

PromisePromise<void>Required
void

applyDiscount

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

cartCartRequired
the cart to update
discountCodestringRequired
the discount code

Returns

PromisePromise<void>Required

applyDiscounts

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

cartCartRequired
the cart to update
discountCodesstring[]Required
the discount code(s) to apply

Returns

PromisePromise<void>Required

applyGiftCard_

Parameters

cartCartRequired
codestringRequired

Returns

PromisePromise<void>Required

atomicPhase_

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

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

PromisePromise<TResult>Required
the result of the transactional work

authorizePayment

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

cartIdstringRequired
the id of the cart to authorize payment for
contextRecord<string, unknown> & objectRequired
object containing whatever is relevant for authorizing the payment with the payment provider. As an example, this could be IP address or similar for fraud handling.

Returns

PromisePromise<Cart>Required
the resulting cart

create

Creates a cart.

Parameters

dataCartCreatePropsRequired
the data to create the cart with

Returns

PromisePromise<Cart>Required
the result of the create operation

createOrFetchGuestCustomerFromEmail_

Creates or fetches a user based on an email.

Parameters

emailstringRequired
the email to use

Returns

PromisePromise<Customer>Required
the resultign customer object

createTaxLines

Parameters

cartOrIdstring | CartRequired

Returns

PromisePromise<void>Required

decorateTotals

Parameters

cartCartRequired
totalsConfigTotalsConfigRequired

Default: {}

Returns

PromisePromise<WithRequiredProperty<Cart, "total">>Required

decorateTotals_

Parameters

cartCartRequired
totalsToSelectTotalField[]Required
optionsTotalsConfigRequired

Returns

PromisePromise<Cart>Required

Deprecated

Use decorateTotals instead


delete

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

Parameters

cartIdstringRequired
the id of the cart to delete

Returns

PromisePromise<Cart>Required
the deleted cart or undefined if the cart was not found.

deletePaymentSession

Removes a payment session from the cart.

Parameters

cartIdstringRequired
the id of the cart to remove from
providerIdstringRequired
the id of the provider whose payment session should be removed.

Returns

PromisePromise<void>Required
the resulting cart.

deleteTaxLines

Parameters

idstringRequired

Returns

PromisePromise<void>Required

findCustomShippingOption

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

cartCustomShippingOptionsCustomShippingOption[]Required
the cart's custom shipping options
optionIdstringRequired
id of the normal or custom shipping option to find in the cartCustomShippingOptions

Returns

undefined \| CustomShippingOptionundefined | CustomShippingOption
custom shipping option

getTotalsRelations

Parameters

configFindConfig<Cart>Required

Returns

string[]string[]Required

getValidatedSalesChannel

Parameters

salesChannelIdstring

Returns

PromisePromise<SalesChannel>Required

list

Parameters

selectorFilterableCartPropsRequired
the query object for find
configFindConfig<Cart>Required
config object

Default: {}

Returns

PromisePromise<Cart[]>Required
the result of the find operation

onSalesChannelChange

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

Parameters

cartCartRequired
The cart being updated
newSalesChannelIdstringRequired
The new sales channel being assigned to the cart

Returns

PromisePromise<void>Required
void

refreshAdjustments_

Parameters

cartCartRequired

Returns

PromisePromise<void>Required

refreshPaymentSession

Refreshes a payment session on a cart

Parameters

cartIdstringRequired
the id of the cart to remove from
providerIdstringRequired
the id of the provider whose payment session should be removed.

Returns

PromisePromise<void>Required
the resulting cart.

removeDiscount

Removes a discount based on a discount code.

Parameters

cartIdstringRequired
the id of the cart to remove from
discountCodestringRequired
the discount code to remove

Returns

PromisePromise<Cart>Required
the resulting cart

removeLineItem

Removes a line item from the cart.

Parameters

cartIdstringRequired
the id of the cart that we will remove from
lineItemIdstringRequired
the line item to remove.

Returns

PromisePromise<Cart>Required
the result of the update operation

retrieve

Gets a cart by id.

Parameters

cartIdstringRequired
the id of the cart to get.
optionsFindConfig<Cart>Required
the options to get a cart

Default: {}

totalsConfigTotalsConfigRequired

Default: {}

Returns

PromisePromise<Cart>Required
the cart document.

retrieveLegacy

Parameters

cartIdstringRequired
optionsFindConfig<Cart>Required

Default: {}

totalsConfigTotalsConfigRequired

Default: {}

Returns

PromisePromise<Cart>Required

Deprecated


retrieveWithTotals

Parameters

cartIdstringRequired
optionsFindConfig<Cart>Required

Default: {}

totalsConfigTotalsConfigRequired

Default: {}

Returns

PromisePromise<WithRequiredProperty<Cart, "total">>Required

setMetadata

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

Parameters

cartIdstringRequired
the cart to apply metadata to.
keystringRequired
key for metadata field
valuestring | numberRequired
value for metadata field.

Returns

PromisePromise<Cart>Required
resolves to the updated result.

setPaymentSession

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

Parameters

cartIdstringRequired
the id of the cart to add payment method to
providerIdstringRequired
the id of the provider to be set to the cart

Returns

PromisePromise<void>Required

setPaymentSessions

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

cartOrCartIdstring | CartRequired
the id of the cart to set payment session for

Returns

PromisePromise<void>Required
the result of the update operation.

setRegion_

Set's the region of a cart.

Parameters

cartCartRequired
the cart to set region on
regionIdstringRequired
the id of the region to set the region to
countryCodenull | stringRequired
the country code to set the country to

Returns

PromisePromise<void>Required
the result of the update operation

shouldRetryTransaction_

Parameters

errRecord<string, unknown> | objectRequired

Returns

booleanboolean

transformQueryForTotals_

Parameters

configFindConfig<Cart>Required

Returns

FindConfig&#60;Cart&#62; & objectFindConfig<Cart> & object

update

Parameters

cartOrIdstring | CartRequired
dataCartUpdatePropsRequired

Returns

PromisePromise<Cart>Required

updateBillingAddress_

Updates the cart's billing address.

Parameters

cartCartRequired
the cart to update
addressOrIdstring | AddressPayload | Partial<Address>Required
the value to set the billing address to
addrRepoRepository<Address>Required
the repository to use for address updates

Returns

PromisePromise<void>Required
the result of the update operation

updateCustomerId_

Sets the customer id of a cart

Parameters

cartCartRequired
the cart to add email to
customerIdstringRequired
the customer to add to cart

Returns

PromisePromise<void>Required
the result of the update operation

updateLineItem

Updates a cart's existing line item.

Parameters

cartIdstringRequired
the id of the cart to update
lineItemIdstringRequired
the id of the line item to update.
updateLineItemUpdateRequired

Returns

PromisePromise<Cart>Required
the result of the update operation

updatePaymentSession

Updates the currently selected payment session.

Parameters

cartIdstringRequired
the id of the cart to update the payment session for
updateRecord<string, unknown>Required
the data to update the payment session with

Returns

PromisePromise<Cart>Required
the resulting cart

updateShippingAddress_

Updates the cart's shipping address.

Parameters

cartCartRequired
the cart to update
addressOrIdstring | AddressPayload | Partial<Address>Required
the value to set the shipping address to
addrRepoRepository<Address>Required
the repository to use for address updates

Returns

PromisePromise<void>Required
the result of the update operation

updateUnitPrices_

Parameters

cartCartRequired
regionIdstring
customer_idstring

Returns

PromisePromise<void>Required

validateLineItem

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

Parameters

sales_channel_idobjectRequired
the cart for the line item
sales_channel_id.sales_channel_idnull | stringRequired
lineItemLineItemValidateDataRequired
the line item being added

Returns

PromisePromise<boolean>Required
a boolean indicating validation result

validateLineItemShipping_

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

shippingMethodsShippingMethod[]Required
the set of shipping methods to check from
lineItemShippingProfiledIdstringRequired

Returns

booleanboolean
boolean representing whether shipping method is validated

withTransaction

Parameters

transactionManagerEntityManager

Returns

CartServiceobjectRequired
Was this section helpful?