Я хочу посчитать мои продукты $ _SESSION ['cart'] (единицы) из моей корзины.
#check added stock
$units = (int) $_POST['units'];
$sizes_id = (int) $_POST['size_id'];
$in_stock = $db->GetScalar("SELECT p.units FROM products_sizes s
INNER JOIN products p ON p.product_id = s.product_id
WHERE s.id = '$sizes_id' LIMIT 1");
$total = isset($_SESSION['cart'][$sizes_id]) ? $_SESSION['cart'][$sizes_id]['units'] : 0;
$total += $units;
if($total <= $in_stock) {
if(isset($_SESSION['cart'][$sizes_id])) {
$_SESSION['cart'][$sizes_id]['units'] = $total;
} else {
$_SESSION['cart'][$sizes_id] = array('size' => $sizes_id, 'units' => $total);
}
$msg = 'Added';
$status = 1;
}else {
$msg = 'Product is not in the basket';
$status = 0;
}
Вот так выглядит вывод
Array(
[57] => Array
(
[size] => 57
[units] => 5
)
[56] => Array
(
[size] => 56
[units] => 1
)
)