The CXACartContext service

Current version: 10.3

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.

RequestResponsecoffee
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.

RequestResponsecoffee
CXACartContext. IsCachedCartCountAvailable()

GetCachedCartCount

Gets the value of the cart count.

RequestResponseshell
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.

RequestResponseshell
CXACartContext.UpdateCachedCartCount(cart.Lines.length);

DeleteCachedCartCount

Removes the cached cart count from the browser sessionStorage.

RequestResponseshell
CXACartContext.DeleteCachedCartCount();

AddCartLine

Calls to AddCartLine API endpoint

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

where:

cartLine is the expected object for the AddCartLine API

AddBundleCartLine

Calls to AddBundleCartLine  API endpoint.

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

where:

bundleCartLine is the expected object for the AddBundleCartLine API

AddCartLinesWithSublines

Calls to AddCartLinesWithSublines API endpoint.

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

where:

bundleCartLine is the expected object for the AddCartLinesWithSublines API

RemoveCartLine

Calls to RemoveShoppingCartLine API endpoint.

RequestResponseshell
CXACartContext. RemoveCartLine(lineItemId, productId, productName, productPrice function (data,
success) {});

where:

lineItemId is the existing line item ID in the cart line.

productId is the product's unique identifier. Optional parameter, but required for Commerce reporting in Experience Profile.

productName is the name product. Optional parameter, but required for Commerce reporting in Experience Profile.

productPrice is the price of the product. Optional parameter, but required for Commerce reporting in Experience Profile.

UpdateCartLineQuantity

Calls to UpdateCartLineQuantity API endpoint.

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

where:

lineItemId is the existing line item ID in the cart line.

quantity is the new quantity of the cart line.

productId is the product's unique identifier. Optional parameter, but required to display the updated quantity in Experience Profile reports for Commerce.

variantId is the unique identifier of the product variant. Optional parameter, but required for Commerce reporting in Experience Profile.

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.

RequestResponseshell
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.

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

GetCartLinesCount

Calls to GetCartLinesCount API endpoint.

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

RemoveMinicartLine

Calls to RemoveMinicartLine API endpoint.

RequestResponseshell
CXACartContext.RemoveMinicartLine
(lineItemId, productId, productName, productPrice function (data, success) {});

where:

lineItemId is the existing line item ID in the cart line.

productId is the product unique identifier. Optional parameter, but required for Commerce reporting in Experience Profile.

productName is the name product. Optional parameter, but required for Commerce reporting in Experience Profile.

productPrice is the price of the product. Optional parameter, but required for Commerce reporting in Experience Profile.

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.

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

ApplyPromotionCode

Calls to ApplyDiscount API endpoint.

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

where:

promotionCode is the promotion code to be applied to the cart

RemovePromotionCode

Calls to RemoveDiscount API endpoint.

RequestResponseshell
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.

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.

Do you have some feedback for us?

If you have suggestions for improving this article,