The CXACartContext service

Abstract

Overview of the CXACartContext service and the methods it supports.

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

Code

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.

CXACartContext.IsCartCountCached()

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.

CXACartContext. IsCachedCartCountAvailable()

GetCachedCartCount

Gets the value of the cart count.

if (CXACartContext. IsCachedCartCountAvailable()) {

// ‘this’ is in the context of the minicart model

this.updateCartCount(CXACartContext.GetCachedCartCount());
}

UpdateCachedCartCount

Updates the cart count in the browser sessionStorage.

CXACartContext.UpdateCachedCartCount(cart.Lines.length);

DeleteCachedCartCount

Removes the cached cart count from the browser sessionStorage.

CXACartContext.DeleteCachedCartCount();

AddCartLine

Calls to AddCartLine API endpoint

CXACartContext.AddCartLine(cartLine,
function (data, success) {});

where:

cartLine is the expected object for the AddCartLine API

AddBundleCartLine

Calls to AddBundleCartLine  API endpoint.

CXACartContext.
AddBundleCartLine(bundleCartLine, function (data, success) {});

where:

bundleCartLine is the expected object for the AddBundleCartLine API

AddCartLinesWithSublines

Calls to AddCartLinesWithSublines API endpoint.

CXACartContext.AddCartLinesWithSublines(cartLines,
function (data, success) {});

where:

bundleCartLine is the expected object for the AddCartLinesWithSublines API

RemoveCartLine

Calls to RemoveShoppingCartLine API endpoint.

CXACartContext. RemoveCartLine(lineItemId, function (data,
success) {});

where:

lineItemId is the existing line item ID in the cart line

UpdateCartLineQuantity

Calls to UpdateCartLineQuantity API endpoint.

CXACartContext. UpdateCartLineQuantity(lineItemId, quantity,
function (data, success) {});

where:

lineItemId is the existing line item ID in the cart line quantity is the new quantity of the cart line

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.

CXACartContext.GetCart(function (data, success)
{});

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.

CXACartContext.GetMinicart(recalculateTotals,
function (data, success) {});

GetCartLinesCount

Calls to GetCartLinesCount API endpoint.

CXACartContext. GetCartLinesCount(function
(data, success) {});

RemoveMinicartLine

Calls to RemoveMinicartLine API endpoint.

CXACartContext.RemoveMinicartLine
(lineItemId, function (data, success) {});

where:

lineItemId is the existing line item ID in the cart line

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.

CXACartContext.GetPromoCodes(function (data,
success) {});

ApplyPromotionCode

Calls to ApplyDiscount API endpoint.

CXACartContext.ApplyPromotionCode(promotionCode,
function (data, success) {});

where:

promotionCode is the promotion code to be applied to the cart

RemovePromotionCode

Calls to RemoveDiscount API endpoint.

CXACartContext.RemovePromotionCode(promotionCode,
function (data, success) {});

where:

promotionCode is the promotion code to be removed from the cart

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.

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

The following diagram shows the call sequence for minicart updates.

Sequence diagram showing the Minicart update call sequence