prepare(" SELECT o.*, u.username, u.first_name, u.last_name, u.email FROM orders o JOIN users u ON o.customer_id = u.user_id WHERE o.order_id = ? "); $stmt->execute([$order_id]); $order = $stmt->fetch(); // Verify order belongs to current user if (!$order || $order['customer_id'] != $_SESSION['user_id']) { header("Location: /unimarket/customer/orders/list.php"); exit(); } // Handle pickup confirmation and cancellation if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST['confirm_pickup'])) { try { $stmt = $pdo->prepare("UPDATE orders SET status = 'completed' WHERE order_id = ? AND customer_id = ? AND status = 'ready'"); $stmt->execute([$order_id, $_SESSION['user_id']]); // Refresh order data $stmt = $pdo->prepare(" SELECT o.order_id, o.order_date, o.total_amount, o.status, o.pickup_schedule FROM orders o WHERE o.order_id = ? "); $stmt->execute([$order_id]); $order = $stmt->fetch(); $success_message = 'Order pickup confirmed! Thank you for your purchase.'; } catch (PDOException $e) { $error_message = 'Failed to confirm pickup. Please try again.'; } } elseif (isset($_POST['cancel_order'])) { try { // Only allow cancellation if order is not completed or cancelled if ($order['status'] !== 'completed' && $order['status'] !== 'cancelled') { $stmt = $pdo->prepare("UPDATE orders SET status = 'cancelled' WHERE order_id = ? AND customer_id = ?"); $stmt->execute([$order_id, $_SESSION['user_id']]); // Refresh order data $stmt = $pdo->prepare(" SELECT o.order_id, o.order_date, o.total_amount, o.status, o.pickup_schedule FROM orders o WHERE o.order_id = ? "); $stmt->execute([$order_id]); $order = $stmt->fetch(); $success_message = 'Order has been cancelled successfully.'; } else { $error_message = 'This order cannot be cancelled.'; } } catch (PDOException $e) { $error_message = 'Failed to cancel order. Please try again.'; } } elseif (isset($_POST['delete_order'])) { try { // Start transaction $pdo->beginTransaction(); // Update order status to deleted $stmt = $pdo->prepare("UPDATE orders SET status = 'deleted' WHERE order_id = ? AND customer_id = ?"); $stmt->execute([$order_id, $_SESSION['user_id']]); // Log the deletion error_log("Deleting order ID: " . $order_id . " for user ID: " . $_SESSION['user_id']); // Commit transaction $pdo->commit(); // Redirect to orders list header("Location: /unimarket/customer/orders/list.php"); exit(); } catch (PDOException $e) { // Rollback transaction on error $pdo->rollBack(); error_log("Error deleting order: " . $e->getMessage()); $error_message = 'Failed to delete order. Please try again.'; } } } // Get order items with seller information $stmt = $pdo->prepare(" SELECT oi.*, p.name, p.image_path, p.price, u.username as seller_username, u.first_name as seller_first_name, u.last_name as seller_last_name FROM order_items oi JOIN products p ON oi.product_id = p.product_id JOIN users u ON p.owner_id = u.user_id WHERE oi.order_id = ? "); $stmt->execute([$order_id]); $order_items = $stmt->fetchAll(); // Calculate total items $total_items = array_sum(array_column($order_items, 'quantity')); ?>
| Product | Seller | Price | Quantity | Subtotal |
|---|---|---|---|---|
|
|
@
|
₱ | ₱ |