function cartAddToCart($productID, $variants) { $is = GetProductInStockCount($productID); $sql = ' SELECT min_order_amount FROM ' . PRODUCTS_TABLE . ' WHERE productID=' . (int) $productID; $q = db_query($sql); $min_order_amount = db_fetch_row($q); $min_order_amount = $min_order_amount[0]; $count_to_order = 1; if (!isset($_SESSION['log'])) { //save shopping cart in the session variables //$_SESSION['gids'] contains product IDs //$_SESSION['counts'] contains product quantities // ($_SESSION['counts'][$i] corresponds to $_SESSION['gids'][$i]) //$_SESSION['configurations'] contains variants //$_SESSION[gids][$i] == 0 means $i-element is 'empty' if (!isset($_SESSION['gids'])) { $_SESSION['gids'] = array(); $_SESSION['counts'] = array(); $_SESSION['configurations'] = array(); } //check for current item in the current shopping cart content $item_index = SearchConfigurationInSessionVariable($variants, $productID); if ($item_index == -1) { $count_to_order = $min_order_amount; } if ($item_index != -1) { //increase current product's quantity if (CONF_CHECKSTOCK == 0 || $_SESSION['counts'][$item_index] + $count_to_order <= $is) { $_SESSION['counts'][$item_index] += $count_to_order; } else { return false; } } else { if (CONF_CHECKSTOCK == 0 || $is >= $count_to_order) { //no item - add it to $gids array $_SESSION['gids'][] = $productID; $_SESSION['counts'][] = $count_to_order; $_SESSION['configurations'][] = $variants; } else { return false; } } } else { //authorized customer - get cart from database $itemID = SearchConfigurationInDataBase($variants, $productID); if ($itemID != -1) { // if this configuration exists in database $sql = ' SELECT Quantity FROM ' . SHOPPING_CARTS_TABLE . ' WHERE customerID=' . (int) regGetIdByLogin($_SESSION['log']) . ' AND itemID=' . (int) $itemID; $q = db_query($sql); $row = db_fetch_row($q); $quantity = $row['Quantity']; if (CONF_CHECKSTOCK == 0 || $quantity + $count_to_order <= $is) { $sql = ' UPDATE ' . SHOPPING_CARTS_TABLE . ' SET Quantity=' . (int) ($row[0] + $count_to_order) . ' WHERE customerID=' . (int) regGetIdByLogin($_SESSION['log']) . ' AND itemID=' . (int) $itemID; db_query($sql); } else { return false; } } else { //insert new item $count_to_order = $min_order_amount; if (CONF_CHECKSTOCK == 0 || $is >= $count_to_order) { $itemID = InsertNewItem($variants, $productID); InsertItemIntoCart($itemID); $sql = ' UPDATE ' . SHOPPING_CARTS_TABLE . ' SET Quantity=' . (int) $count_to_order . ' WHERE customerID=' . (int) regGetIdByLogin($_SESSION['log']) . ' AND itemID=' . (int) $itemID; db_query($sql); } else { return false; } } } return true; }
$is = GetProductInStockCount($productID); if ($val > 0) { //$val is a new items count in the shopping cart if (CONF_CHECKSTOCK == 1) { $val = min($val, $is); } //check stock level $q = db_query("UPDATE " . SHOPPING_CARTS_TABLE . " SET Quantity=" . floor($val) . " WHERE customerID=" . regGetIdByLogin($_SESSION["log"]) . " AND itemID=" . (int) str_replace("count_", "", $key)); } else { //$val<=0 => delete item from cart $q = db_query("DELETE FROM " . SHOPPING_CARTS_TABLE . " WHERE customerID=" . regGetIdByLogin($_SESSION["log"]) . " AND itemID=" . (int) str_replace("count_", "", $key)); } } else { //session vars $res = DeCodeItemInClient(str_replace("count_", "", $key)); $is = GetProductInStockCount($res["productID"]); if ($val > 0) { $i = SearchConfigurationInSessionVariable($res["variants"], $res["productID"]); //check stock level if (CONF_CHECKSTOCK == 1) { $val = min($val, $is); } $_SESSION["counts"][$i] = floor($val); } else { //remove $i = SearchConfigurationInSessionVariable($res["variants"], $res["productID"]); $_SESSION["gids"][$i] = 0; } } } }