$sc_errors = "";
$item_added = false;
$sc_item_id = "";
$sc_category_id = "";
$cart = get_param("cart");
if($cart)
{
$placed_ids = get_session("placed_ids");
if (!is_array($placed_ids)) {
$placed_ids = array();
}
$random_id = get_param("rnd");
//-- checking if such page has been already called
if (!isset($placed_ids[$random_id]))
{
$placed_ids[$random_id] = $random_id;
$shopping_cart = get_session("shopping_cart");
switch(strtoupper($cart))
{
case "ADD": // add item to the cart
if (!is_array($shopping_cart)) {
$shopping_cart = array();
}
$item_id = get_param("item_id");
$accessory_id = get_param("accessory_id");
$sc_item_id = $accessory_id ? $accessory_id : $item_id;
$sc_category_id = get_param("ic_id");;
$sql = " SELECT item_name,price,is_sales,sales_price,stock_level,use_stock_level,hide_out_of_stock ";
$sql .= " FROM " . $table_prefix . "items ";
$sql .= " WHERE item_id=" . $db->tosql($sc_item_id, INTEGER);
$sql .= " AND is_showing=1";
$sql .= " AND ((hide_out_of_stock=1 AND stock_level > 0) OR hide_out_of_stock=0)";
$db->query($sql);
if($db->next_record()) {
$item_name = $db->f("item_name");
$stock_level = $db->f("stock_level");
$use_stock_level = $db->f("use_stock_level");
$hide_out_of_stock = $db->f("hide_out_of_stock");
$price = calculate_price($db->f("price"), $db->f("is_sales"), $db->f("sales_price"));
} else {
// item doesn't exists or unavailable
return;
}
$properties = "";
$properties_types = array();
$sql = " SELECT property_id, property_name, control_type, required ";
$sql .= " FROM " . $table_prefix . "items_properties WHERE item_id=" . $sc_item_id;
if ($type == "list") {
$sql .= " AND use_on_list=1 ";
} else {
$sql .= " AND use_on_details=1 ";
}
$sql .= " ORDER BY property_order, property_id ";
$db->query($sql);
while($db->next_record())
{
$property_id = $db->f("property_id");
$property_name = $db->f("property_name");
$property_type = $db->f("control_type");
$property_required = $db->f("required");
$property_values = array();
if ($property_type == "CHECKBOXLIST") {
$property_total = get_param("property_total_" . $property_id);
for ($i = 1; $i <= $property_total; $i++) {
$property_value = get_param("property_" . $property_id . "_" . $i);
if ($property_value) { $property_values[] = $property_value; }
}
} else {
$property_value = get_param("property_" . $property_id);
if ($property_value) { $property_values[] = $property_value; }
}
if(sizeof($property_values) > 0) {
$properties[$property_id] = $property_values;
$properties_types[$property_id] = $property_type;
} else if ($property_required) {
$property_error = str_replace("{property_name}", get_translation($property_name), REQUIRED_PROPERTY_MSG);
$property_error = str_replace("{product_name}", get_translation($item_name), $property_error);
$sc_errors .= $property_error . "
";
}
}
if($sc_errors) {
// error occurred can't continue process
return;
}
// check if such item already in our cart than increase quantity by one
$in_cart = false;
reset($shopping_cart);
while(list($cart_id, $item) = each($shopping_cart))
{
if($item["ITEM_ID"] == $sc_item_id)
{
$item_properties = $item["PROPERTIES"];
if(!is_array($item_properties) && !is_array($properties))
{
$in_cart = true;
break;
}
else if(is_array($item_properties) && is_array($properties) && sizeof($item_properties) == sizeof($properties))
{
$identical_properties = true;
reset($properties);
while(list($property_id, $property_values) = each($properties))
{
$new_values = implode(",", $property_values);
if(isset($item_properties[$property_id])) {
$exists_values = implode(",", $item_properties[$property_id]);
} else {
$exists_values = "";
}
if ($exists_values != $new_values) {
$identical_properties = false;
break;
}
}
if($identical_properties)
{
$in_cart = true;
break;
}
}
}
}
if($in_cart && $settings["change_quantity"])
{
if($hide_out_of_stock)
{
if(!$use_stock_level || $stock_level > $shopping_cart[$cart_id]["QUANTITY"]) {
$shopping_cart[$cart_id]["QUANTITY"]++;
$item_added = true;
} else {
$property_error = str_replace("{limit_quantity}", $stock_level, PRODUCT_LIMIT_MSG);
$property_error = str_replace("{product_name}", get_translation($item_name), $property_error);
$sc_errors .= $property_error . "
";
}
}
else
{
$shopping_cart[$cart_id]["QUANTITY"]++;
$item_added = true;
}
}
else
{
//-- prepare item for adding to cart
//-- check for additional price for product
if(is_array($properties)) {
foreach($properties as $property_id => $property_values) {
if(strtoupper($properties_types[$property_id]) == "LISTBOX"
|| strtoupper($properties_types[$property_id]) == "RADIOBUTTON"
|| strtoupper($properties_types[$property_id]) == "CHECKBOXLIST") {
for($pv = 0; $pv < sizeof($property_values); $pv++) {
$sql = " SELECT additional_price, additional_weight ";
$sql .= " FROM " . $table_prefix . "items_properties_values ipv ";
$sql .= " WHERE property_id=" . $db->tosql($property_id, INTEGER);
$sql .= " AND item_property_id=" . $db->tosql($property_values[$pv], INTEGER);
$sql .= " ORDER BY item_property_id ";
$db->query($sql);
if($db->next_record()) {
$additional_price = $db->f("additional_price");
$price += $additional_price;
}
}
}
}
}
$item = array (
"ITEM_ID" => $sc_item_id,
"ITEM_NAME" => $item_name,
"PROPERTIES" => $properties,
"QUANTITY" => 1, // only one item can be placed
"PRICE" => $price
);
//-- add to cart
$shopping_cart[] = $item;
$item_added = true;
}
set_session("shopping_cart", $shopping_cart);
if($item_added && isset($settings["redirect_to_cart"]) && $settings["redirect_to_cart"]) {
$rp = get_param("rp");
$cart_page = strlen($rp) ? "basket.php?rp=" . urlencode($rp) : "basket.php";
header("Location: " . $cart_page);
exit;
}
break;
case "RM": //-- remove the item from the cart
if (is_array($shopping_cart))
{
$cart_id = get_param("cart_id");
unset($shopping_cart[$cart_id]);
if(sizeof($shopping_cart) == 0) {
unset($shopping_cart);
set_session("shopping_cart", "");
set_session("session_coupons", "");
}
else {
set_session("shopping_cart", $shopping_cart);
}
}
break;
case "QTY": // update item quantity in the cart
if (is_array($shopping_cart) && $settings["change_quantity"])
{
$cart_id = get_param("cart_id");
$new_quantity = get_param("new_quantity");
$new_quantity = abs($new_quantity);
if(isset($shopping_cart[$cart_id]))
{
$item_id = $shopping_cart[$cart_id]["ITEM_ID"];
$sql = " SELECT item_name, stock_level, use_stock_level, hide_out_of_stock FROM " . $table_prefix . "items ";
$sql .= " WHERE item_id=" . $db->tosql($item_id, INTEGER);
$db->query($sql);
if($db->next_record()) {
$item_name = $db->f("item_name");
$stock_level = $db->f("stock_level");
$use_stock_level = $db->f("use_stock_level");
$hide_out_of_stock = $db->f("hide_out_of_stock");
} else {
// item doesn't exists or unavailable
return;
}
if($hide_out_of_stock)
{
if(!$use_stock_level || $stock_level > $new_quantity)
$shopping_cart[$cart_id]["QUANTITY"] = $new_quantity;
else
$shopping_cart[$cart_id]["QUANTITY"] = $stock_level;
}
else
{
$shopping_cart[$cart_id]["QUANTITY"] = $new_quantity;
}
set_session("shopping_cart", $shopping_cart);
}
}
break;
case "CLR": //-- remove all items from the cart
if (is_array($shopping_cart)) {
set_session("shopping_cart", "");
set_session("session_coupons", "");
}
break;
}
set_session("placed_ids", $placed_ids);
}
}
function calculate_price($price, $is_sales, $sales_price)
{
if($is_sales && $sales_price)
{
$price = $sales_price;
}
return $price;
}
function remove_coupon($coupon_id)
{
global $shopping_cart, $coupons;
if (is_array($coupons) && isset($coupons[$coupon_id])) {
unset($coupons[$coupon_id]);
if (sizeof($coupons) == 0) {
set_session("session_coupons", "");
} else {
set_session("session_coupons", $coupons);
}
}
foreach ($shopping_cart as $cart_id => $item) {
if (isset($shopping_cart[$cart_id]["COUPONS"]) && isset($shopping_cart[$cart_id]["COUPONS"][$coupon_id])) {
unset($shopping_cart[$cart_id]["COUPONS"][$coupon_id]);
if (sizeof($shopping_cart[$cart_id]["COUPONS"]) == 0) {
unset($shopping_cart[$cart_id]["COUPONS"]);
}
}
}
set_session("shopping_cart", $shopping_cart);
}
// calculate fingerprint
function calculate_fp ($login_id, $trankey, $amount, $sequence, $timestamp, $currency = "")
{
return (hmac_md5 ($login_id."^".$sequence."^".$timestamp."^".$amount."^".$currency, $trankey));
}
function get_payment_rate($payment_id, $currency_rate)
{
global $db, $table_prefix;
$payment_rate = 1;
$sql = " SELECT parameter_type,parameter_source FROM " . $table_prefix . "payment_parameters ";
$sql .= " WHERE payment_id=" . $db->tosql($payment_id, INTEGER);
$sql .= " AND parameter_name IN ('currency_code', 'x_currency_code', 'currency') ";
$sql .= " AND not_passed<>1 ";
$db->query($sql);
if ($db->next_record()) {
$parameter_type = $db->f("parameter_type");
$parameter_source = $db->f("parameter_source");
if (strtoupper($parameter_type) == "VARIABLE") {
if ($parameter_source == "currency_code" || $parameter_source == "{currency_code}") {
$payment_rate = $currency_rate;
}
} else {
$sql = " SELECT exchange_rate FROM " . $table_prefix . "currencies ";
$sql .= " WHERE currency_code=" . $db->tosql($parameter_source, TEXT);
$db->query($sql);
if ($db->next_record()) {
$payment_rate = $db->f("exchange_rate");
}
}
}
return $payment_rate;
}
function get_final_message($message, $message_type)
{
if (preg_match("/\[" . $message_type . "\](.+)\[\/" . $message_type . "\]/s", $message, $match)) {
$message = $match[1];
} else {
$message = preg_replace("/\[success].*\[\/success]/s", "", $message);
$message = preg_replace("/\[pending].*\[\/pending]/s", "", $message);
$message = preg_replace("/\[failure].*\[\/failure]/s", "", $message);
}
return $message;
}
?>
|
|