mirror of
https://github.com/vercel/commerce.git
synced 2025-05-14 21:47:51 +00:00
feat(poc): improve totalQuantity
This commit is contained in:
parent
66343b5e73
commit
2df48267a9
@ -41,7 +41,7 @@ export const addItem = async (variantId: string | undefined): Promise<Error | un
|
||||
quantity = itemInCart.quantity + 1;
|
||||
}
|
||||
|
||||
apiClient.invoke('addLineItem post /checkout/cart/line-item', {
|
||||
await apiClient.invoke('addLineItem post /checkout/cart/line-item', {
|
||||
items: [
|
||||
{
|
||||
id: variantId,
|
||||
|
@ -354,10 +354,19 @@ export function transformCart(resCart: ExtendedCart): Cart {
|
||||
id: resCart.token ?? '',
|
||||
lines:
|
||||
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 {
|
||||
return {
|
||||
id: resLineItem.id || '',
|
||||
|
Loading…
x
Reference in New Issue
Block a user