0 && $user_id > 0) { $cart_query = $conn->prepare(" SELECT product_id FROM shopping_cart WHERE user_id = ? AND product_id = ? "); $cart_query->bind_param("ii", $user_id, $product_id); $cart_query->execute(); $result = $cart_query->get_result(); $isInCart = $result->num_rows > 0; $cart_query->close(); } // Fetch product details $product = []; if ($product_id > 0) { $stmt = $conn->prepare("SELECT * FROM products WHERE id = ?"); $stmt->bind_param("i", $product_id); $stmt->execute(); $result = $stmt->get_result(); $product = $result->fetch_assoc(); $stmt->close(); } if (empty($product)) { die("Product not found!"); } // Handle Add to Cart if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST['addtocart'])) { $quantity = isset($_POST['quantity']) ? intval($_POST['quantity']) : 1; $product_id = intval($_POST['product_id']); $user_id = intval($_POST['user_id']); // DB connection // 3. Insert into shopping_cart_item $stmt = $conn->prepare("INSERT INTO shopping_cart (user_id, product_id, qty) VALUES (?, ?, ?)"); $stmt->bind_param("iii", $user_id, $product_id, $quantity); if (!$stmt->execute()) { die("Failed to insert shopping_cart_item: " . $stmt->error); } $stmt->close(); $conn->close(); // Redirect back to same page header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $product_id); exit(); } if (isset($_POST['removefromcart'])) { $product_id = intval($_POST['product_id']); $user_id = intval($_POST['user_id']); $sql = " DELETE FROM shopping_cart WHERE product_id = ? "; $stmt = $conn->prepare($sql); $stmt->bind_param("i", $product_id); if ($stmt->execute()) { header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $product_id); } else { echo "Error removing product: " . $stmt->error; } $stmt->close(); } } // Fetch cart items from database if user is logged in // Fetch cart items from database if user is logged in $product_id = isset($_GET['id']) ? intval($_GET['id']) : 0; $user_id = $_SESSION['user_id'] ?? null; $product = []; $cartItems = []; $subtotal = 0; // Fetch product details if product_id is valid if ($product_id > 0) { $stmt = $conn->prepare("SELECT * FROM products WHERE id = ?"); $stmt->bind_param("i", $product_id); $stmt->execute(); $result = $stmt->get_result(); $product = $result->fetch_assoc(); $stmt->close(); } // Fetch cart items from database if user is logged in if ($user_id) { $stmt = $conn->prepare(" SELECT p.id, p.product_name, p.sale_price, p.image_path, sc.qty FROM shopping_cart sc INNER JOIN products p ON sc.product_id = p.id WHERE sc.user_id = ? "); $stmt->bind_param("i", $user_id); $stmt->execute(); $result = $stmt->get_result(); $cartItems = $result->fetch_all(MYSQLI_ASSOC); $stmt->close(); // Calculate subtotal foreach ($cartItems as $item) { $subtotal += $item['sale_price'] * $item['qty']; } } if (empty($product)) { die("Product not found!"); } // Fetch cart items from database if user is logged in // Fetch cart items from database if user is logged in ?>

Summary

  • subtotal: $
  • delivery:
  • discount: -$
  • total: $
mango

0): ?> $ $ 0 ? $product['sale_price'] : $product['price']), 2) ?>

Available: items