const fetchStock = async (productId) => { try { const stockResponse = await axios.get( `${process.env.NEXT_PUBLIC_BASE_API_URL}/api/stock/get/${productId}` ); return stockResponse.data?.quantity; } catch (error) { console.error(`Error fetching stock for product ID ${productId}:`, error); return null; } }; useEffect(() => { const updateCartWithStock = async () => { const storedCart = JSON.parse(localStorage.getItem("myCart")) || []; if (storedCart.length === 0) { router.push("/"); } else { let cartTotal = 0; const updatedCart = await Promise.all( storedCart.map(async (cartItem) => { const stockInfo = await fetchStock(cartItem.productId); return { ...cartItem, stock: stockInfo, }; }) ); // Set the cart state with updatedCart, not storedCart setCart(updatedCart); updatedCart.forEach((item) => { cartTotal += item.price * item.quantity; }); setCartTotal(cartTotal); setOrderTotal(Number(deliveryCharge) + Number(cartTotal)); } }; updateCartWithStock(); }, [cartState]);