function cartAddToCart( $productID, $variants )
{
$is=GetProductInStockCount( $productID );
$q = db_query( "select min_order_amount from ".PRODUCTS_TABLE.
" where productID=".$productID );
$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
{
$q = db_query("SELECT Quantity FROM ".
SHOPPING_CARTS_TABLE.
" WHERE customerID='".regGetIdByLogin($_SESSION["log"])."' AND itemID='".$itemID."'");
$row = db_fetch_row($q);
$quantity = $row[0];
if (CONF_CHECKSTOCK==0 || $quantity + $count_to_order <= $is)
db_query("UPDATE ".SHOPPING_CARTS_TABLE.
" SET Quantity=".($row[0]+$count_to_order).
" WHERE customerID='".regGetIdByLogin($_SESSION["log"]).
"' AND itemID='".$itemID."'");
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);
db_query("UPDATE ".SHOPPING_CARTS_TABLE.
" SET Quantity=".$count_to_order.
" WHERE customerID='".regGetIdByLogin($_SESSION["log"]).
"' AND itemID='".$itemID."'");
}
else
return false;
}
}
return true;
}