The CXACartContext service

Version:

The CXACartContext service handles cart related methods and manages the cart count caching.

Note

The CXACartContext service, introduced in Sitecore XC 10, replaces the CartContext service. Events previously performed by CartContext such as TriggerCartUpdateEvent and SubscribeHandler have been moved to consume CXAObservable instead.

The CXACartContext service performs the following methods. It interacts with CXAObservable and uses various shared events.

  • Method: Description
  • IsCartCountCached: Gets a boolean value indicating whether the cart count should be cached. If it is true, the cart count is stored in the browser sessionStorage.
  • IsCachedCartCountAvailable: Gets a boolean value indicating whether the cart count is available in the browser sessionStorage. If it is true, the cached cart count is retrieved using the GetCachedCartCount method.
  • GetCachedCartCount: Gets the value of the cart count.
  • UpdateCachedCartCount: Updates the cart count in the browser sessionStorage.
  • DeleteCachedCartCount: Removes the cached cart count from the browser sessionStorage.
  • AddCartLine: Calls to AddCartLine API endpoint
  • AddBundleCartLine: Calls to AddBundleCartLine  API endpoint.
  • AddCartLinesWithSublines: Calls to AddCartLinesWithSublines API endpoint.
  • RemoveCartLine: Calls to RemoveShoppingCartLine API endpoint.
  • UpdateCartLineQuantity: Calls to UpdateCartLineQuantity API endpoint.
  • GetCart: Calls to GetCart API endpoint.If there are multiple calls to GetCart method at the same time, the calls are pooled and only one Ajax call is executed to the GetCart API endpoint. The CXACartContext service keeps a copy of the cart object after the cart is first retrieved. The cart object is then returned to the callback functions whenever there is subsequent call to this method. The cached cart object is invalidated every time a cart method is performed, such as applying promotion code.
  • GetMinicart: When recalculateTotals is true or if the context is retrieving the cart or has the cached cart object, it forwards the call to GetCart method. Otherwise it calls to GetMinicart API endpoint.
  • GetCartLinesCount: Calls to GetCartLinesCount API endpoint.
  • RemoveMinicartLine: Calls to RemoveMinicartLine API endpoint.
  • GetPromoCodes: Forwards the call to GetCart method because the backend returns the same cart object and can be shared with other GetCart calls to reduce unnecessary Ajax calls.
  • ApplyPromotionCode: Calls to ApplyDiscount API endpoint.
  • RemovePromotionCode: Calls to RemoveDiscount API endpoint.

CXACartContext emits the event CXAEvent.CartUpdated via CXAObservable when the cart object is received in the response of certain Ajax calls, including:

  • AddCartLine
  • AddBundleCartLine
  • AddCartLinesWithSublines
  • RemoveCartLine
  • UpdateCartLineQuantity
  • GetCart
  • GetMinicart
  • RemoveMinicartLine
  • GetPromoCodes

All the methods above might be calling to different API endpoints, but all the API endpoints return the same CartJsonResult model so the event can be emitted with the cart object as the event argument.

SXA Storefront uses several shared events. The following sections contain details about the cart model and illustrate the call sequence.

Updated cart model

The following class diagram shows the model for the CXAEvent.CartUpdated event, which is found in the cart-minicart-model.js, the cart-freeegiftselections-model.js, as well as the cart-lines-model.js. If the the Free Gift Selection rendering, Minicart rendering, or the Shopping Cart Lines rendering subscribes to the CXAEvent.CartUpdated event, the response returned is Sitecore.Commerce.XA.Feature.Cart.Models.JsonResults.CartJsonResult, which is the updated cart.

Class diagram for the CXAEvent.CartUpdated event

Minicart update call sequence

The following diagram shows the call sequence for Minicart updates.

Sequence diagram showing the Minicart update call sequence
If you have suggestions for improving this article, let us know!