Cart Service

CartService is found in Poq.Shopify.Services namespace. Cart service uses only Shopify Storefront API to store data and can be used for both feedless and feed implementation of BFC. It requires two dependencies to be used in your backend. All required dependencies should be registered using IoC:

services.AddScoped<IShopifyCartClient, ShopifyCartClient>();
services.AddScoped<ICartResponseModifier, CartResponseModifier>();
services.AddScoped<ICartService, CartService>();

Explanation of services

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

  • ICartService with implementation CartService

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

  • ICartResponseModifier with implementation CartResponseModifier is responsible of mapping ShopifyCart object received from Shopify API to CartWithProductsResponse object.

Here in an example of overriding default handling in ICartResponseModifier.

services.AddScoped<ICartResponseModifier, MyCartResponseModifier>();
public class MyCartResponseModifier : ICartResponseModifier
{
public async Task<PoqResponse<CartWithProductsResponse>> CreateCartWithProductsResponse(ShopifyCart shopifyCart, string appIdentifier, string currency)
{
if (shopifyCart == null || shopifyCart?.Lines?.Count <= 0)
{
return new PoqResponse<CartWithProductsResponse>(new HttpResponseMessage(HttpStatusCode.OK),
new CartWithProductsResponse(shopifyCart.Id, new Price(shopifyCart.EstimatedCost.TotalAmount.Amount ?? 0))
{
CustomData = MapAttributesToCustomData(shopifyCart.Attributes)
});
}
var cartProducts = new List<CartItemWithProductResponse>();
foreach (var item in shopifyCart.Lines!)
{
var cartLineItem = item.CartLineItem!;
var shopifyVariant = cartLineItem?.Merchandise;
cartProducts.Add(new CartItemWithProductResponse()
{
Product = new CartProductResponse()
{
},
Id = cartLineItem?.Id ?? string.Empty,
ListingId = GetEntityId(shopifyVariant?.Product?.Id),
ProductId = GetEntityId(shopifyVariant?.Product?.Id),
VariantId = GetEntityId(shopifyVariant?.Id),
VariantName = shopifyVariant?.SelectedOptions?
.FirstOrDefault(x => string.Equals(x.Name, "size", StringComparison.OrdinalIgnoreCase))?.Value ?? "One Size",
Brand = shopifyVariant?.Product.Vendor,
Color = shopifyVariant?.SelectedOptions?
.FirstOrDefault(x => string.Equals(x.Name, "colour", StringComparison.OrdinalIgnoreCase) || string.Equals(x.Name, "color", StringComparison.OrdinalIgnoreCase))?.Value,
IsInStock = shopifyVariant?.AvailableForSale ?? false,
ThumbnailUrl = shopifyVariant?.Image?.Url ?? string.Empty,
Quantity = cartLineItem?.Quantity ?? 0,
Title = shopifyVariant?.Product?.Title ?? string.Empty,
CustomData = MapAttributesToCustomData(cartLineItem?.Attributes),
Price = new Price(shopifyVariant?.Price.Amount ?? 0),
Total = new Price((shopifyVariant?.Price.Amount ?? 0) * (cartLineItem?.Quantity ?? 0))
});
}
return new PoqResponse<CartWithProductsResponse>(new HttpResponseMessage(HttpStatusCode.OK),
new CartWithProductsResponse(shopifyCart.Id, new Price(shopifyCart.EstimatedCost.TotalAmount.Amount ?? 0))
{
CartItems = cartProducts,
CustomData = MapAttributesToCustomData(shopifyCart.Attributes)
});
}
...
}

Methods

  • GetCartAsync Get cart data by using underlying Shopify integration.

  • UpdateCartAsync Updates underlying cart.

  • AddToCartAsync Adds item to underlying cart.

All methods use Poq Contract classes for their interface and can handle incoming request data. Be cautious to error messages returned. Similarly to Shopify storefront graphql API Poq Shopify SDK returns successful status codes with error messages inside.