Skip to main content

Cart Client

The CartClient is used to interact with the cart API. It is created by the CartFactory's createClient method.

The CartClient extends SubscriberMap, allowing your application to subscribe to events like 'cart' or 'error' to react to state changes.

Constructor

new CartClient(definition, options)

NameTypeDescription
definitionOmit<CartDefinition<S>, 'Provider'>The feature definition, excluding the server-side Provider.
optionsCartClientOptionsConfiguration options for the client, including the API configuration.

Properties

NameTypeDescription
definitionOmit<CartDefinition<S>, 'Provider'>The feature definition passed to the constructor.
promiseDecoratedPromiseA utility promise for managing data fetching states.

Methods

Setup Methods

These methods are used to configure the client after it has been created. These are used for example in a context provider like CartClientProvider from the Commerce integration package. When providing your own context, you can use these.

  • setDataClient(dataClient): Injects the DataClient dependency.
  • setAppClient(appClient): Injects the AppClient dependency.
  • setOptions(options): Updates the client's options.
  • getOptions(): Retrieves the current options.
  • initialize(): A lifecycle method called when the client is registered.

Cart Action Methods

These methods perform actions on the cart by calling the underlying CartAdapter.

  • getCart(key, options): Fetches the current cart. Publishes a 'cart' event with the response on success.
  • addToCart(options): Adds one or more items to the cart.
  • removeFromCart(options): Removes one or more items from the cart.
  • patchEntry(options): Updates an existing entry in the cart (e.g., changes quantity).
  • mergeCarts(cartId): Merges the current guest cart with a customer's existing cart.