---
displayed_sidebar: servicesSidebar
---
import ParameterTypes from "@site/src/components/ParameterTypes"
# CartService
## Constructors
### constructor
`**new CartService**(«destructured»)`
#### Parameters
## Properties
`",
"description": "",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "__container__",
"type": "`any`",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "__moduleDeclaration__",
"type": "`Record`",
"description": "",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "addressRepository_",
"type": "Repository<[Address](Address.mdx)>",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "cartRepository_",
"type": "Repository<[Cart](Cart.mdx)> & `{ findOneWithRelations: Method findOneWithRelations ; findWithRelations: Method findWithRelations }`",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "customShippingOptionService_",
"type": "[CustomShippingOptionService](CustomShippingOptionService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "customerService_",
"type": "[CustomerService](CustomerService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "discountService_",
"type": "[DiscountService](DiscountService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "eventBus_",
"type": "[EventBusService](EventBusService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "featureFlagRouter_",
"type": "[FlagRouter](FlagRouter.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "giftCardService_",
"type": "[GiftCardService](GiftCardService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "lineItemAdjustmentService_",
"type": "[LineItemAdjustmentService](LineItemAdjustmentService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "lineItemRepository_",
"type": "Repository<[LineItem](LineItem.mdx)> & `{ findByReturn: Method findByReturn }`",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "lineItemService_",
"type": "[LineItemService](LineItemService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "manager_",
"type": "EntityManager",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "newTotalsService_",
"type": "[NewTotalsService](NewTotalsService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "paymentProviderService_",
"type": "[PaymentProviderService](PaymentProviderService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "paymentSessionRepository_",
"type": "Repository<[PaymentSession](PaymentSession.mdx)>",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "priceSelectionStrategy_",
"type": "[IPriceSelectionStrategy](../interfaces/IPriceSelectionStrategy.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "pricingService_",
"type": "[PricingService](PricingService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "productService_",
"type": "[ProductService](ProductService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "productVariantInventoryService_",
"type": "[ProductVariantInventoryService](ProductVariantInventoryService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "productVariantService_",
"type": "[ProductVariantService](ProductVariantService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "regionService_",
"type": "[RegionService](RegionService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "salesChannelService_",
"type": "[SalesChannelService](SalesChannelService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "shippingMethodRepository_",
"type": "Repository<[ShippingMethod](ShippingMethod.mdx)>",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "shippingOptionService_",
"type": "[ShippingOptionService](ShippingOptionService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "shippingProfileService_",
"type": "[ShippingProfileService](ShippingProfileService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "storeService_",
"type": "[StoreService](StoreService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "taxProviderService_",
"type": "[TaxProviderService](TaxProviderService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "totalsService_",
"type": "[TotalsService](TotalsService.mdx)",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "transactionManager_",
"type": "`undefined` \\| EntityManager",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "Events",
"type": "`object`",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "Events.CREATED",
"type": "`string`",
"description": "",
"optional": false,
"defaultValue": "\"cart.created\"",
"expandable": false,
"children": []
},
{
"name": "Events.CUSTOMER_UPDATED",
"type": "`string`",
"description": "",
"optional": false,
"defaultValue": "\"cart.customer_updated\"",
"expandable": false,
"children": []
},
{
"name": "Events.UPDATED",
"type": "`string`",
"description": "",
"optional": false,
"defaultValue": "\"cart.updated\"",
"expandable": false,
"children": []
}
]} />
## Accessors
### activeManager\_
`Protected get**activeManager_**(): EntityManager`
#### Returns
EntityManager
## Methods
### addLineItem
`**addLineItem**(cartId, lineItem, config?): Promise<void>`
Adds a line item to the cart.
#### Parameters
#### Returns
Promise<void>
#### Deprecated
Use [addOrUpdateLineItems](CartService.mdx#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
#### Returns
Promise<void>
___
### addShippingMethod
`**addShippingMethod**(cartOrId, optionId, data?): Promise<[Cart](Cart.mdx)>`
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
`",
"description": "the fulmillment data for the method",
"optional": false,
"defaultValue": "{}",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise<[Cart](Cart.mdx)>
___
### 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
#### Returns
Promise<void>
___
### 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
#### Returns
Promise<void>
___
### 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
#### Returns
Promise<void>
___
### applyGiftCard\_
`Protected **applyGiftCard_**(cart, code): Promise<void>`
#### Parameters
#### Returns
Promise<void>
___
### atomicPhase\_
`Protected **atomicPhase_**(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.
#### Parameters
Promise<TResult>",
"description": "the transactional work to be done",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "isolationOrErrorHandler",
"type": "`IsolationLevel` \\| (`error`: `TError`) => Promise<void \\| TResult>",
"description": "the isolation level to be used for the work.",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
},
{
"name": "maybeErrorHandlerOrDontFail",
"type": "(`error`: `TError`) => Promise<void \\| TResult>",
"description": "Potential error handler",
"optional": true,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise<TResult>
___
### authorizePayment
`**authorizePayment**(cartId, context?): Promise<[Cart](Cart.mdx)>`
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
` & `{ cart_id: string }`",
"description": "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.",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise<[Cart](Cart.mdx)>
___
### create
`**create**(data): Promise<[Cart](Cart.mdx)>`
Creates a cart.
#### Parameters
#### Returns
Promise<[Cart](Cart.mdx)>
___
### createOrFetchGuestCustomerFromEmail\_
`Protected **createOrFetchGuestCustomerFromEmail_**(email): Promise<[Customer](Customer.mdx)>`
Creates or fetches a user based on an email.
#### Parameters
#### Returns
Promise<[Customer](Customer.mdx)>
___
### createTaxLines
`**createTaxLines**(cartOrId): Promise<void>`
#### Parameters
#### Returns
Promise<void>
___
### decorateTotals
`**decorateTotals**(cart, totalsConfig?): Promise<[WithRequiredProperty](../types/WithRequiredProperty.mdx)<[Cart](Cart.mdx), `"total"`>>`
#### Parameters
#### Returns
Promise<[WithRequiredProperty](../types/WithRequiredProperty.mdx)<[Cart](Cart.mdx), `"total"`>>
___
### decorateTotals\_
`Protected **decorateTotals_**(cart, totalsToSelect, options?): Promise<[Cart](Cart.mdx)>`
#### Parameters
#### Returns
Promise<[Cart](Cart.mdx)>
#### Deprecated
Use decorateTotals instead
___
### delete
`**delete**(cartId): Promise<[Cart](Cart.mdx)>`
Deletes a cart from the database. Completed carts cannot be deleted.
#### Parameters
#### Returns
Promise<[Cart](Cart.mdx)>
___
### deletePaymentSession
`**deletePaymentSession**(cartId, providerId): Promise<void>`
Removes a payment session from the cart.
#### Parameters
#### Returns
Promise<void>
___
### deleteTaxLines
`**deleteTaxLines**(id): Promise<void>`
#### Parameters
#### Returns
Promise<void>
___
### findCustomShippingOption
`**findCustomShippingOption**(cartCustomShippingOptions, optionId): undefined \| [CustomShippingOption](CustomShippingOption.mdx)`
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
#### Returns
`undefined` \| [CustomShippingOption](CustomShippingOption.mdx)
___
### getTotalsRelations
`Private **getTotalsRelations**(config): string[]`
#### Parameters
#### Returns
`string`[]
___
### getValidatedSalesChannel
`Protected **getValidatedSalesChannel**(salesChannelId?): Promise<[SalesChannel](SalesChannel.mdx)>`
#### Parameters
#### Returns
Promise<[SalesChannel](SalesChannel.mdx)>
___
### list
`**list**(selector, config?): Promise<[Cart](Cart.mdx)[]>`
#### Parameters
#### Returns
Promise<[Cart](Cart.mdx)[]>
___
### onSalesChannelChange
`Protected **onSalesChannelChange**(cart, newSalesChannelId): Promise<void>`
Remove the cart line item that does not belongs to the newly assigned sales channel
#### Parameters
#### Returns
Promise<void>
___
### refreshAdjustments\_
`Protected **refreshAdjustments_**(cart): Promise<void>`
#### Parameters
#### Returns
Promise<void>
___
### refreshPaymentSession
`**refreshPaymentSession**(cartId, providerId): Promise<void>`
Refreshes a payment session on a cart
#### Parameters
#### Returns
Promise<void>
___
### removeDiscount
`**removeDiscount**(cartId, discountCode): Promise<[Cart](Cart.mdx)>`
Removes a discount based on a discount code.
#### Parameters
#### Returns
Promise<[Cart](Cart.mdx)>
___
### removeLineItem
`**removeLineItem**(cartId, lineItemId): Promise<[Cart](Cart.mdx)>`
Removes a line item from the cart.
#### Parameters
#### Returns
Promise<[Cart](Cart.mdx)>
___
### retrieve
`**retrieve**(cartId, options?, totalsConfig?): Promise<[Cart](Cart.mdx)>`
Gets a cart by id.
#### Parameters
#### Returns
Promise<[Cart](Cart.mdx)>
___
### retrieveLegacy
`Protected **retrieveLegacy**(cartId, options?, totalsConfig?): Promise<[Cart](Cart.mdx)>`
#### Parameters
#### Returns
Promise<[Cart](Cart.mdx)>
#### Deprecated
___
### retrieveWithTotals
`**retrieveWithTotals**(cartId, options?, totalsConfig?): Promise<[WithRequiredProperty](../types/WithRequiredProperty.mdx)<[Cart](Cart.mdx), `"total"`>>`
#### Parameters
#### Returns
Promise<[WithRequiredProperty](../types/WithRequiredProperty.mdx)<[Cart](Cart.mdx), `"total"`>>
___
### setMetadata
`**setMetadata**(cartId, key, value): Promise<[Cart](Cart.mdx)>`
Dedicated method to set metadata for a cart.
To ensure that plugins does not overwrite each
others metadata fields, setMetadata is provided.
#### Parameters
#### Returns
Promise<[Cart](Cart.mdx)>
___
### setPaymentSession
`**setPaymentSession**(cartId, providerId): Promise<void>`
Selects a payment session for a cart and creates a payment object in the external provider system
#### Parameters
#### Returns
Promise<void>
___
### 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
#### Returns
Promise<void>
___
### setRegion\_
`Protected **setRegion_**(cart, regionId, countryCode): Promise<void>`
Set's the region of a cart.
#### Parameters
#### Returns
Promise<void>
___
### shouldRetryTransaction\_
`Protected **shouldRetryTransaction_**(err): boolean`
#### Parameters
` \\| `{ code: string }`",
"description": "",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
`boolean`
___
### transformQueryForTotals\_
`Protected **transformQueryForTotals_**(config): [FindConfig](../interfaces/FindConfig.mdx)<[Cart](Cart.mdx)> & { totalsToSelect: [TotalField](../types/TotalField.mdx)[] }`
#### Parameters
#### Returns
[FindConfig](../interfaces/FindConfig.mdx)<[Cart](Cart.mdx)> & `{ totalsToSelect: [TotalField](../types/TotalField.mdx)[] }`
___
### update
`**update**(cartOrId, data): Promise<[Cart](Cart.mdx)>`
#### Parameters
#### Returns
Promise<[Cart](Cart.mdx)>
___
### updateBillingAddress\_
`Protected **updateBillingAddress_**(cart, addressOrId, addrRepo): Promise<void>`
Updates the cart's billing address.
#### Parameters
#### Returns
Promise<void>
___
### updateCustomerId\_
`Protected **updateCustomerId_**(cart, customerId): Promise<void>`
Sets the customer id of a cart
#### Parameters
#### Returns
Promise<void>
___
### updateLineItem
`**updateLineItem**(cartId, lineItemId, update): Promise<[Cart](Cart.mdx)>`
Updates a cart's existing line item.
#### Parameters
#### Returns
Promise<[Cart](Cart.mdx)>
___
### updatePaymentSession
`**updatePaymentSession**(cartId, update): Promise<[Cart](Cart.mdx)>`
Updates the currently selected payment session.
#### Parameters
`",
"description": "the data to update the payment session with",
"optional": false,
"defaultValue": "",
"expandable": false,
"children": []
}
]} />
#### Returns
Promise<[Cart](Cart.mdx)>
___
### updateShippingAddress\_
`Protected **updateShippingAddress_**(cart, addressOrId, addrRepo): Promise<void>`
Updates the cart's shipping address.
#### Parameters
#### Returns
Promise<void>
___
### updateUnitPrices\_
`Protected **updateUnitPrices_**(cart, regionId?, customer_id?): Promise<void>`
#### Parameters
#### Returns
Promise<void>
___
### validateLineItem
`Protected **validateLineItem**(sales_channel_id, lineItem): Promise<boolean>`
Check if line item's variant belongs to the cart's sales channel.
#### Parameters
#### Returns
Promise<boolean>
___
### 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
#### Returns
`boolean`
___
### withTransaction
`**withTransaction**(transactionManager?): [CartService](CartService.mdx)`
#### Parameters
#### Returns
[CartService](CartService.mdx)