34968-vm/cart_actions.php
Flatlogic Bot ab1ae8b39b V2
2025-10-14 23:38:25 +00:00

56 lines
1.6 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
// Initialize cart if it doesn't exist
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = [];
$_SESSION['cart_restaurant'] = null;
}
$action = $_POST['action'] ?? $_GET['action'] ?? null;
$menu_item_id = $_POST['menu_item_id'] ?? $_GET['menu_item_id'] ?? null;
$restaurant_id = $_POST['restaurant_id'] ?? null;
$quantity = $_POST['quantity'] ?? 1;
switch ($action) {
case 'add':
if ($menu_item_id && $restaurant_id && $quantity > 0) {
// If cart is not empty and new item is from a different restaurant, clear the cart
if (!empty($_SESSION['cart']) && $_SESSION['cart_restaurant'] != $restaurant_id) {
$_SESSION['cart'] = [];
}
$_SESSION['cart_restaurant'] = $restaurant_id;
// Add or update item in cart
if (isset($_SESSION['cart'][$menu_item_id])) {
$_SESSION['cart'][$menu_item_id] += $quantity;
} else {
$_SESSION['cart'][$menu_item_id] = $quantity;
}
}
header('Location: menu.php?id=' . $restaurant_id);
exit;
case 'update':
if ($menu_item_id && $quantity > 0) {
$_SESSION['cart'][$menu_item_id] = $quantity;
}
header('Location: cart.php');
exit;
case 'remove':
if ($menu_item_id) {
unset($_SESSION['cart'][$menu_item_id]);
}
header('Location: cart.php');
exit;
case 'clear':
$_SESSION['cart'] = [];
$_SESSION['cart_restaurant'] = null;
header('Location: cart.php');
exit;
}