1 min read

Cart Service

CartService is found in Poq.Sfcc.Integration.Services namespace. It requires several dependencies to be used in your backend. All required dependencies should be registered using IoC:

services.AddScoped<IProductClient, ProductClient>();
services.AddScoped<ICheckoutClient, CheckoutClient>();
services.AddScoped<ICustomerClient, CustomerClient>();
services.AddScoped<IBasketClient, BasketClient>();
services.AddScoped<ICartService, CartService>();
services.AddScoped<ICartContractMigrator, CartContractMigrator>();

Explanation of services:

  • IProductClient is available as part of Poq nuget package. No further action is required than properly registering the service.

  • ICheckoutClient is available as part of Poq nuget package. No further action is required than properly registering the service.

  • ICustomerClient is available as part of Sfcc Sdk. docs.

  • IBasketClient is available as part of Sfcc Sdk. docs.

  • ICartService with implementation CartService

    Cart Services stores user cart information by using Sfcc Baskets resource. The service relies on CheckoutClient to store permanent cart information for guest users. Service functionality and product items mapping can be modified by providing custom implementation of ICartContractMigrator.

  • ICartContractMigrator with implementation CartContractMigrator is responsible of mapping Sfcc Basket object to PoqCartResponse object. It is also used to create custom SfccProductItem before sending it to OCAPI as Sfcc Basket update.

Here in an example of overriding default handling in ICartContractMigrator.

services.AddScoped<ICartContractMigrator, MyCustomCartContractMigrator>();
public class MyCustomCartContractMigrator : ICartContractMigrator
public async Task<PoqResponse<CartResponse>> GenerateCartResponseAsync<TSfccBasket>(string appIdentifier, string currencyCode, TSfccBasket sfccBasket)
where TSfccBasket : SfccBasket
{
var cartResponse = new CartWithProductsResponse(sfccBasket.BasketId!, total)
{
CartItems = cartItemResponses,
CustomData = new JObject()
};
return new PoqResponse<CartResponse>(new HttpResponseMessage(HttpStatusCode.OK), cartResponse);
}
public SfccProductItem GenerateSfccProductItem(string appIdentifier, AddToCartRequest addToCartRequest)
{
return new SfccProductItem()
{
ProductId = addToCartRequest.VariantId,
Quantity = addToCartRequest.Quantity,
BonusDiscountLineItemId = addToCartRequest.CustomData?.GetValue(BonusDiscountLineItemIdKey)?.Value<string>()
};
}