Cart Service

CartService is found in Poq.Shopify.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<ICartResponseModifier, CartResponseModifier>();
services.AddScoped<ICartService, CartService>();

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.

  • ICartService with implementation CartService

    Cart Services stores user cart information by using Poq Cart Api. Service functionality and product items mapping can be modified by providing custom implementation of ICartResponseModifier.

  • ICartResponseModifier with implementation CartResponseModifier is responsible of mapping CartResponse object to CartWithProductsResponse object.

Here in an example of overriding default handling in ICartResponseModifier.

services.AddScoped<ICartResponseModifier, MyCartResponseModifier>();
public class MyCartResponseModifier : ICartResponseModifier
{
public CartWithProductsResponse CreateCartWithProductsResponse(CartResponse cartResponse, IList<Product> products, string currencyIdentifier)
{
var result = new CartWithProductsResponse(cartResponse.CartId, new Price())
{
CartItems = new List<CartItemWithProductResponse>(),
CustomData = cartResponse.CustomData,
};
foreach (var cartItem in cartResponse.CartItems)
{
var product = products.FirstOrDefault(p => p.Variants.Values.Any(v => v.Id == cartItem.VariantId));
if (product == null)
{
continue;
}
var variant = product.Variants.Values.FirstOrDefault(v => v.Id == cartItem.VariantId);
var cartItemResponse = CreateCartItemResponse(cartItem, product, variant, currencyIdentifier);
// Custom mapping of any type
result.CartItems.Add(cartItemResponse);
}
return result;
}
public CartItemWithProductResponse CreateCartItemResponse(CartItemResponse cartItem, Product product, ProductVariant variant, string currencyIdentifier)
{
var cartItemResponse = new CartItemWithProductResponse
{
// Any Custom mapping required
};
return cartItemResponse;
}
}