prepare("SELECT id FROM products WHERE id = ? AND status = 1"); $stmt->execute([$product_id]); if (!$stmt->fetch()) { echo json_encode(['success' => false, 'message' => 'Product not found']); exit; } add_to_cart($product_id, $quantity, $variation); echo json_encode([ 'success' => true, 'message' => 'Product added to cart successfully', 'cart_count' => get_cart_count(), 'cart_total' => format_currency(get_cart_total()) ]); break; case 'update': $product_id = intval($_POST['product_id']); $quantity = intval($_POST['quantity']); update_cart($product_id, $quantity); echo json_encode([ 'success' => true, 'message' => 'Cart updated', 'cart_count' => get_cart_count(), 'cart_total' => format_currency(get_cart_total()) ]); break; case 'remove': $product_id = intval($_POST['product_id']); remove_from_cart($product_id); echo json_encode([ 'success' => true, 'message' => 'Item removed from cart', 'cart_count' => get_cart_count(), 'cart_total' => format_currency(get_cart_total()) ]); break; case 'apply_coupon': $code = sanitize($_POST['code']); $cart_total = get_cart_total(); $coupon = validate_coupon($code, $cart_total); if ($coupon) { $_SESSION['coupon'] = $coupon; echo json_encode([ 'success' => true, 'message' => 'Coupon applied successfully!', 'discount' => $coupon['value'] ]); } else { echo json_encode(['success' => false, 'message' => 'Invalid or expired coupon']); } break; default: echo json_encode(['success' => false, 'message' => 'Invalid action']); } function sanitize($input) { return htmlspecialchars(trim($input), ENT_QUOTES, 'UTF-8'); }