feat(poc): improve totalQuantity

This commit is contained in:
Björn Meyer 2023-07-24 12:19:27 +02:00
parent 66343b5e73
commit 2df48267a9
2 changed files with 11 additions and 2 deletions

View File

@ -41,7 +41,7 @@ export const addItem = async (variantId: string | undefined): Promise<Error | un
quantity = itemInCart.quantity + 1; quantity = itemInCart.quantity + 1;
} }
apiClient.invoke('addLineItem post /checkout/cart/line-item', { await apiClient.invoke('addLineItem post /checkout/cart/line-item', {
items: [ items: [
{ {
id: variantId, id: variantId,

View File

@ -354,10 +354,19 @@ export function transformCart(resCart: ExtendedCart): Cart {
id: resCart.token ?? '', id: resCart.token ?? '',
lines: lines:
resCart.lineItems?.map((lineItem: ExtendedLineItem) => transformLineItem(lineItem)) || [], resCart.lineItems?.map((lineItem: ExtendedLineItem) => transformLineItem(lineItem)) || [],
totalQuantity: resCart.lineItems?.length || 0 totalQuantity: resCart.lineItems ? calculateTotalCartQuantity(resCart.lineItems) : 0
}; };
} }
function calculateTotalCartQuantity(lineItems: ExtendedLineItem[]) {
let totalQuantity = 0;
lineItems.forEach((lineItem) => {
totalQuantity += lineItem.quantity ?? 0;
});
return totalQuantity;
}
function transformLineItem(resLineItem: ExtendedLineItem): CartItem { function transformLineItem(resLineItem: ExtendedLineItem): CartItem {
return { return {
id: resLineItem.id || '', id: resLineItem.id || '',