Can anyone point out why this is not incrementing items already in the cart? It simply adds new rows to the cart.
session_start();
if(isset($_SESSION['CART']) == false || isset($_SESSION['CART']['ITEMS']) == false) {
$_SESSION['CART']['ITEMS'] = array();
}
foreach($_POST AS $sku => $qty) {
if (!empty($qty)) {
// if the product sku is already in the cart, do this //
foreach ($_SESSION['CART']['ITEMS'] as $cart_item_sku => $item) {
if (($item['item_sku'] == $sku) && ($qty == '1')) {
$_SESSION['CART']['ITEMS'][$cart_item_sku]['item_qty']++;
$found = true;
break;
}
elseif (($item['item_sku'] == $sku) && ($qty > '1')) {
$_SESSION['CART']['ITEMS'][$cart_item_sku]['item_qty'] = $_SESSION['CART']['ITEMS'][$cart_item_sku]['item_qty'] + $qty;
$found = true;
break;
}
}
// if the product sku is not already in the cart, do this //
if (!$found) {
$_SESSION['CART']['ITEMS'][] = array('item_sku' => $sku, 'item_qty' => $qty);
}
}