Example #1
0
function add_to_cart($sc_item_id, $sc_price, $sc_quantity, $type, $cart, &$new_cart_id, &$second_page_options, &$sc_errors, &$sc_message, $cart_item_id = "", $sc_item_name = "")
{
    global $db, $table_prefix, $site_id, $settings, $eol, $currency;
    $sc_errors = "";
    $sc_message = "";
    $item_added = false;
    $shopping_cart = get_session("shopping_cart");
    if (!is_array($shopping_cart)) {
        $shopping_cart = array();
    }
    $discount_type = get_session("session_discount_type");
    $discount_amount = get_session("session_discount_amount");
    $user_type_id = get_session("session_user_type_id");
    $price_type = get_session("session_price_type");
    if ($price_type == 1) {
        $price_field = "trade_price";
        $sales_field = "trade_sales";
        $additional_price_field = "trade_additional_price";
    } else {
        $price_field = "price";
        $sales_field = "sales_price";
        $additional_price_field = "additional_price";
    }
    $is_error = false;
    if (VA_Products::check_permissions($sc_item_id, VIEW_ITEMS_PERM)) {
        $sql = " SELECT item_type_id,item_name," . $price_field . ",is_price_edit,is_sales," . $sales_field . ",buying_price,";
        $sql .= " tax_id,tax_free,stock_level,";
        $sql .= " use_stock_level,hide_out_of_stock,disable_out_of_stock,min_quantity,max_quantity,quantity_increment ";
        $sql .= " FROM " . $table_prefix . "items ";
        $sql .= " WHERE item_id=" . $db->tosql($sc_item_id, INTEGER);
        $db->query($sql);
        if ($db->next_record()) {
            $item_type_id = $db->f("item_type_id");
            $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");
            $disable_out_of_stock = $db->f("disable_out_of_stock");
            $min_quantity = $db->f("min_quantity");
            if (!strlen($sc_quantity) && $min_quantity) {
                $sc_quantity = $min_quantity;
            }
            if ($sc_quantity < 1) {
                $sc_quantity = 1;
            }
            $max_quantity = $db->f("max_quantity");
            $quantity_increment = $db->f("quantity_increment");
            $buying_price = $db->f("buying_price");
            $tax_id = $db->f("tax_id");
            $tax_free = $db->f("tax_free");
            $is_price_edit = $db->f("is_price_edit");
            if ($is_price_edit) {
                $price = $sc_price;
            } else {
                $price = calculate_price($db->f($price_field), $db->f("is_sales"), $db->f($sales_field));
            }
            $properties_buying = 0;
            $properties_discount = 0;
            $discount_applicable = 1;
        } else {
            $is_error = true;
        }
    } else {
        $is_error = true;
    }
    if ($is_error) {
        // item doesn't exists or no longer available
        if ($type == "db") {
            $item = array("ITEM_ID" => intval($sc_item_id), "ITEM_TYPE_ID" => 0, "ITEM_NAME" => $sc_item_name, "ERROR" => PROD_NOT_AVAILABLE_ERROR, "PROPERTIES" => "", "PROPERTIES_PRICE" => 0, "PROPERTIES_PERCENTAGE" => 0, "PROPERTIES_BUYING" => 0, "PROPERTIES_DISCOUNT" => 0, "PROPERTIES_MORE" => 0, "COMPONENTS" => "", "QUANTITY" => $sc_quantity, "TAX_ID" => 0, "TAX_FREE" => 0, "DISCOUNT" => 0, "BUYING_PRICE" => 0, "PRICE_EDIT" => 0, "PRICE" => $sc_price);
            //-- add to cart with error
            $shopping_cart[] = $item;
            end($shopping_cart);
            $new_cart_id = key($shopping_cart);
            set_session("shopping_cart", $shopping_cart);
            return true;
        } else {
            return false;
        }
    }
    // calculate summary stock levels for products and options available in the cart
    $stock_levels = array();
    foreach ($shopping_cart as $cart_id => $cart_info) {
        $item_id = $cart_info["ITEM_ID"];
        $item_quantity = $cart_info["QUANTITY"];
        $item_properties = $cart_info["PROPERTIES"];
        $item_more_properties = $cart_info["PROPERTIES_MORE"];
        if (!$item_more_properties) {
            if (isset($stock_levels[$item_id])) {
                $stock_levels[$item_id] += $item_quantity;
            } else {
                $stock_levels[$item_id] = $item_quantity;
            }
            $item_components = $cart_info["COMPONENTS"];
            if (is_array($item_components) && sizeof($item_components) > 1) {
                foreach ($item_components as $property_id => $component_values) {
                    foreach ($component_values as $property_item_id => $component) {
                        $sub_item_id = $component["sub_item_id"];
                        $sub_quantity = $component["quantity"];
                        if ($sub_quantity < 1) {
                            $sub_quantity = 1;
                        }
                        if (isset($stock_levels[$sub_item_id])) {
                            $stock_levels[$sub_item_id] += $item_quantity * $sub_quantity;
                        } else {
                            $stock_levels[$sub_item_id] = $item_quantity * $sub_quantity;
                        }
                    }
                }
            }
        }
    }
    // check stock level for parent product
    if (isset($stock_levels[$sc_item_id])) {
        $total_quantity = $stock_levels[$sc_item_id] + $sc_quantity;
    } else {
        $total_quantity = $sc_quantity;
    }
    /*
    			//PRODUCT_MIN_LIMIT_MSG
    			$min_quantity = $db->f("min_quantity");
    			$max_quantity = $db->f("max_quantity");
    			$quantity_increment = $db->f("quantity_increment");
    */
    // check stock levels only if product added to the shopping cart
    if ($cart == "ADD" && $use_stock_level && $stock_level < $total_quantity && ($hide_out_of_stock || $disable_out_of_stock)) {
        if ($stock_level > 0) {
            $limit_error = str_replace("{limit_quantity}", $stock_level, PRODUCT_LIMIT_MSG);
            $limit_error = str_replace("{product_name}", get_translation($item_name), $limit_error);
            $sc_errors .= $limit_error . "<br>";
        } else {
            $sc_errors .= PRODUCT_OUT_STOCK_MSG . "<br>";
        }
        if ($type != "db") {
            return false;
        }
    } elseif ($cart == "ADD" && $min_quantity && $total_quantity < $min_quantity) {
        $limit_error = str_replace("{limit_quantity}", $min_quantity, PRODUCT_MIN_LIMIT_MSG);
        $limit_error = str_replace("{product_name}", get_translation($item_name), $limit_error);
        $sc_errors .= $limit_error . "<br>";
        if ($type != "db") {
            return false;
        }
    } elseif ($cart == "ADD" && $max_quantity && $total_quantity > $max_quantity) {
        $limit_error = str_replace("{limit_quantity}", $max_quantity, PRODUCT_LIMIT_MSG);
        $limit_error = str_replace("{product_name}", get_translation($item_name), $limit_error);
        $sc_errors .= $limit_error . "<br>";
        if ($type != "db") {
            return false;
        }
    } elseif ($cart == "ADD" && $quantity_increment && ($sc_quantity - $min_quantity) % $quantity_increment != 0) {
        $quantity_error = str_replace("{quantity}", $sc_quantity, PRODUCT_QUANTITY_ERROR);
        $quantity_error = str_replace("{product_name}", get_translation($item_name), $quantity_error);
        $sc_errors .= $quantity_error . "<br>";
        $quantities_list = "";
        $quantities_index = 0;
        $quantity_list = $min_quantity ? $min_quantity : $quantity_increment;
        while ((!$max_quantity || $quantity_list < $max_quantity) && $quantities_index < 5) {
            $quantities_index++;
            $quantities_list .= $quantity_list . ", ";
            $quantity_list += $quantity_increment;
        }
        if (!$max_quantity || $quantity_list < $max_quantity) {
            $quantities_list .= "...";
        }
        $quantities_allowed = str_replace("{quantities_list}", $quantities_list, PRODUCT_ALLOWED_QUANTITIES_MSG);
        $sc_errors .= $quantities_allowed . "<br>";
        if ($type != "db") {
            return false;
        }
    } elseif ($is_price_edit && $type != "options") {
        $error_message = "";
        if (!strlen($price)) {
            $error_message = str_replace("{field_name}", PRICE_MSG, REQUIRED_MESSAGE);
        } elseif (!is_numeric($price)) {
            $error_message = str_replace("{field_name}", PRICE_MSG, INCORRECT_VALUE_MESSAGE);
        } elseif ($price < 0) {
            $error_message = str_replace("{field_name}", PRICE_MSG, MIN_VALUE_MESSAGE);
            $error_message = str_replace("{min_value}", "0.01", $error_message);
        }
        if ($error_message) {
            $sc_errors .= $error_message . "<br>" . $eol;
            if ($type != "db") {
                return false;
            }
        } else {
            // convert value to basic currency
            $price = $price / $currency["rate"];
        }
    }
    // get properties from db
    $db_properties = array();
    if ($type == "db") {
        $sql = " SELECT property_id, property_value, property_values_ids FROM " . $table_prefix . "saved_items_properties ";
        $sql .= " WHERE cart_item_id=" . $db->tosql($cart_item_id, INTEGER);
        $db->query($sql);
        while ($db->next_record()) {
            $property_id = $db->f("property_id");
            $property_value = $db->f("property_value");
            $property_values_ids = $db->f("property_values_ids");
            if (strlen($property_value)) {
                $db_properties[$property_id] = array($property_value);
            } elseif (strlen($property_values_ids)) {
                $db_properties[$property_id] = explode(",", $property_values_ids);
            }
        }
    }
    $components = array();
    $components_values = array();
    $components_price = 0;
    $controls_price = 0;
    $properties = "";
    $properties_ids = "";
    $properties_info = "";
    $sql = " SELECT ip.property_type_id, ip.property_order, ip.usage_type, ip.property_id, ip.sub_item_id, ip.property_name, ";
    $sql .= " ip.quantity, ip.quantity_action, ip.property_price_type, ip.additional_price, ip.trade_additional_price, ";
    $sql .= " ip.control_type, ip.required, ip.parent_property_id, ip.parent_value_id, ip.free_price_type, ip.free_price_amount, ip.use_on_second ";
    $sql .= " FROM (" . $table_prefix . "items_properties ip ";
    $sql .= " LEFT JOIN " . $table_prefix . "items_properties_sites ips ON ip.property_id=ips.property_id) ";
    $sql .= " WHERE (ip.item_id=" . $db->tosql($sc_item_id, INTEGER) . " OR ip.item_type_id=" . $db->tosql($item_type_id, INTEGER) . ") ";
    if (isset($site_id)) {
        $sql .= " AND (ip.sites_all=1 OR ips.site_id=" . $db->tosql($site_id, INTEGER) . ")";
    } else {
        $sql .= " AND ip.sites_all=1 ";
    }
    if ($type == "db") {
        $sql .= " AND (ip.use_on_details=1 OR ip.use_on_list=1 OR ip.use_on_second=1)";
    } elseif ($type == "list") {
        $sql .= " AND ip.use_on_list=1 ";
    } elseif ($type == "table") {
        $sql .= " AND ip.use_on_table=1 ";
    } elseif ($type == "grid") {
        $sql .= " AND ip.use_on_grid=1 ";
    } elseif ($type == "options") {
        $sql .= " AND ip.use_on_second=1 ";
    } else {
        $sql .= " AND ip.use_on_details=1 ";
    }
    $sql .= " ORDER BY ip.property_order, ip.property_id ";
    $db->query($sql);
    while ($db->next_record()) {
        $property_id = $db->f("property_id");
        $property_name = $db->f("property_name");
        $property_order = $db->f("property_order");
        $usage_type = $db->f("usage_type");
        $parent_property_id = $db->f("parent_property_id");
        $parent_value_id = $db->f("parent_value_id");
        $property_type_id = $db->f("property_type_id");
        $property_name = get_translation($db->f("property_name"));
        $property_price_type = $db->f("property_price_type");
        $additional_price = $db->f($additional_price_field);
        $free_price_type = $db->f("free_price_type");
        $free_price_amount = $db->f("free_price_amount");
        $property_quantity_action = $db->f("quantity_action");
        $use_on_second = $db->f("use_on_second");
        $option_step = $use_on_second ? 2 : 1;
        if ($property_type_id == 2) {
            $sub_item_id = $db->f("sub_item_id");
            $sub_quantity = $db->f("quantity");
            if ($sub_quantity < 1) {
                $sub_quantity = 1;
            }
            $components[$property_id][0] = array("type_id" => 2, "usage_type" => $usage_type, "sub_item_id" => $sub_item_id, "quantity" => $sub_quantity, "quantity_action" => $property_quantity_action, "price" => $additional_price);
        } else {
            $property_type = $db->f("control_type");
            $property_required = $db->f("required");
            $property_values = array();
            $values_text = array();
            if ($properties_ids) {
                $properties_ids .= ",";
            }
            $properties_ids .= $property_id;
            if ($type == "db") {
                // get properties from db
                if (isset($db_properties[$property_id])) {
                    $property_values = $db_properties[$property_id];
                }
            } else {
                // get properties from form
                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 {
                    if ($property_type == "TEXTBOXLIST") {
                        $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) {
                                $value_id = get_param("property_value_" . $property_id . "_" . $i);
                                $property_values[] = $value_id;
                                $values_text[$value_id] = $property_value;
                            }
                        }
                    } else {
                        $property_value = get_param("property_" . $property_id);
                        if (strlen($property_value)) {
                            if ($property_type == "IMAGEUPLOAD" && !preg_match("/^http\\:\\/\\//", $property_value)) {
                                $property_value = $settings["site_url"] . "images/options/" . $property_value;
                            }
                            $property_values[] = $property_value;
                            if ($property_type == "TEXTBOX" || $property_type == "TEXTAREA") {
                                $values_text[$property_value] = $property_value;
                            }
                        }
                    }
                }
            }
            $control_price = calculate_control_price($property_values, $values_text, $property_price_type, $additional_price, $free_price_type, $free_price_amount);
            $controls_price += $control_price;
            // add all properties for further checks for their different use
            $properties_info[$property_id] = array("USAGE_TYPE" => $usage_type, "CONTROL" => $property_type, "TYPE" => $property_type_id, "NAME" => $property_name, "VALUES" => $property_values, "REQUIRED" => $property_required, "PARENT_PROPERTY_ID" => $parent_property_id, "PARENT_VALUE_ID" => $parent_value_id, "TEXT" => $values_text, "CONTROL_PRICE" => $control_price, "ORDER" => $property_order, "QUANTITY_ACTION" => $property_quantity_action, "OPTION_STEP" => $option_step, "BUYING" => 0, "PRICE" => 0, "PERCENTAGE" => 0);
        }
    }
    // check components
    foreach ($components as $property_id => $component_values) {
        $component = $component_values[0];
        if ($component["usage_type"] == 2) {
            $sql = " SELECT item_id FROM " . $table_prefix . "items_properties_assigned ";
            $sql .= " WHERE item_id=" . $db->tosql($sc_item_id, INTEGER);
            $sql .= " AND property_id=" . $db->tosql($property_id, INTEGER);
            $db->query($sql);
            if (!$db->next_record()) {
                // remove component if it wasn't assigned to product
                unset($components[$property_id]);
                continue;
            }
        }
        /*if (isset($component["sub_item_id"]) && $component["sub_item_id"]) {
        			if (!VA_Products::check_permissions($component["sub_item_id"], VIEW_ITEMS_PERM)) {
        				unset($components[$property_id]);
        				continue;
        			}
        		}*/
    }
    // check usage and required settings for product options and populate $product_properties array
    $product_properties = "";
    if (isset($properties_info) && is_array($properties_info)) {
        foreach ($properties_info as $property_id => $property_info) {
            $property_exists = true;
            if ($property_info["USAGE_TYPE"] == 2) {
                // check if option should be assigned to product first
                $sql = " SELECT item_id FROM " . $table_prefix . "items_properties_assigned ";
                $sql .= " WHERE item_id=" . $db->tosql($sc_item_id, INTEGER);
                $sql .= " AND property_id=" . $db->tosql($property_id, INTEGER);
                $db->query($sql);
                if (!$db->next_record()) {
                    // remove option if it wasn't assigned to product
                    $property_exists = false;
                    unset($properties_info[$property_id]);
                }
            }
            $parent_property_id = $property_info["PARENT_PROPERTY_ID"];
            $parent_value_id = $property_info["PARENT_VALUE_ID"];
            if ($property_exists && $parent_property_id) {
                $values = array();
                if (isset($properties_info[$parent_property_id]["VALUES"])) {
                    $values = $properties_info[$parent_property_id]["VALUES"];
                }
                if (!isset($properties_info[$parent_property_id]) || sizeof($values) == 0) {
                    $property_exists = false;
                    unset($properties_info[$property_id]);
                } else {
                    if ($parent_value_id && !in_array($parent_value_id, $values)) {
                        $property_exists = false;
                        unset($properties_info[$property_id]);
                    }
                }
            }
            if ($property_exists) {
                $property_values = $property_info["VALUES"];
                $property_required = $property_info["REQUIRED"];
                if (sizeof($property_values) > 0) {
                    $properties[$property_id] = $property_values;
                    if ($property_info["TYPE"] == 3) {
                        $components_values[$property_id] = $property_values;
                    }
                    $product_properties[$property_id] = $property_info;
                } else {
                    if ($property_required) {
                        $property_error = str_replace("{property_name}", $property_info["NAME"], REQUIRED_PROPERTY_MSG);
                        $property_error = str_replace("{product_name}", get_translation($item_name), $property_error);
                        $sc_errors .= $property_error . "<br>";
                    }
                }
            }
        }
    }
    // calculate summary stock levels for options recently selected
    $options_levels = array();
    foreach ($shopping_cart as $cart_id => $cart_info) {
        $item_id = $cart_info["ITEM_ID"];
        $item_quantity = $cart_info["QUANTITY"];
        $item_properties = $cart_info["PROPERTIES"];
        $item_more_properties = $cart_info["PROPERTIES_MORE"];
        if (!$item_more_properties) {
            if (is_array($item_properties)) {
                foreach ($item_properties as $property_id => $property_values) {
                    if (isset($product_properties[$property_id])) {
                        $ct = $product_properties[$property_id]["CONTROL"];
                        if (strtoupper($ct) == "LISTBOX" || strtoupper($ct) == "RADIOBUTTON" || strtoupper($ct) == "CHECKBOXLIST" || strtoupper($ct) == "TEXTBOXLIST") {
                            for ($ov = 0; $ov < sizeof($property_values); $ov++) {
                                $option_value_id = $property_values[$ov];
                                if (isset($options_levels[$option_value_id])) {
                                    $options_levels[$option_value_id] += $item_quantity;
                                } else {
                                    $options_levels[$option_value_id] = $item_quantity;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    // check components values for selection
    if (sizeof($components_values)) {
        foreach ($components_values as $property_id => $values) {
            for ($v = 0; $v < sizeof($values); $v++) {
                $item_property_id = $values[$v];
                $sql = " SELECT ipv.sub_item_id, ipv.quantity, ipv.additional_price, ipv.trade_additional_price ";
                $sql .= " FROM " . $table_prefix . "items_properties_values ipv ";
                $sql .= " WHERE ipv.item_property_id=" . $db->tosql($item_property_id, INTEGER);
                $db->query($sql);
                if ($db->next_record()) {
                    $sub_item_id = $db->f("sub_item_id");
                    $sub_quantity = $db->f("quantity");
                    if ($sub_quantity < 1) {
                        $sub_quantity = 1;
                    }
                    $additional_price = $db->f($additional_price_field);
                    $components[$property_id][$item_property_id] = array("type_id" => 3, "sub_item_id" => $sub_item_id, "quantity" => $sub_quantity, "quantity_action" => $properties_info[$property_id]["QUANTITY_ACTION"], "price" => $additional_price);
                }
            }
        }
    }
    $second_page_options = 0;
    if ($type != "options" && $type != "db") {
        $sql = " SELECT COUNT(*) ";
        $sql .= " FROM (" . $table_prefix . "items_properties ip ";
        $sql .= " LEFT JOIN " . $table_prefix . "items_properties_sites ips ON ip.property_id=ips.property_id) ";
        $sql .= " WHERE (ip.item_id=" . $db->tosql($sc_item_id, INTEGER) . " OR ip.item_type_id=" . $db->tosql($item_type_id, INTEGER) . ") ";
        $sql .= " AND ip.use_on_second=1 AND ip.property_type_id<>2 ";
        if (isset($site_id)) {
            $sql .= " AND (ip.sites_all=1 OR ips.site_id=" . $db->tosql($site_id, INTEGER) . ")";
        } else {
            $sql .= " AND ip.sites_all=1 ";
        }
        if ($properties_ids) {
            $sql .= " AND ip.property_id NOT IN (" . $properties_ids . ") ";
        }
        $second_page_options = get_db_value($sql);
    }
    if ($sc_errors && $type != "db") {
        // error occurred can't continue process
        return false;
    }
    // TODO: add some new status parameter to check different shopping cart items
    if ($cart == "SHIPPING") {
        // use second_page_options property for a product we check shipping
        $second_page_options = true;
    }
    // begin calculate buying, price and percentage values
    $properties_price = 0;
    $properties_percentage = 0;
    if (is_array($properties)) {
        foreach ($properties as $property_id => $property_values) {
            if (strtoupper($product_properties[$property_id]["CONTROL"]) == "LISTBOX" || strtoupper($product_properties[$property_id]["CONTROL"]) == "RADIOBUTTON" || strtoupper($product_properties[$property_id]["CONTROL"]) == "CHECKBOXLIST" || strtoupper($product_properties[$property_id]["CONTROL"]) == "TEXTBOXLIST") {
                for ($pv = 0; $pv < sizeof($property_values); $pv++) {
                    if ($product_properties[$property_id]["TYPE"] == 3) {
                    } else {
                        $item_property_id = $property_values[$pv];
                        if (isset($options_levels[$item_property_id])) {
                            $option_quantity = $options_levels[$item_property_id] + $sc_quantity;
                        } else {
                            $option_quantity = $sc_quantity;
                        }
                        $sql = " SELECT buying_price, additional_price, trade_additional_price, percentage_price, additional_weight, ";
                        $sql .= " property_value, stock_level, use_stock_level, hide_out_of_stock ";
                        $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_field);
                            $percentage_price = $db->f("percentage_price");
                            $buying_price = $db->f("buying_price");
                            $properties_price += $additional_price;
                            $properties_percentage += $percentage_price;
                            $properties_buying += $buying_price;
                            $option_value = get_translation($db->f("property_value"));
                            $option_stock_level = $db->f("stock_level");
                            $option_use_stock = $db->f("use_stock_level");
                            $option_hide_stock = $db->f("hide_out_of_stock");
                            // populate properties array with prices information
                            $product_properties[$property_id]["BUYING"] += $buying_price;
                            $product_properties[$property_id]["PRICE"] += $additional_price;
                            $product_properties[$property_id]["PERCENTAGE"] += $percentage_price;
                        }
                        // check stock levels only if product added to shopping cart
                        if ($cart == "ADD" && $option_use_stock && $option_stock_level < $option_quantity && $option_hide_stock) {
                            if ($option_stock_level > 0) {
                                $limit_product = get_translation($item_name) . " (" . $product_properties[$property_id]["NAME"] . ": " . $option_value . ")";
                                $limit_error = str_replace("{limit_quantity}", $option_stock_level, PRODUCT_LIMIT_MSG);
                                $limit_error = str_replace("{product_name}", $limit_product, $limit_error);
                                $sc_errors .= $limit_error . "<br>";
                            } else {
                                $sc_errors .= PRODUCT_OUT_STOCK_MSG . "<br>";
                            }
                            if ($type != "db") {
                                return false;
                            }
                        }
                    }
                }
            }
        }
    }
    // end calculate buying, price and percentage values
    // check if the item already in the cart than increase quantity
    $in_cart = false;
    if ($cart == "ADD" && !$second_page_options && $type != "options") {
        foreach ($shopping_cart as $in_cart_id => $item) {
            if ($item["ITEM_ID"] == $sc_item_id && !$item["PROPERTIES_MORE"]) {
                $item_properties = $item["PROPERTIES"];
                $item_properties_info = $item["PROPERTIES_INFO"];
                if (!is_array($item_properties) && !is_array($properties)) {
                    $in_cart = true;
                    break;
                } elseif (is_array($item_properties) && is_array($properties) && $item_properties_info == $product_properties) {
                    // compare if new product and product in the cart has the same options values
                    $in_cart = true;
                    break;
                }
            }
        }
    }
    if ($in_cart) {
        $new_quantity = $shopping_cart[$in_cart_id]["QUANTITY"] + $sc_quantity;
    } else {
        $new_quantity = $sc_quantity;
    }
    // check components prices and stock levels
    if (sizeof($components) > 0) {
        foreach ($components as $property_id => $component_values) {
            foreach ($component_values as $item_property_id => $component) {
                $sub_item_id = $component["sub_item_id"];
                $sub_quantity = $component["quantity"];
                if ($sub_quantity < 1) {
                    $sub_quantity = 1;
                }
                $component_price = $component["price"];
                if (isset($stock_levels[$sub_item_id])) {
                    $component_quantity = $stock_levels[$sub_item_id] + $sc_quantity * $sub_quantity;
                } else {
                    $component_quantity = $sc_quantity * $sub_quantity;
                }
                $sql = " SELECT i.item_type_id, i.item_name, i.buying_price, i." . $price_field . ", i.is_sales, i." . $sales_field . ", i.tax_id, i.tax_free, ";
                $sql .= " i.stock_level, i.use_stock_level, i.hide_out_of_stock, i.disable_out_of_stock ";
                $sql .= " FROM " . $table_prefix . "items i ";
                $sql .= " WHERE i.item_id=" . $db->tosql($sub_item_id, INTEGER);
                $db->query($sql);
                if ($db->next_record()) {
                    $sub_type_id = $db->f("item_type_id");
                    $sub_tax_id = $db->f("tax_id");
                    $sub_tax_free = $db->f("tax_free");
                    $sub_stock_level = $db->f("stock_level");
                    $sub_use_stock = $db->f("use_stock_level");
                    $sub_hide_stock = $db->f("hide_out_of_stock");
                    $sub_disable_stock = $db->f("disable_out_of_stock");
                    $sub_item_name = get_translation($db->f("item_name"));
                    // check stock levels only if product added to shopping cart
                    if ($cart == "ADD" && $sub_use_stock && $sub_stock_level < $component_quantity && ($sub_hide_stock || $sub_disable_stock)) {
                        if ($sub_stock_level > 0) {
                            $limit_product = get_translation($item_name);
                            if (isset($product_properties[$property_id]["NAME"])) {
                                $limit_product .= " (" . $product_properties[$property_id]["NAME"] . ": " . $sub_item_name . ")";
                            }
                            $limit_error = str_replace("{limit_quantity}", $sub_stock_level, PRODUCT_LIMIT_MSG);
                            $limit_error = str_replace("{product_name}", $limit_product, $limit_error);
                            $sc_errors .= $limit_error . "<br>";
                        } else {
                            $sc_errors .= PRODUCT_OUT_STOCK_MSG . "<br>";
                        }
                        if ($type != "db") {
                            return false;
                        }
                    }
                    $components[$property_id][$item_property_id]["item_type_id"] = $sub_type_id;
                    $components[$property_id][$item_property_id]["tax_id"] = $sub_tax_id;
                    $components[$property_id][$item_property_id]["tax_free"] = $sub_tax_free;
                    if (!strlen($component_price)) {
                        $sub_price = $db->f($price_field);
                        $sub_is_sales = $db->f("is_sales");
                        $sub_sales = $db->f($sales_field);
                        if ($sub_is_sales && $sub_sales > 0) {
                            $components[$property_id][$item_property_id]["base_price"] = $sub_sales;
                        } else {
                            $components[$property_id][$item_property_id]["base_price"] = $sub_price;
                        }
                        $user_price = false;
                        $user_price_action = 0;
                        $q_prices = get_quantity_price($sub_item_id, $new_quantity * $sub_quantity);
                        if ($q_prices) {
                            $user_price = $q_prices[0];
                            $user_price_action = $q_prices[2];
                        }
                        $components[$property_id][$item_property_id]["buying"] = $db->f("buying_price");
                        $components[$property_id][$item_property_id]["user_price"] = $user_price;
                        $components[$property_id][$item_property_id]["user_price_action"] = $user_price_action;
                        if ($in_cart) {
                            $shopping_cart[$in_cart_id]["COMPONENTS"][$property_id][$item_property_id] = $components[$property_id][$item_property_id];
                        }
                    }
                } else {
                    // there is no such subcomponent
                    $sc_errors .= "Component is missing.<br>";
                    if ($type != "db") {
                        return false;
                    }
                }
            }
        }
    }
    if ($in_cart && !$is_price_edit) {
        $shopping_cart[$in_cart_id]["QUANTITY"] += $sc_quantity;
        $quantity_price = get_quantity_price($item["ITEM_ID"], $shopping_cart[$in_cart_id]["QUANTITY"]);
        if (sizeof($quantity_price) > 0) {
            $shopping_cart[$in_cart_id]["PRICE"] = $quantity_price[0];
            $shopping_cart[$in_cart_id]["PROPERTIES_DISCOUNT"] = $quantity_price[1];
            $shopping_cart[$in_cart_id]["DISCOUNT"] = $quantity_price[2];
        }
        $item_added = true;
    } else {
        if ($type == "options") {
            // remove options for all following steps if they were added before
            $options_step = 2;
            $all_properties = $shopping_cart[$cart_id]["PROPERTIES"];
            $all_properties_info = $shopping_cart[$cart_id]["PROPERTIES_INFO"];
            if (is_array($all_properties)) {
                foreach ($all_properties_info as $property_id => $property_info) {
                    if ($property_info["OPTION_STEP"] >= $options_step) {
                        unset($all_properties[$property_id]);
                        unset($all_properties_info[$property_id]);
                    }
                }
            }
            if (is_array($properties)) {
                foreach ($properties as $property_id => $property_values) {
                    $all_properties[$property_id] = $property_values;
                }
                foreach ($product_properties as $property_id => $property_info) {
                    $all_properties_info[$property_id] = $property_info;
                }
            }
            $shopping_cart[$cart_id]["PROPERTIES"] = $all_properties;
            $shopping_cart[$cart_id]["PROPERTIES_INFO"] = $all_properties_info;
            $shopping_cart[$cart_id]["PROPERTIES_MORE"] = 0;
            // recalculate options totals
            $shopping_cart[$cart_id]["PROPERTIES_PRICE"] = 0;
            $shopping_cart[$cart_id]["PROPERTIES_PERCENTAGE"] = 0;
            $shopping_cart[$cart_id]["PROPERTIES_BUYING"] = 0;
            if (is_array($all_properties)) {
                foreach ($all_properties_info as $property_id => $property_info) {
                    $shopping_cart[$cart_id]["PROPERTIES_PRICE"] += $property_info["PRICE"];
                    $shopping_cart[$cart_id]["PROPERTIES_PERCENTAGE"] += $property_info["PERCENTAGE"];
                    $shopping_cart[$cart_id]["PROPERTIES_BUYING"] += $property_info["BUYING"];
                }
            }
            if ($cart == "WISHLIST") {
                add_to_saved_items($shopping_cart, $new_cart_id, 0, true);
            }
        } else {
            if (!$is_price_edit) {
                $quantity_price = get_quantity_price($sc_item_id, $sc_quantity);
                if (sizeof($quantity_price) > 0) {
                    $price = $quantity_price[0];
                    $properties_discount = $quantity_price[1];
                    $discount_applicable = $quantity_price[2];
                }
            }
            $item = array("ITEM_ID" => intval($sc_item_id), "ITEM_TYPE_ID" => $item_type_id, "CART_ITEM_ID" => $cart_item_id, "SAVED_TYPE_ID" => get_param("saved_type_id"), "ITEM_NAME" => $item_name, "ERROR" => $sc_errors, "PROPERTIES" => $properties, "PROPERTIES_INFO" => $product_properties, "PROPERTIES_PRICE" => $properties_price + $controls_price, "PROPERTIES_PERCENTAGE" => $properties_percentage, "PROPERTIES_BUYING" => $properties_buying, "PROPERTIES_DISCOUNT" => $properties_discount, "PROPERTIES_MORE" => $second_page_options, "COMPONENTS" => $components, "QUANTITY" => $sc_quantity, "TAX_ID" => $tax_id, "TAX_FREE" => $tax_free, "DISCOUNT" => $discount_applicable, "BUYING_PRICE" => $buying_price, "PRICE_EDIT" => $is_price_edit, "PRICE" => $price);
            //-- add to cart
            $shopping_cart[] = $item;
            end($shopping_cart);
            $new_cart_id = key($shopping_cart);
            if ($cart == "WISHLIST" && !$second_page_options) {
                add_to_saved_items($shopping_cart, $new_cart_id, 0, true);
            }
        }
        $item_added = true;
    }
    // save session
    set_session("shopping_cart", $shopping_cart);
    // return success message
    $sc_message = str_replace("{product_name}", get_translation($item_name), ADDED_PRODUCT_MSG);
    return $item_added;
}
Example #2
0
 $sql = " SELECT property_type_id, property_name, control_type, ";
 $sql .= " property_price_type, additional_price, trade_additional_price, free_price_type, free_price_amount ";
 $sql .= " FROM " . $table_prefix . "items_properties ";
 $sql .= " WHERE property_id=" . $db->tosql($property_id, INTEGER);
 $db->query($sql);
 if ($db->next_record()) {
     $property_type_id = $db->f("property_type_id");
     // show only product options and subcomponents separately
     if ($property_type_id == 1) {
         $property_name = get_translation($db->f("property_name"));
         $control_type = $db->f("control_type");
         $property_price_type = $db->f("property_price_type");
         $additional_price = $db->f($additional_price_field);
         $free_price_type = $db->f("free_price_type");
         $free_price_amount = $db->f("free_price_amount");
         $property_price = calculate_control_price($item["PROPERTIES_INFO"][$property_id]["VALUES"], $item["PROPERTIES_INFO"][$property_id]["TEXT"], $property_price_type, $additional_price, $free_price_type, $free_price_amount);
         $properties_price += $property_price;
         if (strtoupper($control_type) == "LISTBOX" || strtoupper($control_type) == "RADIOBUTTON" || strtoupper($control_type) == "CHECKBOXLIST" || strtoupper($control_type) == "TEXTBOXLIST") {
             $values_list = "";
             for ($pv = 0; $pv < sizeof($property_values); $pv++) {
                 $sql = " SELECT property_value, " . $additional_price_field . ", percentage_price, buying_price, ";
                 $sql .= " item_code, manufacturer_code ";
                 $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);
                 $db->query($sql);
                 if ($db->next_record()) {
                     $additional_price = $db->f($additional_price_field);
                     $percentage_price = $db->f("percentage_price");
                     $item_code .= $db->f("item_code");
                     $manufacturer_code .= $db->f("manufacturer_code");