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:
IProductClientis available as part of Poq nuget package. No further action is required than properly registering the service.ICheckoutClientis available as part of Poq nuget package. No further action is required than properly registering the service.ICustomerClientis available as part of Sfcc Sdk. docs.IBasketClientis available as part of Sfcc Sdk. docs.ICartServicewith implementationCartServiceCart 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.ICartContractMigratorwith implementationCartContractMigratoris responsible of mapping Sfcc Basket object toPoqCartResponseobject. It is also used to create customSfccProductItembefore 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>() }; }