diff --git a/src/app/checkout/page.tsx b/src/app/checkout/page.tsx index 27767ad..bd5a48b 100644 --- a/src/app/checkout/page.tsx +++ b/src/app/checkout/page.tsx @@ -22,7 +22,6 @@ const Checkout = () => { let ship = searchParams.get('ship') const { cartState, clearCart } = useCart(); - const [cart, setCart] = useState([]) const [totalCart, setTotalCart] = useState(0) const [activePayment, setActivePayment] = useState('credit-card') const [selectedCountry, setSelectedCountry] = useState('') @@ -42,41 +41,24 @@ const Checkout = () => { note: '' }) - // Fetch cart data from API - useEffect(() => { - getCartData() - }, []) + // ✅ Use cartState.cartArray as the source of truth for BOTH guest and logged-in users + const cart = cartState.cartArray; + console.log("📦 Checkout - Cart from CartContext:", cart); // Calculate total whenever cart changes useEffect(() => { if (cart && cart.length > 0) { - const total = cart.reduce((acc: number, item: any) => acc + (item.price * item.quantity), 0) - setTotalCart(total) + const total = cart.reduce((acc: number, item: any) => { + const price = item.price || 0; + const quantity = item.quantity || item.quantityPurchase || 1; + return acc + (price * quantity); + }, 0); + setTotalCart(total); + } else { + setTotalCart(0); } }, [cart]) - const getCartData = async () => { - const token = localStorage.getItem("token") - if (!token) { - console.error("❌ No token found in localStorage"); - return; - } - - try { - const res = await axios.get(`${BaseURL}/cart`, { - headers: { - Authorization: `Bearer ${token}`, - "Content-Type": "application/json" - }, - withCredentials: true - }) - console.log("Cart response ✅:", res.data); - setCart(res?.data?.cart?.items || []) - } catch (error) { - console.error("❌ Error fetching cart:", error) - } - } - const handleCountryChange = (countryName: string) => { setSelectedCountry(countryName) const country = locationData.countries.find(c => c.name === countryName) @@ -146,7 +128,6 @@ const Checkout = () => { // Clear cart immediately after successful order console.log("💳 Cash on Delivery order successful - clearing cart"); clearCart() - setCart([]) router.push(`/order-success?orderId=${response.data.data._id}&orderNumber=${response.data.data.orderNumber}`) } } @@ -191,7 +172,6 @@ const Checkout = () => { // Clear cart immediately after successful order console.log("💳 Credit Card/Stripe order successful - clearing cart"); clearCart() - setCart([]) router.push(`/order-success?orderId=${orderId}&orderNumber=${response.data.data.orderNumber}`) } } @@ -416,36 +396,46 @@ const Checkout = () => { {cart.length < 1 ? (

No product in cart

) : ( - cart.map((item, index) => ( -
-
- img -
-
-
-
{item.product.name}
-
- {item.size} - / - {item.color} + cart.map((item: any, index: number) => { + // Handle both API format (item.product.name) and CartContext format (item.name) + const itemName = item.product?.name || item.name; + const itemImage = item.product?.thumbImage?.[0] || item.thumbImage?.[0] || item.images?.[0]; + const itemSize = item.size || item.selectedSize || item.sizes?.[0]; + const itemColor = item.color || item.selectedColor || item.variation?.[0]?.color; + const itemQuantity = item.quantity || item.quantityPurchase || 1; + const itemPrice = item.price; + + return ( +
+
+ img +
+
+
+
{itemName}
+
+ {itemSize} + / + {itemColor} +
+
+
+ {itemQuantity} + x + + ${itemPrice}.00 +
-
- {item.quantity} - x - - ${item.price}.00 - -
-
- )) + ); + }) )}