mirror of
https://github.com/vercel/commerce.git
synced 2025-05-15 14:06:59 +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;
|
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,
|
||||||
|
@ -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 || '',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user