Skip to content

Commit

Permalink
fix cart total
Browse files Browse the repository at this point in the history
  • Loading branch information
diboune committed Oct 24, 2024
1 parent fb129c9 commit 7c845b2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 7 additions & 2 deletions storefront/components/global/header/cart/cart-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export function CartProvider({

startTransition(async () => {
setOptimisticCart((prev) => {
console.log({prev: cart?.items?.length});

const items = [...(prev?.items || [])];

const existingItemIndex = items.findIndex(
Expand Down Expand Up @@ -108,7 +110,7 @@ export function CartProvider({

const newTotal = calculateCartTotal(newItems);

return {...prev, items: newItems, total: newTotal} as Cart;
return {...prev, item_total: newTotal, items: newItems} as Cart;
});

await addToCart({
Expand Down Expand Up @@ -211,5 +213,8 @@ export function isOptimisticItemId(id: string) {
}

function calculateCartTotal(cartItems: StoreCartLineItem[]) {
return cartItems.reduce((acc, item) => acc + item.total, 0) || 0;
return (
cartItems.reduce((acc, item) => acc + item.unit_price * item.quantity, 0) ||
0
);
}
8 changes: 4 additions & 4 deletions storefront/components/global/header/cart/cart-footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {useCart} from "./cart-context";
export default function CartFooter() {
const {cart} = useCart();

const total = cart
const item_total = cart
? convertToLocale({
amount: cart.total,
amount: cart.item_total,
currency_code: cart.currency_code,
})
: null;
Expand All @@ -33,9 +33,9 @@ export default function CartFooter() {
Taxes and shipping calculated at checkout
</Body>
</div>
{total && (
{item_total && (
<Body font="sans" mobileSize="base">
{total}
{item_total}
</Body>
)}
</div>
Expand Down

0 comments on commit 7c845b2

Please sign in to comment.