yogiwhere/public/frontend2/update_cart.php

40 lines
1.1 KiB
PHP

<?php
session_start();
// Check if the request method is POST and the necessary data is present
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['id_schedule'], $_POST['quantity'])) {
$idschedule = intval($_POST['id_schedule']);
$quantity = intval($_POST['quantity']);
// Validate that quantity is greater than 0
if ($quantity <= 0) {
echo json_encode([
'status' => 'error',
'message' => 'La quantità deve essere almeno 1.'
]);
exit;
}
// Check if the item exists in the cart
if (isset($_SESSION['cart'][$idschedule])) {
// Update the quantity for the specified item
$_SESSION['cart'][$idschedule]['quantity'] = $quantity;
echo json_encode([
'status' => 'success',
'message' => 'Quantità aggiornata con successo.'
]);
} else {
echo json_encode([
'status' => 'error',
'message' => 'Articolo non trovato nel carrello.'
]);
}
} else {
echo json_encode([
'status' => 'error',
'message' => 'Dati non validi.'
]);
}