function fn_exim_put_product_combination($product_id, $product_name, $combination_code, $combination, $amount, &$counter, $lang_code = CART_LANGUAGE)
{
    $pair_delimiter = ':';
    $set_delimiter = ',';
    if (!empty($combination)) {
        // Get product_id
        $object_id = 0;
        if (!empty($product_id)) {
            $object_exists = db_get_field('SELECT COUNT(*) FROM ?:products WHERE product_id = ?i', $product_id);
            if ($object_exists) {
                $object_id = $product_id;
            }
        }
        if (empty($object_id) && !empty($product_name)) {
            $object_id = db_get_field('SELECT product_id FROM ?:product_descriptions WHERE product = ?s AND lang_code = ?s', $product_name, $lang_code);
        }
        if (empty($object_id)) {
            $counter['S']++;
            return false;
        }
        $options = explode($set_delimiter, $combination);
        if (!empty($options)) {
            $_combination = array();
            foreach ($options as $option_pair) {
                $pair = explode($pair_delimiter, $option_pair);
                if (is_array($pair)) {
                    array_walk($pair, 'fn_trim_helper');
                    $option_id = db_get_field("SELECT o.option_id FROM ?:product_options_descriptions as d INNER JOIN ?:product_options as o ON o.option_id = d.option_id AND o.product_id = ?i WHERE d.option_name = ?s AND d.lang_code = ?s LIMIT 1", $object_id, $pair[0], $lang_code);
                    $variant_id = db_get_field("SELECT v.variant_id FROM ?:product_option_variants_descriptions as d INNER JOIN ?:product_option_variants as v ON v.variant_id = d.variant_id AND v.option_id = ?i WHERE d.variant_name = ?s AND d.lang_code = ?s LIMIT 1", $option_id, $pair[1], $lang_code);
                    if (empty($option_id) || empty($variant_id)) {
                        $counter['S']++;
                        return false;
                    }
                    $_combination[$option_id] = $variant_id;
                }
            }
            $combination = fn_get_options_combination($_combination);
            $combination_hash = fn_generate_cart_id($object_id, array('product_options' => $_combination));
            $object_exists = db_get_field('SELECT COUNT(*) FROM ?:product_options_inventory WHERE combination_hash = ?i AND product_id = ?i', $combination_hash, $object_id);
            $_data = array('product_id' => $object_id, 'product_code' => $combination_code, 'combination_hash' => $combination_hash, 'combination' => $combination, 'amount' => $amount);
            if ($object_exists) {
                db_query('UPDATE ?:product_options_inventory SET ?u WHERE combination_hash = ?i', $_data, $combination_hash);
                fn_echo(fn_get_lang_var('updating') . ' ' . fn_get_lang_var('product_combinations') . '...');
                $counter['E']++;
            } else {
                db_query('INSERT INTO ?:product_options_inventory ?e', $_data);
                fn_echo(fn_get_lang_var('creating') . ' ' . fn_get_lang_var('product_combinations') . '...');
                $counter['N']++;
            }
            fn_echo('<b>' . $object_id . '</b>.<br />');
            return $combination;
        }
    }
    $counter['S']++;
    return false;
}
Example #2
0
function fn_exim_1c_offers($xml, $company_id, $lang_code)
{
    $cml = fn_get_cml_tag_names();
    $create_prices = Registry::get('addons.rus_exim_1c.exim_1c_create_prices');
    $type_option = Registry::get('addons.rus_exim_1c.exim_1c_type_option');
    $hide_product = Registry::get('addons.rus_exim_1c.exim_1c_add_out_of_stock');
    $schema_version = Registry::get('addons.rus_exim_1c.exim_1c_schema_version');
    if (isset($xml->{$cml}['packages']->{$cml}['prices_types']) && $create_prices == 'Y') {
        $prices_1c = array();
        $_prices_1c = db_get_array("SELECT price_1c, type, usergroup_id FROM ?:rus_exim_1c_prices");
        foreach ($xml->{$cml}['packages']->{$cml}['prices_types']->{$cml}['price_type'] as $_price) {
            foreach ($_prices_1c as $_price_1c) {
                if ($_price_1c['price_1c'] == strval($_price->{$cml}['name'])) {
                    $_price_1c['external_id'] = strval($_price->{$cml}['id']);
                    $prices_1c[] = $_price_1c;
                }
            }
        }
    }
    if (isset($xml->{$cml}['packages']->{$cml}['offers'])) {
        $standart_option_name = Registry::get('addons.rus_exim_1c.exim_1c_standart_option_name');
        $import_mode = Registry::get('addons.rus_exim_1c.exim_1c_import_mode_offers');
        $standart_option_name = Registry::get('addons.rus_exim_1c.exim_1c_import_option_name');
        $options_data = $global_options_data = array();
        foreach ($xml->{$cml}['packages']->{$cml}['offers']->{$cml}['offer'] as $offer) {
            $product = array();
            $ids = fn_explode('#', strval($offer->{$cml}['id']));
            $product_guid = array_shift($ids);
            $combination_guid = !empty($ids) ? array_shift($ids) : '';
            $product_id = db_get_field("SELECT product_id FROM ?:products WHERE external_id = ?s", $product_guid);
            if (empty($product_id)) {
                continue;
            }
            $amount = 0;
            if (isset($offer->{$cml}['store'])) {
                foreach ($offer->{$cml}['store'] as $store) {
                    $amount += strval($store[$cml['in_stock']]);
                }
            }
            if (isset($offer->{$cml}['amount'])) {
                $amount = strval($offer->{$cml}['amount']);
            }
            $prices = array();
            if (isset($offer->{$cml}['prices'])) {
                if ($create_prices == 'Y') {
                    foreach ($offer->{$cml}['prices']->{$cml}['price'] as $_price_data) {
                        foreach ($prices_1c as $price_1c) {
                            if (strval($_price_data->{$cml}['price_id']) == $price_1c['external_id']) {
                                if ($price_1c['type'] == 'base') {
                                    $prices['base_price'] = strval($_price_data->{$cml}['price_per_item']);
                                }
                                if ($price_1c['type'] == 'list') {
                                    $prices['list_price'] = strval($_price_data->{$cml}['price_per_item']);
                                }
                                if ($price_1c['usergroup_id'] > 0) {
                                    $prices['qty_prices'][] = array('usergroup_id' => $price_1c['usergroup_id'], 'price' => strval($_price_data->{$cml}['price_per_item']));
                                }
                            }
                        }
                    }
                } else {
                    $prices['base_price'] = strval($offer->{$cml}['prices']->{$cml}['price']->{$cml}['price_per_item']);
                }
            }
            if (empty($prices)) {
                $prices['base_price'] = 0;
            }
            if (empty($combination_guid)) {
                db_query("UPDATE ?:products SET ?u WHERE product_id = ?i", array('amount' => $amount), $product_id);
                // To hide products if they are out of stock
                if ($hide_product == 'Y') {
                    if ($amount == 0) {
                        db_query("UPDATE ?:products SET status = 'H' WHERE product_id = ?i", $product_id);
                    }
                }
                fn_exim_1c_add_price($prices, $product_id);
            } else {
                db_query("UPDATE ?:products SET ?u WHERE product_id = ?i", array('tracking' => 'O'), $product_id);
                if ($schema_version == '2.07') {
                    if (!empty($prices['base_price'])) {
                        fn_exim_1c_add_price(array('base_price' => 0), $product_id);
                    }
                    $option_id = db_get_field("SELECT option_id FROM ?:product_options WHERE product_id = ?i", $product_id);
                    $variant_id = db_get_field("SELECT variant_id FROM ?:product_option_variants WHERE external_id = ?s AND option_id = ?i", $combination_guid, $option_id);
                    db_query("UPDATE ?:product_option_variants SET modifier = ?i WHERE variant_id = ?i", $prices['base_price'], $variant_id);
                    $old_combination_hash = db_get_field("SELECT combination_hash FROM ?:product_options_inventory WHERE external_id = ?s", $combination_guid);
                    $image_pair_id = db_get_field("SELECT pair_id FROM ?:images_links WHERE object_id = ?i", $old_combination_hash);
                    db_query("DELETE FROM ?:product_options_inventory WHERE external_id = ?s AND product_id = ?i", $combination_guid, $product_id);
                    $combination_data = array('product_id' => $product_id, 'combination_hash' => fn_generate_cart_id($product_id, array('product_options' => array($option_id => $variant_id))), 'combination' => fn_get_options_combination(array($option_id => $variant_id)), 'amount' => $amount, 'external_id' => $combination_guid);
                    $variant_combination = db_get_field("SELECT combination_hash FROM ?:product_options_inventory WHERE combination_hash = ?i", $combination_data['combination_hash']);
                    if (empty($variant_combination)) {
                        db_query("INSERT INTO ?:product_options_inventory ?e", $combination_data);
                    }
                    if (!empty($image_pair_id)) {
                        db_query("UPDATE ?:images_links SET object_id = ?i WHERE pair_id = ?i", $combination_data['combination_hash'], $image_pair_id);
                    }
                    continue;
                }
                if ($import_mode == 'standart') {
                    $variant_name = '';
                    if (!empty($offer->{$cml}['product_features']->{$cml}['product_feature'])) {
                        foreach ($offer->{$cml}['product_features']->{$cml}['product_feature'] as $feature_data) {
                            $variant_name .= strval($feature_data->{$cml}['name']) . ':' . strval($feature_data->{$cml}['value']) . '; ';
                        }
                    }
                    $options_data[$product_id][] = array('variant_name' => $variant_name, 'amount' => $amount, 'combination_guid' => $combination_guid, 'price' => $prices['base_price']);
                    if (!empty($prices['base_price'])) {
                        fn_exim_1c_add_price(array('base_price' => 0), $product_id);
                    }
                } elseif ($import_mode == 'global_option') {
                    $combination = array();
                    foreach ($offer->{$cml}['product_features']->{$cml}['product_feature'] as $feature_data) {
                        if (isset($global_options_data[strval($feature_data->{$cml}['name'])])) {
                            if (!in_array(strval($feature_data->{$cml}['value']), $global_options_data[strval($feature_data->{$cml}['name'])]['variants'])) {
                                $global_options_data[strval($feature_data->{$cml}['name'])]['variants'][] = strval($feature_data->{$cml}['value']);
                            }
                            if (!in_array($product_id, $global_options_data[strval($feature_data->{$cml}['name'])]['product_ids'])) {
                                $global_options_data[strval($feature_data->{$cml}['name'])]['product_ids'][] = $product_id;
                            }
                        } else {
                            $global_options_data[strval($feature_data->{$cml}['name'])]['variants'][] = strval($feature_data->{$cml}['value']);
                            $global_options_data[strval($feature_data->{$cml}['name'])]['product_ids'][] = $product_id;
                        }
                        $combination[] = array('option_name' => strval($feature_data->{$cml}['name']), 'variant_name' => strval($feature_data->{$cml}['value']));
                    }
                    $options_data[$product_id][] = array('combination' => $combination, 'amount' => $amount, 'combination_guid' => $combination_guid);
                } elseif ($import_mode == 'individual_option') {
                    $combination = array();
                    foreach ($offer->{$cml}['product_features']->{$cml}['product_feature'] as $feature_data) {
                        $combination[] = array('option_name' => strval($feature_data->{$cml}['name']), 'variant_name' => strval($feature_data->{$cml}['value']));
                    }
                    $options_data[$product_id][] = array('combination' => $combination, 'amount' => $amount, 'combination_guid' => $combination_guid);
                }
            }
        }
        if ($schema_version == '2.07') {
            return;
        }
        if ($import_mode == 'standart') {
            foreach ($options_data as $pid => $variants_data) {
                $option_id = db_get_field("SELECT option_id FROM ?:product_options WHERE product_id = ?i", $pid);
                $option_id = empty($option_id) ? 0 : $option_id;
                $option_data = fn_exim_1c_create_option_structure($pid, $standart_option_name, $company_id, $type_option);
                foreach ($variants_data as $variant_data) {
                    $variant_id = db_get_field("SELECT variants.variant_id FROM ?:product_option_variants AS variants " . "LEFT JOIN ?:product_option_variants_descriptions AS variants_descriptions ON variants.variant_id = variants_descriptions.variant_id " . "WHERE variants.option_id = ?i AND variants_descriptions.lang_code = ?s AND variants_descriptions.variant_name = ?s", $option_id, $lang_code, $variant_data['variant_name']);
                    $variant_id = empty($variant_id) ? 0 : $variant_id;
                    $option_data['variants'][] = array('variant_name' => $variant_data['variant_name'], 'variant_id' => $variant_id, 'modifier_type' => 'A', 'modifier' => $variant_data['price'], 'weight_modifier' => 0, 'weight_modifier_type' => 'A', 'external_id' => $variant_data['combination_guid']);
                }
                $option_id = fn_update_product_option($option_data, $option_id, $lang_code);
                $empty_stock = true;
                foreach ($variants_data as $variant_data) {
                    $variant_id = db_get_field("SELECT variant_id FROM ?:product_option_variants WHERE external_id = ?s AND option_id = ?i", $variant_data['combination_guid'], $option_id);
                    $old_combination_hash = db_get_field("SELECT combination_hash FROM ?:product_options_inventory WHERE external_id = ?s", $variant_data['combination_guid']);
                    $image_pair_id = db_get_field("SELECT pair_id FROM ?:images_links WHERE object_id = ?i", $old_combination_hash);
                    db_query("DELETE FROM ?:product_options_inventory WHERE external_id = ?s AND product_id = ?i", $variant_data['combination_guid'], $pid);
                    $combination_data = array('product_id' => $pid, 'combination_hash' => fn_generate_cart_id($pid, array('product_options' => array($option_id => $variant_id))), 'combination' => fn_get_options_combination(array($option_id => $variant_id)), 'amount' => $variant_data['amount'], 'external_id' => $variant_data['combination_guid']);
                    $variant_combination = db_get_field("SELECT combination_hash FROM ?:product_options_inventory WHERE combination_hash = ?i", $combination_data['combination_hash']);
                    if (empty($variant_combination)) {
                        db_query("INSERT INTO ?:product_options_inventory ?e", $combination_data);
                    }
                    if (!empty($image_pair_id)) {
                        db_query("UPDATE ?:images_links SET object_id = ?i WHERE pair_id = ?i", $combination_data['combination_hash'], $image_pair_id);
                    }
                    if ($variant_data['amount'] > 0) {
                        $empty_stock = false;
                    }
                }
                // To hide products if they are out of stock
                if ($hide_product == 'Y') {
                    if ($empty_stock) {
                        db_query("UPDATE ?:products SET status = 'H' WHERE product_id = ?i", $pid);
                    }
                }
            }
        } elseif ($import_mode == 'global_option') {
            foreach ($global_options_data as $option => &$global_option_data) {
                $option_id = db_get_field("SELECT options.option_id FROM ?:product_options AS options " . "LEFT JOIN ?:product_options_descriptions AS options_descriptions ON options.option_id = options_descriptions.option_id " . "WHERE options.product_id = 0 AND options_descriptions.option_name = ?s", $option);
                $option_id = empty($option_id) ? 0 : $option_id;
                $option_data = fn_exim_1c_create_option_structure(0, $option, $company_id, $type_option);
                if ($option_id != 0) {
                    $old_variants = db_get_fields("SELECT variants_descriptions.variant_name FROM ?:product_option_variants AS variants " . "LEFT JOIN ?:product_option_variants_descriptions AS variants_descriptions ON variants.variant_id = variants_descriptions.variant_id " . "WHERE variants.option_id = ?i AND variants_descriptions.lang_code = ?s", $option_id, $lang_code);
                    $old_variants = array_diff($old_variants, $global_option_data['variants']);
                    $global_option_data['variants'] = fn_array_merge($global_option_data['variants'], $old_variants);
                }
                foreach ($global_option_data['variants'] as $variant) {
                    $variant_id = db_get_field("SELECT variants.variant_id FROM ?:product_option_variants AS variants " . "LEFT JOIN ?:product_option_variants_descriptions AS variants_descriptions ON variants.variant_id = variants_descriptions.variant_id " . "WHERE variants.option_id = ?i AND variants_descriptions.lang_code = ?s AND variants_descriptions.variant_name = ?s", $option_id, $lang_code, $variant);
                    $variant_id = empty($variant_id) ? 0 : $variant_id;
                    $option_data['variants'][] = array('variant_name' => $variant, 'variant_id' => $variant_id, 'modifier_type' => 'A', 'modifier' => 0, 'weight_modifier' => 0, 'weight_modifier_type' => 'A');
                }
                $option_id = fn_update_product_option($option_data, $option_id, $lang_code);
                $global_option_data['option_id'] = $option_id;
                foreach ($global_option_data['product_ids'] as $product_id) {
                    db_query("REPLACE INTO ?:product_global_option_links ?e", array('option_id' => $option_id, 'product_id' => $product_id));
                }
            }
            foreach ($options_data as $pid => $combinations) {
                foreach ($combinations as $_combination_data) {
                    $add_options_combination = array();
                    foreach ($_combination_data['combination'] as $combination) {
                        $option_id = $global_options_data[$combination['option_name']]['option_id'];
                        $variant_id = db_get_field("SELECT variants_descriptions.variant_id FROM ?:product_option_variants AS variants " . "LEFT JOIN ?:product_option_variants_descriptions AS variants_descriptions ON variants.variant_id = variants_descriptions.variant_id " . "WHERE lang_code = ?s AND option_id = ?i AND variant_name = ?s", $lang_code, $option_id, $combination['variant_name']);
                        $add_options_combination[$option_id] = $variant_id;
                    }
                    $old_combination_hash = db_get_field("SELECT combination_hash FROM ?:product_options_inventory WHERE external_id = ?s", $_combination_data['combination_guid']);
                    $image_pair_id = db_get_field("SELECT pair_id FROM ?:images_links WHERE object_id = ?i", $old_combination_hash);
                    $empty_stock = true;
                    db_query("DELETE FROM ?:product_options_inventory WHERE external_id = ?s AND product_id = ?i", $_combination_data['combination_guid'], $pid);
                    $combination_data = array('product_id' => $pid, 'combination_hash' => fn_generate_cart_id($pid, array('product_options' => $add_options_combination)), 'combination' => fn_get_options_combination($add_options_combination), 'amount' => $_combination_data['amount'], 'external_id' => $_combination_data['combination_guid']);
                    db_query("INSERT INTO ?:product_options_inventory ?e", $combination_data);
                    if (!empty($image_pair_id)) {
                        db_query("UPDATE ?:images_links SET object_id = ?i WHERE pair_id = ?i", $combination_data['combination_hash'], $image_pair_id);
                    }
                    if ($combination_data['amount'] > 0) {
                        $empty_stock = false;
                    }
                }
                // To hide products if they are out of stock
                if ($hide_product == 'Y') {
                    if ($empty_stock) {
                        db_query("UPDATE ?:products SET status = 'H' WHERE product_id = ?i", $pid);
                    }
                }
            }
        } elseif ($import_mode == 'individual_option') {
            foreach ($options_data as $pid => $combinations) {
                foreach ($combinations as $_combination_data) {
                    $add_options_combination = array();
                    foreach ($_combination_data['combination'] as $combination) {
                        $option_id = db_get_field("SELECT options.option_id FROM ?:product_options AS options " . "LEFT JOIN ?:product_options_descriptions AS options_descriptions ON options.option_id = options_descriptions.option_id " . "WHERE options_descriptions.lang_code = ?s AND options_descriptions.option_name = ?s AND options.product_id = ?i", $lang_code, $combination['option_name'], $pid);
                        $option_id = empty($option_id) ? 0 : $option_id;
                        $option_data = fn_exim_1c_create_option_structure($pid, $combination['option_name'], $company_id, $type_option);
                        $option_id = fn_update_product_option($option_data, $option_id, $lang_code);
                        $variant_id = db_get_field("SELECT variants_descriptions.variant_id FROM ?:product_option_variants AS variants " . "LEFT JOIN ?:product_option_variants_descriptions AS variants_descriptions ON variants.variant_id = variants_descriptions.variant_id " . "WHERE variants_descriptions.lang_code = ?s AND variants.option_id = ?i AND variants_descriptions.variant_name = ?s", $lang_code, $option_id, $combination['variant_name']);
                        if (empty($variant_id)) {
                            $variant = array('option_id' => $option_id, 'modifier_type' => 'A', 'modifier' => 0, 'weight_modifier' => 0, 'weight_modifier_type' => 'A');
                            $variant_id = db_query("INSERT INTO ?:product_option_variants ?e", $variant);
                            $variant = array('variant_id' => $variant_id, 'variant_name' => $combination['variant_name'], 'lang_code' => $lang_code);
                            db_query("INSERT INTO ?:product_option_variants_descriptions ?e", $variant);
                        }
                        $add_options_combination[$option_id] = $variant_id;
                    }
                    $old_combination_hash = db_get_field("SELECT combination_hash FROM ?:product_options_inventory WHERE external_id = ?s", $_combination_data['combination_guid']);
                    $image_pair_id = db_get_field("SELECT pair_id FROM ?:images_links WHERE object_id = ?i", $old_combination_hash);
                    $empty_stock = true;
                    db_query("DELETE FROM ?:product_options_inventory WHERE external_id = ?s AND product_id = ?i", $_combination_data['combination_guid'], $pid);
                    $combination_data = array('product_id' => $pid, 'combination_hash' => fn_generate_cart_id($pid, array('product_options' => $add_options_combination)), 'combination' => fn_get_options_combination($add_options_combination), 'amount' => $_combination_data['amount'], 'external_id' => $_combination_data['combination_guid']);
                    db_query("INSERT INTO ?:product_options_inventory ?e", $combination_data);
                    if (!empty($image_pair_id)) {
                        db_query("UPDATE ?:images_links SET object_id = ?i WHERE pair_id = ?i", $combination_data['combination_hash'], $image_pair_id);
                    }
                    if ($combination_data['amount'] > 0) {
                        $empty_stock = false;
                    }
                }
                // To hide products if they are out of stock
                if ($hide_product == 'Y') {
                    if ($empty_stock) {
                        db_query("UPDATE ?:products SET status = 'H' WHERE product_id = ?i", $pid);
                    }
                }
            }
        }
    }
}
Example #3
0
/**
 * Add product to wishlist
 *
 * @param array $product_data array with data for the product to add)(product_id, price, amount, product_options, is_edp)
 * @param array $wishlist wishlist data storage
 * @param array $auth user session data
 * @return mixed array with wishlist IDs for the added products, false otherwise
 */
function fn_add_product_to_wishlist($product_data, &$wishlist, &$auth)
{
    // Check if products have cusom images
    list($product_data, $wishlist) = fn_add_product_options_files($product_data, $wishlist, $auth, false, 'wishlist');
    fn_set_hook('pre_add_to_wishlist', $product_data, $wishlist, $auth);
    if (!empty($product_data) && is_array($product_data)) {
        $wishlist_ids = array();
        foreach ($product_data as $product_id => $data) {
            if (empty($data['amount'])) {
                $data['amount'] = 1;
            }
            if (!empty($data['product_id'])) {
                $product_id = $data['product_id'];
            }
            if (empty($data['extra'])) {
                $data['extra'] = array();
            }
            // Add one product
            if (!isset($data['product_options'])) {
                $data['product_options'] = fn_get_default_product_options($product_id);
            }
            // Generate wishlist id
            $data['extra']['product_options'] = $data['product_options'];
            $_id = fn_generate_cart_id($product_id, $data['extra']);
            //$_SESSION['tessssssssssssssssssssssssssssssssssssssstttttttttttt'] = $data['extra'];
            $_data = db_get_row('SELECT is_edp, options_type, tracking FROM ?:products WHERE product_id = ?i', $product_id);
            $data['is_edp'] = $_data['is_edp'];
            $data['options_type'] = $_data['options_type'];
            $data['tracking'] = $_data['tracking'];
            // Check the sequential options
            if (!empty($data['tracking']) && $data['tracking'] == 'O' && $data['options_type'] == 'S') {
                $inventory_options = db_get_fields("SELECT a.option_id FROM ?:product_options as a LEFT JOIN ?:product_global_option_links as c ON c.option_id = a.option_id WHERE (a.product_id = ?i OR c.product_id = ?i) AND a.status = 'A' AND a.inventory = 'Y'", $product_id, $product_id);
                $sequential_completed = true;
                if (!empty($inventory_options)) {
                    foreach ($inventory_options as $option_id) {
                        if (!isset($data['product_options'][$option_id]) || empty($data['product_options'][$option_id])) {
                            $sequential_completed = false;
                            break;
                        }
                    }
                }
                if (!$sequential_completed) {
                    fn_set_notification('E', __('error'), __('select_all_product_options'));
                    // Even if customer tried to add the product from the catalog page, we will redirect he/she to the detailed product page to give an ability to complete a purchase
                    $redirect_url = fn_url('products.view?product_id=' . $product_id . '&combination=' . fn_get_options_combination($data['product_options']));
                    $_REQUEST['redirect_url'] = $redirect_url;
                    //FIXME: Very very very BAD style to use the global variables in the functions!!!
                    return false;
                }
            }
            $wishlist_ids[] = $_id;
            $wishlist['products'][$_id]['product_id'] = $product_id;
            $wishlist['products'][$_id]['product_options'] = $data['product_options'];
            $wishlist['products'][$_id]['extra'] = $data['extra'];
            $wishlist['products'][$_id]['amount'] = $data['amount'];
        }
        return $wishlist_ids;
    } else {
        return false;
    }
}
Example #4
0
         } else {
             $existent_amount = db_get_field("SELECT amount FROM ?:giftreg_event_products WHERE item_id = ?i AND event_id = ?i", $data['item_id'], $_REQUEST['event_id']);
             if (!empty($data['amount'])) {
                 $data = fn_check_table_fields($data, 'giftreg_event_products');
                 db_query("UPDATE ?:giftreg_event_products SET ?u WHERE item_id = ?i AND event_id = ?i", $data, $item_id, $_REQUEST['event_id']);
             } else {
                 db_query("DELETE FROM ?:giftreg_event_products WHERE item_id = ?i AND event_id = ?i", $item_id, $_REQUEST['event_id']);
             }
         }
     }
     $suffix = ".update?selected_section=products&event_id={$_REQUEST['event_id']}";
 }
 // Add products to the event
 if ($mode == 'add_products') {
     foreach ($_REQUEST['product_data'] as $product_id => $data) {
         $data['item_id'] = fn_generate_cart_id($product_id, array("product_options" => @$data['product_options']), false);
         $existent_amount = db_get_field("SELECT amount FROM ?:giftreg_event_products WHERE item_id = ?i", $data['item_id']);
         if (!empty($data['product_options'])) {
             $data['extra'] = serialize($data['product_options']);
         }
         $data['product_id'] = $product_id;
         $data['event_id'] = $_REQUEST['event_id'];
         if (!empty($data['amount'])) {
             $data['amount'] += $existent_amount;
             $data = fn_check_table_fields($data, 'giftreg_event_products');
             db_query("REPLACE INTO ?:giftreg_event_products ?e", $data);
         }
     }
     $suffix = ".update?selected_section=products&event_id={$_REQUEST['event_id']}";
 }
 // Add new event
Example #5
0
/**
 * Add gift certificate to wishlist
 *
 * @param array $wishlist wishlist data storage
 * @param array $gift_cert_data array with data for the certificate to add
 * @return array array with gift certificate ID and data if addition is successful and empty array otherwise
 */
function fn_add_gift_certificate_to_wishlist(&$wishlist, $gift_cert_data)
{
    if (!empty($gift_cert_data) && is_array($gift_cert_data)) {
        fn_correct_gift_certificate($gift_cert_data);
        // Generate wishlist id
        $gift_cert_wishlist_id = fn_generate_gift_certificate_cart_id($gift_cert_data);
        $wishlist['gift_certificates'][$gift_cert_wishlist_id] = $gift_cert_data;
        $gift_cert_data['display_subtotal'] = $gift_cert_data['amount'];
        if (!empty($gift_cert_data['products'])) {
            $product_data = array();
            foreach ($gift_cert_data['products'] as $w_id => $_data) {
                if (empty($_data['amount'])) {
                    unset($gift_cert_data['products'][$w_id]);
                    continue;
                }
                if (empty($_data['product_options'])) {
                    $_data['product_options'] = fn_get_default_product_options($_data['product_id']);
                }
                $wishlist_id = fn_generate_cart_id($_data['product_id'], array('product_options' => $_data['product_options'], 'parent' => array('certificate' => $gift_cert_wishlist_id)), true);
                $product_data[$wishlist_id] = $_data;
                $wishlist['products'][$wishlist_id] = array('product_id' => $_data['product_id'], 'product_options' => $_data['product_options'], 'amount' => $_data['amount'], 'extra' => array('parent' => array('certificate' => $gift_cert_wishlist_id)));
                $product = fn_get_product_data($_data['product_id'], $_SESSION['auth']);
                $gift_cert_data['display_subtotal'] += $_data['amount'] * $product['price'];
            }
            $gift_cert_data['products'] = $wishlist['gift_certificates'][$gift_cert_wishlist_id]['products'] = $product_data;
        }
        return array($gift_cert_wishlist_id, $gift_cert_data);
    } else {
        return array();
    }
}
 $product['combination_hash'] = fn_generate_cart_id($product['product_id'], $_REQUEST['product_data'][$product['product_id']], true);
 $selected_options_for_hash = array();
 if ($_REQUEST['product_data'][$product['product_id']]['extra']) {
     $selected_options_for_hash["price_calc"] = array("total_price_calc" => (string) $product['price']);
 }
 $product_options_ids_for_hash = array();
 foreach ($product['selected_options'] as $k1 => $v1) {
     $product_options_ids_for_hash[] = $k1;
 }
 $product_options_ids_for_hash_ordered = db_get_array("SELECT option_id FROM ?:product_options WHERE option_id IN (?n) ORDER BY position", $product_options_ids_for_hash);
 foreach ($product_options_ids_for_hash_ordered as $k2 => $v2) {
     $selected_options_for_hash["product_options"][$v2['option_id']] = (string) $product['selected_options'][$v2['option_id']];
 }
 $_SESSION['ls_selected_options']['options'] = $_REQUEST['product_data'][$product['product_id']]['product_options'];
 $_SESSION['ls_selected_options']['product_id'] = $product['product_id'];
 $product['combination_hash_wishlist'] = fn_generate_cart_id($product['product_id'], $selected_options_for_hash);
 Registry::get('view')->assign('ls_post_hash', $product['combination_hash']);
 //get cart products details
 list($ls_total_products, $ls_product_groups) = fn_calculate_cart_content($_SESSION['cart'], $auth, Registry::get('settings.General.estimate_shipping_cost') == 'Y' ? 'A' : 'S', true, 'F', true);
 //copy product info to pass it as reference later
 $ls_current_page_product = array($product['combination_hash'] => $product);
 //copy the db hash
 $ls_current_page_product[$product['combination_hash']]['ls_db_hash'] = $product['combination_hash'];
 //set the product page order amount
 $ls_current_page_product[$product['combination_hash']]['order_amount'] = 1;
 //check to see if this product is already in cart
 if (!fn_is_product_in_cart($ls_current_page_product, $ls_total_products, $product)) {
     //set the product page order amount
     $ls_current_page_product[$product['combination_hash']]['order_amount'] = 1;
     $product['ls_order_amount'] = 1;
     //for template logic to hide the add to cart button
             } else {
                 fn_set_notification('W', __('warning'), __('exception_exist'));
             }
         }
         fn_update_exceptions($_REQUEST['product_id']);
         $suffix = ".exceptions?product_id={$_REQUEST['product_id']}";
     }
     if ($mode == 'm_delete_exceptions') {
         db_query("DELETE FROM ?:product_options_exceptions WHERE exception_id IN (?n)", $_REQUEST['exception_ids']);
         $suffix = ".exceptions?product_id={$_REQUEST['product_id']}";
     }
 }
 if ($mode == 'add_combinations') {
     if (is_array($_REQUEST['add_inventory'])) {
         foreach ($_REQUEST['add_inventory'] as $k => $v) {
             $combination_hash = fn_generate_cart_id($_REQUEST['product_id'], array('product_options' => $_REQUEST['add_options_combination'][$k]));
             $combination = fn_get_options_combination($_REQUEST['add_options_combination'][$k]);
             $product_code = fn_get_product_code($_REQUEST['product_id'], $_REQUEST['add_options_combination'][$k]);
             $_data = array('product_id' => $_REQUEST['product_id'], 'combination_hash' => $combination_hash, 'combination' => $combination, 'product_code' => !empty($product_code) ? $product_code : '');
             $_data = fn_array_merge($v, $_data);
             db_query("REPLACE INTO ?:product_options_inventory ?e", $_data);
         }
     }
     $suffix = ".inventory?product_id={$_REQUEST['product_id']}";
 }
 if ($mode == 'update_combinations') {
     // Updating images
     fn_attach_image_pairs('combinations', 'product_option', 0, CART_LANGUAGE, array());
     $inventory = db_get_hash_array("SELECT * FROM ?:product_options_inventory WHERE product_id = ?i", 'combination_hash', $_REQUEST['product_id']);
     foreach ($inventory as $i) {
         $inventory_ids[] = $i['combination_hash'];
Example #8
0
} elseif ($mode == 'ls_reload_product_data') {
    //get page product details
    //get the product id
    if (!isset($_REQUEST['product_data'][$product['product_id']]['product_id'])) {
        $product_id = reset(array_keys($_REQUEST['product_data']));
        $_REQUEST['product_data'][$product['product_id']]['product_id'] = $product_id;
    } else {
        $product_id = $_REQUEST['product_data'][$product['product_id']]['product_id'];
    }
    //get the combination hash
    $combination_hash = fn_generate_cart_id($product_id, $_REQUEST['product_data'][$product_id], true);
    //check if the combination hash exists
    if (!$combination_hash) {
        //product with no options
        //use cart combination hash
        $combination_hash = fn_generate_cart_id($product['product_id'], $_REQUEST['product_data'][$product['product_id']], false);
        //  echo 'cart combination hash is ' . $product['combination_hash'];
    }
    //copy the product id
    $ls_current_page_product[$combination_hash]['product_id'] = $product_id;
    //assign the db hash
    $ls_current_page_product[$combination_hash]['ls_db_hash'] = $combination_hash;
    //assign the product options
    foreach ($_REQUEST['product_data'][$product_id] as $option_id => $variant_id) {
        $ls_current_page_product[$combination_hash]['product_options'][$option_id]['value'] = $variant_id;
    }
    //tests
    $ls_msg['product_id'] = $product_id;
    $ls_msg['combination_hash'] = $combination_hash;
    //get product trackig and available since
    $ls_current_page_product[$combination_hash]['tracking'] = db_get_field('SELECT tracking FROM ?:products WHERE product_id = ?i', $product_id);
Example #9
0
function fn_add_product_to_cart($product_data, &$cart, &$auth, $update = false)
{
    $ids = array();
    if (!empty($product_data) && is_array($product_data)) {
        if (!defined('GET_OPTIONS')) {
            list($product_data, $cart) = fn_add_product_options_files($product_data, $cart, $auth, $update);
        }
        fn_set_hook('pre_add_to_cart', $product_data, $cart, $auth, $update);
        foreach ($product_data as $key => $data) {
            if (empty($key)) {
                continue;
            }
            if (empty($data['amount'])) {
                continue;
            }
            $data['stored_price'] = !empty($data['stored_price']) && AREA != 'C' ? $data['stored_price'] : 'N';
            if (empty($data['extra'])) {
                $data['extra'] = array();
            }
            $product_id = !empty($data['product_id']) ? $data['product_id'] : $key;
            // Check if product options exist
            if (!isset($data['product_options'])) {
                $data['product_options'] = fn_get_default_product_options($product_id);
            }
            // Generate cart id
            $data['extra']['product_options'] = $data['product_options'];
            $_id = fn_generate_cart_id($product_id, $data['extra'], false);
            if (isset($data['extra']['exclude_from_calculate'])) {
                if (!empty($cart['products'][$key]) && !empty($cart['products'][$key]['extra']['aoc'])) {
                    $cart['saved_product_options'][$cart['products'][$key]['extra']['saved_options_key']] = $data['product_options'];
                }
                if (isset($cart['deleted_exclude_products'][$data['extra']['exclude_from_calculate']][$_id])) {
                    continue;
                }
            }
            $amount = fn_normalize_amount(@$data['amount']);
            if (!isset($data['extra']['exclude_from_calculate'])) {
                if ($data['stored_price'] != 'Y') {
                    // Check if the product price with options modifiers equals to zero
                    $price = fn_get_product_price($product_id, $amount, $auth);
                    $price = fn_apply_options_modifiers($data['product_options'], $price, 'P');
                    if (!floatval($price)) {
                        $data['price'] = isset($data['price']) ? fn_parse_price($data['price']) : 0;
                        $zero_price_action = db_get_field("SELECT zero_price_action FROM ?:products WHERE product_id = ?i", $product_id);
                        if (($zero_price_action == 'R' || $zero_price_action == 'A' && floatval($data['price']) < 0) && AREA == 'C') {
                            if ($zero_price_action == 'A') {
                                fn_set_notification('E', fn_get_lang_var('error'), fn_get_lang_var('incorrect_price_warning'));
                            }
                            continue;
                        }
                        $price = empty($data['price']) ? 0 : $data['price'];
                    }
                } else {
                    $price = empty($data['price']) ? 0 : $data['price'];
                }
            } else {
                $price = 0;
            }
            $data['is_edp'] = db_get_field('SELECT is_edp FROM ?:products WHERE product_id = ?i', $product_id);
            if (!isset($cart['products'][$_id])) {
                // If product doesn't exists in the cart
                $amount = empty($data['original_amount']) ? fn_check_amount_in_stock($product_id, $amount, $data['product_options'], $_id, $data['is_edp'], 0, $cart, $update == true ? $key : 0) : $data['original_amount'];
                if ($amount === false) {
                    continue;
                }
                $cart['products'][$_id]['product_id'] = $product_id;
                $cart['products'][$_id]['amount'] = $amount;
                $cart['products'][$_id]['product_options'] = $data['product_options'];
                $cart['products'][$_id]['price'] = $price;
                $cart['products'][$_id]['stored_price'] = $data['stored_price'];
                fn_define_original_amount($product_id, $_id, $cart['products'][$_id], $data);
                if ($update == true && $key != $_id) {
                    unset($cart['products'][$key]);
                }
            } else {
                // If product is already exist in the cart
                $_initial_amount = empty($cart['products'][$_id]['original_amount']) ? $cart['products'][$_id]['amount'] : $cart['products'][$_id]['original_amount'];
                // If ID changed (options were changed), summ the total amount of old and new products
                if ($update == true && $key != $_id) {
                    $amount += $_initial_amount;
                    unset($cart['products'][$key]);
                }
                $cart['products'][$_id]['amount'] = fn_check_amount_in_stock($product_id, ($update == true ? 0 : $_initial_amount) + $amount, $data['product_options'], $_id, !empty($data['is_edp']) && $data['is_edp'] == 'Y' ? 'Y' : 'N', 0, $cart, $update == true ? $key : 0);
            }
            $cart['products'][$_id]['extra'] = empty($data['extra']) ? array() : $data['extra'];
            $cart['products'][$_id]['stored_discount'] = @$data['stored_discount'];
            if (defined('ORDER_MANAGEMENT')) {
                $cart['products'][$_id]['discount'] = @$data['discount'];
            }
            // Increase product popularity
            if (empty($_SESSION['products_popularity']['added'][$product_id])) {
                $_data = array('product_id' => $product_id, 'added' => 1, 'total' => POPULARITY_ADD_TO_CART);
                db_query("INSERT INTO ?:product_popularity ?e ON DUPLICATE KEY UPDATE added = added + 1, total = total + ?i", $_data, POPULARITY_ADD_TO_CART);
                $_SESSION['products_popularity']['added'][$product_id] = true;
            }
            $company_id = db_get_field("SELECT company_id FROM ?:products WHERE product_id = ?i", $product_id);
            $cart['products'][$_id]['company_id'] = $company_id;
            fn_set_hook('add_to_cart', $cart, $product_id, $_id);
            $ids[$_id] = $product_id;
        }
        $cart['recalculate'] = true;
        return $ids;
    } else {
        return false;
    }
}
Example #10
0
/**
 * Update buy together products
 *
 * @param array $cart Array of cart content and user information necessary for purchase
 * @param array $product_data Array of new products data
 * @param array $auth Array of user authentication data (e.g. uid, usergroup_ids, etc.)
 * @return boolean Always true
 */
function fn_buy_together_update_cart_products_post(&$cart, &$product_data, &$auth)
{
    if (!empty($cart['products'])) {
        foreach ($cart['products'] as $_id => $product) {
            if (!empty($product['extra']['buy_together']) && !empty($product['prev_cart_id']) && $product['prev_cart_id'] != $_id) {
                foreach ($cart['products'] as $aux_id => $aux_product) {
                    if (!empty($aux_product['extra']['parent']['buy_together']) && $aux_product['extra']['parent']['buy_together'] == $product['prev_cart_id']) {
                        $cart['products'][$aux_id]['extra']['parent']['buy_together'] = $_id;
                        $cart['products'][$aux_id]['update_c_id'] = true;
                    }
                }
            }
        }
        foreach ($cart['products'] as $upd_id => $upd_product) {
            if (!empty($upd_product['update_c_id']) && $upd_product['update_c_id'] == true) {
                $new_id = fn_generate_cart_id($upd_product['product_id'], $upd_product['extra'], false);
                if (!isset($cart['products'][$new_id])) {
                    unset($upd_product['update_c_id']);
                    $cart['products'][$new_id] = $upd_product;
                    unset($cart['products'][$upd_id]);
                    foreach ($cart['product_groups'] as $key_group => $group) {
                        if (in_array($upd_id, array_keys($group['products']))) {
                            unset($cart['product_groups'][$key_group]['products'][$upd_id]);
                            $cart['product_groups'][$key_group]['products'][$new_id] = $upd_product;
                        }
                    }
                    // update taxes
                    fn_update_stored_cart_taxes($cart, $upd_id, $new_id, false);
                }
            }
        }
    }
    return true;
}
Example #11
0
/**
 * Updates/Creates options combination
 *
 * @param array $combination_data Combination data
 * @param string $combination_hash Combination hash
 * @return string Combination hash
 */
function fn_update_option_combination($combination_data, $combination_hash = 0)
{
    /**
     * Change parameters for updating options combination
     *
     * @param array  $combination_data Combination data
     * @param string $combination_hash Combination hash
     */
    fn_set_hook('update_option_combination_pre', $combination_data, $combination_hash);
    $inventory_amount = db_get_field('SELECT amount FROM ?:product_options_inventory WHERE combination_hash = ?s', $combination_hash);
    if (empty($combination_hash)) {
        $combination_hash = fn_generate_cart_id($combination_data['product_id'], array('product_options' => $combination_data['combination']));
        $combination = fn_get_options_combination($combination_data['combination']);
        $product_code = fn_get_product_code($combination_data['product_id'], $combination_data['combination']);
        $_data = array('product_id' => $combination_data['product_id'], 'combination_hash' => $combination_hash, 'combination' => $combination, 'product_code' => !empty($product_code) ? $product_code : '', 'amount' => !empty($combination_data['amount']) ? $combination_data['amount'] : 0, 'position' => !empty($combination_data['position']) ? $combination_data['position'] : 0);
        db_query("REPLACE INTO ?:product_options_inventory ?e", $_data);
    } else {
        // Forbid to update options in the existing combination. Only qty/code/pos.
        unset($combination_data['combination']);
        db_query("UPDATE ?:product_options_inventory SET ?u WHERE combination_hash = ?s", $combination_data, $combination_hash);
    }
    if (isset($combination_data['amount']) && $combination_data['amount'] > 0 && $inventory_amount <= 0) {
        fn_send_product_notifications($combination_data['product_id']);
    }
    // Updating images
    fn_attach_image_pairs('combinations', 'product_option');
    /**
     * Makes extra actions after updating options combination
     *
     * @param array  $combination_data Combination data
     * @param string $combination_hash Combination hash
     * @param int    $inventory_amount Previous (before update) inventory amount of the combination
     */
    fn_set_hook('update_option_combination_pre', $combination_data, $combination_hash, $inventory_amount);
    return $combination_hash;
}
Example #12
0
/**
 * Add product to wishlist
 *
 * @param array $product_data array with data for the product to add)(product_id, price, amount, product_options, is_edp)
 * @param array $wishlist wishlist data storage
 * @param array $auth user session data
 * @return mixed array with wishlist IDs for the added products, false otherwise
 */
function fn_add_product_to_wishlist($product_data, &$wishlist, &$auth)
{
    // Check if products have cusom images
    list($product_data, $wishlist) = fn_add_product_options_files($product_data, $wishlist, $auth, false, 'wishlist');
    fn_set_hook('pre_add_to_wishlist', $product_data, $wishlist, $auth);
    if (!empty($product_data) && is_array($product_data)) {
        $wishlist_ids = array();
        foreach ($product_data as $product_id => $data) {
            if (empty($data['amount'])) {
                $data['amount'] = 1;
            }
            if (!empty($data['product_id'])) {
                $product_id = $data['product_id'];
            }
            if (empty($data['extra'])) {
                $data['extra'] = array();
            }
            // Add one product
            if (!isset($data['product_options'])) {
                $data['product_options'] = fn_get_default_product_options($product_id);
            }
            // Generate wishlist id
            $data['extra']['product_options'] = $data['product_options'];
            $wishlist_ids[] = $_id = fn_generate_cart_id($product_id, $data['extra']);
            $wishlist['products'][$_id]['product_id'] = $product_id;
            $wishlist['products'][$_id]['product_options'] = $data['product_options'];
            $wishlist['products'][$_id]['extra'] = $data['extra'];
        }
        return $wishlist_ids;
    } else {
        return false;
    }
}
Example #13
0
 unset($_SESSION['shipping_rates']);
 if (is_array($cart['products'])) {
     $product_data = $_REQUEST['cart_products'];
     list($product_data, $cart) = fn_add_product_options_files($product_data, $cart, $customer_auth, true);
     unset($product_data['custom_files']);
     foreach ($product_data as $k => $v) {
         if (!isset($cart['products'][$k]['extra']['exclude_from_calculate'])) {
             if (empty($v['extra'])) {
                 $v['extra'] = array();
             }
             unset($v['object_id']);
             $amount = fn_normalize_amount($v['amount']);
             $price = fn_get_product_price($v['product_id'], $amount, $customer_auth);
             $v['extra'] = empty($cart['products'][$k]['extra']) ? array() : $cart['products'][$k]['extra'];
             $v['extra']['product_options'] = empty($v['product_options']) ? array() : $v['product_options'];
             $_id = fn_generate_cart_id($v['product_id'], $v['extra']);
             if (!isset($cart['products'][$_id])) {
                 //if combination doesn't exist in the cart
                 $cart['products'][$_id] = $v;
                 $_product = $cart['products'][$k];
                 fn_define_original_amount($v['product_id'], $_id, $cart['products'][$_id], $_product);
                 unset($cart['products'][$k]);
             } elseif ($k != $_id) {
                 // if the combination is exist but differs from the current
                 $amount += $cart['products'][$_id]['amount'];
                 unset($cart['products'][$k]);
             }
             if (empty($amount)) {
                 fn_delete_cart_product($cart, $_id);
                 continue;
             } else {
Example #14
0
 protected function generateCombinations($product)
 {
     $combinations = array();
     if (!empty($product['product_options'])) {
         $options = $product['product_options'];
         $variants = array();
         $variant_ids = array_keys($options);
         foreach ($variant_ids as $key => $option_id) {
             $variants[$key] = array_keys($options[$option_id]['variants']);
         }
         $combinations_ids = fn_get_options_combinations($variant_ids, $variants);
         $combinations = array();
         foreach ($combinations_ids as $key => $combination) {
             $combinations[$key] = array('product_id' => $product['product_id'], 'product_code' => $product['product_code'], 'combination_hash' => fn_generate_cart_id($product['product_id'], array('product_options' => $combination)), 'combination' => $combination, 'amount' => $product['amount'], 'temp' => 'Y', 'position' => 0, 'image_pairs' => reset($product['images']));
         }
     }
     return $combinations;
 }
Example #15
0
        $product['product_options_ids'] = $product_options;
        $product['product_options'] = fn_get_selected_product_options_info($product_options, CART_LANGUAGE);
        $product['price'] = fn_get_product_price($product['product_id'], 1, $auth);
        $product['avail_amount'] = $product['amount'] - $product['ordered_amount'];
        // selected options combination
        $product['product_options_combination'] = fn_get_options_combination($product_options);
        fn_gather_additional_product_data($product, true, false, false, true);
        // If option combination image is exists than replace the main image with it
        $_options = $product_options;
        if (!empty($product['product_options']) && is_array($product['product_options'])) {
            foreach ($product['product_options'] as $_k => $_v) {
                if ($_v['inventory'] == 'N') {
                    unset($_options[$_v['option_id']]);
                }
            }
        }
        $combination_hash = fn_generate_cart_id($product['product_id'], array('product_options' => $_options));
        if (!empty($product['option_image_pairs']) && is_array($product['option_image_pairs'])) {
            foreach ($product['option_image_pairs'] as $key => $opt_im) {
                if ($opt_im['combination_hash'] == $combination_hash) {
                    $product['main_pair'] = $opt_im;
                    continue;
                }
            }
        }
    }
    $view->assign('event_id', $_REQUEST['event_id']);
    $view->assign('event_data', $event_data);
} elseif ($mode == 'access_key') {
    fn_add_breadcrumb(fn_get_lang_var('access_key'));
}
function fn_exim_put_product_combination($product_id, $product_name, $combination_code, $combination, $amount, &$counter, $set_delimiter, $lang_code)
{
    $pair_delimiter = ':';
    if (!empty($combination)) {
        // Get product_id
        $object_id = 0;
        if (!empty($product_id)) {
            $object_exists = db_get_field('SELECT COUNT(*) FROM ?:products WHERE product_id = ?i', $product_id);
            if ($object_exists) {
                $object_id = $product_id;
            }
        }
        if (empty($object_id) && !empty($product_name)) {
            $object_id = db_get_field('SELECT product_id FROM ?:product_descriptions WHERE product = ?s AND lang_code = ?s', $product_name[$lang_code], $lang_code);
        }
        if (empty($object_id)) {
            $counter['S']++;
            return false;
        }
        $options = array();
        $_options = explode($set_delimiter, $combination);
        foreach ($_options as $key => $value) {
            $options[$key][$lang_code] = $value;
        }
        if (!empty($options)) {
            $_combination = array();
            foreach ($options as $option_pair) {
                $pair = explode($pair_delimiter, $option_pair[$lang_code]);
                if (is_array($pair)) {
                    array_walk($pair, 'fn_trim_helper');
                    $option_id = db_get_field("SELECT o.option_id FROM ?:product_options_descriptions as d INNER JOIN ?:product_options as o ON o.option_id = d.option_id AND o.product_id = ?i WHERE d.option_name = ?s AND d.lang_code = ?s LIMIT 1", $object_id, $pair[0], $lang_code);
                    if (empty($option_id)) {
                        // Search for the global product options
                        $option_id = db_get_field("SELECT o.option_id FROM ?:product_options_descriptions as d INNER JOIN ?:product_options as o ON o.option_id = d.option_id AND o.product_id = ?i WHERE d.option_name = ?s AND d.lang_code = ?s LIMIT 1", 0, $pair[0], $lang_code);
                    }
                    $variant_id = db_get_field("SELECT v.variant_id FROM ?:product_option_variants_descriptions as d INNER JOIN ?:product_option_variants as v ON v.variant_id = d.variant_id AND v.option_id = ?i WHERE d.variant_name = ?s AND d.lang_code = ?s LIMIT 1", $option_id, $pair[1], $lang_code);
                    // Checkboxed do not have descriptions
                    if (empty($variant_id) && in_array($pair[1], array('Yes', 'No'))) {
                        $variant_id = db_get_field("SELECT variant_id FROM ?:product_option_variants WHERE option_id = ?i AND position = ?i LIMIT 1", $option_id, $pair[1] == 'Yes' ? 1 : 0);
                    }
                    if (empty($option_id) || empty($variant_id)) {
                        $counter['S']++;
                        return false;
                    }
                    $_combination[$option_id] = $variant_id;
                }
            }
            $combination = fn_get_options_combination($_combination);
            $combination_hash = fn_generate_cart_id($object_id, array('product_options' => $_combination));
            $object_details = db_get_row('SELECT COUNT(*) as count, amount FROM ?:product_options_inventory WHERE combination_hash = ?i AND product_id = ?i', $combination_hash, $object_id);
            $_data = array('product_id' => $object_id, 'product_code' => $combination_code, 'combination_hash' => $combination_hash, 'combination' => $combination, 'amount' => $amount);
            if ($object_details['count']) {
                if ($object_details['amount'] <= 0 && $_data['amount'] > 0) {
                    fn_send_product_notifications($object_id);
                }
                db_query('UPDATE ?:product_options_inventory SET ?u WHERE combination_hash = ?i', $_data, $combination_hash);
                fn_set_progress('echo', __('updating') . ' ' . __('product_combinations') . '...', false);
                $counter['E']++;
            } else {
                db_query('INSERT INTO ?:product_options_inventory ?e', $_data);
                fn_set_progress('echo', __('creating') . ' ' . __('product_combinations') . '...', false);
                $counter['N']++;
            }
            fn_set_progress('echo', '<b>' . $object_id . '</b>.<br />', false);
            return $combination_hash;
        }
    }
    $counter['S']++;
    return false;
}
Example #17
0
 public static function addNewCombination($product_id, $combination_id, $add_options_combination, $import_params, $amount = 0)
 {
     $old_combination_hash = db_get_field("SELECT combination_hash FROM ?:product_options_inventory WHERE external_id = ?s", $combination_id);
     $image_pair_id = db_get_field("SELECT pair_id FROM ?:images_links WHERE object_id = ?i", $old_combination_hash);
     db_query("DELETE FROM ?:product_options_inventory WHERE external_id = ?s AND product_id = ?i", $combination_id, $product_id);
     $combination_data = array('product_id' => $product_id, 'combination_hash' => fn_generate_cart_id($product_id, array('product_options' => $add_options_combination)), 'combination' => fn_get_options_combination($add_options_combination), 'external_id' => $combination_id);
     if (isset($amount)) {
         $combination_data['amount'] = $amount;
         self::addProductOptionException($add_options_combination, $product_id, $import_params, $amount);
     }
     $variant_combination = db_get_field("SELECT combination_hash FROM ?:product_options_inventory WHERE combination_hash = ?i", $combination_data['combination_hash']);
     if (empty($variant_combination)) {
         db_query("REPLACE INTO ?:product_options_inventory ?e", $combination_data);
     } else {
         db_query("UPDATE ?:product_options_inventory SET ?u WHERE combination_hash = ?i", $combination_data, $combination_data['combination_hash']);
     }
     if (!empty($image_pair_id)) {
         db_query("UPDATE ?:images_links SET object_id = ?i WHERE pair_id = ?i", $combination_data['combination_hash'], $image_pair_id);
     }
 }
Example #18
0
function fn_twg_api_add_product_to_cart($products, &$cart)
{
    $products_data = array();
    foreach ($products as $product) {
        $cid = fn_generate_cart_id($product['product_id'], $product);
        if (!empty($products_data[$cid])) {
            $products_data[$cid]['amount'] += $product['amount'];
        }
        // Get product options images
        $product['combination_hash'] = $cid;
        if (!empty($product['combination_hash']) && !empty($product['product_options'])) {
            $image = fn_get_image_pairs($product['combination_hash'], 'product_option', 'M', true, true, CART_LANGUAGE);
            if (!empty($image)) {
                $product['main_pair'] = $image;
            }
        }
        $products_data[$cid] = $product;
    }
    $auth =& $_SESSION['auth'];
    // actions copied from the checkout.php 'add' action
    $ids = fn_add_product_to_cart($products_data, $cart, $auth);
    fn_save_cart_content($cart, $auth['user_id']);
    $cart['change_cart_products'] = true;
    fn_calculate_cart_content($cart, $auth, 'S', true, 'F', true);
    return $ids;
}
Example #19
0
/**
 * Update cart products from passed products data
 *
 * @param array $cart Array of cart content and user information necessary for purchase
 * @param array $product_data Array of new products data
 * @param array $auth Array of user authentication data (e.g. uid, usergroup_ids, etc.)
 * @return boolean Always true
 */
function fn_update_cart_products(&$cart, $product_data, $auth)
{
    /**
     * Actions before updating cart products
     *
     * @param array $cart         Array of cart content and user information necessary for purchase
     * @param array $product_data Array of new products data
     * @param array $auth         Array of user authentication data (e.g. uid, usergroup_ids, etc.)
     */
    fn_set_hook('update_cart_products_pre', $cart, $product_data, $auth);
    if (is_array($cart['products']) && !empty($product_data)) {
        list($product_data, $cart) = fn_add_product_options_files($product_data, $cart, $auth, true);
        unset($product_data['custom_files']);
        foreach ($product_data as $k => $v) {
            if (!isset($cart['products'][$k]['extra']['exclude_from_calculate'])) {
                if (empty($v['extra'])) {
                    $v['extra'] = array();
                }
                if ($v['price'] < 0) {
                    $v['price'] = 0;
                }
                unset($v['object_id']);
                $amount = fn_normalize_amount($v['amount']);
                $price = fn_get_product_price($v['product_id'], $amount, $auth);
                $v['extra'] = empty($cart['products'][$k]['extra']) ? array() : $cart['products'][$k]['extra'];
                $v['extra']['product_options'] = empty($v['product_options']) ? array() : $v['product_options'];
                $_id = fn_generate_cart_id($v['product_id'], $v['extra']);
                if (!isset($cart['products'][$_id])) {
                    //if combination doesn't exist in the cart
                    $cart['products'][$_id] = $v;
                    $cart['products'][$_id]['company_id'] = !empty($cart['products'][$k]['company_id']) ? $cart['products'][$k]['company_id'] : 0;
                    $_product = $cart['products'][$k];
                    fn_define_original_amount($v['product_id'], $_id, $cart['products'][$_id], $_product);
                    fn_delete_cart_product($cart, $k);
                } elseif ($k != $_id) {
                    // if the combination is exist but differs from the current
                    $amount += $cart['products'][$_id]['amount'];
                    fn_delete_cart_product($cart, $k);
                }
                if (empty($amount)) {
                    fn_delete_cart_product($cart, $_id);
                    continue;
                } else {
                    $_product_options = !empty($v['product_options']) ? $v['product_options'] : array();
                    $cart['products'][$_id]['amount'] = fn_check_amount_in_stock($v['product_id'], $amount, $_product_options, $_id, !empty($cart['products'][$_id]['is_edp']) && $cart['products'][$_id]['is_edp'] == 'Y' ? 'Y' : 'N', !empty($cart['products'][$_id]['original_amount']) ? $cart['products'][$_id]['original_amount'] : 0, $cart);
                    if ($cart['products'][$_id]['amount'] == false && !empty($_product)) {
                        $cart['products'][$_id] = $_product;
                        unset($_product);
                    }
                }
                if ($k != $_id) {
                    $cart['products'][$_id]['prev_cart_id'] = $k;
                    // save stored taxes for products
                    fn_update_stored_cart_taxes($cart, $k, $_id, true);
                } elseif (isset($cart['products'][$_id]['prev_cart_id'])) {
                    unset($cart['products'][$_id]['prev_cart_id']);
                }
                $cart['products'][$_id]['stored_price'] = !empty($v['stored_price']) ? $v['stored_price'] : 'N';
                if ($cart['products'][$_id]['stored_price'] == 'Y') {
                    $cart['products'][$_id]['price'] = $v['price'];
                }
                $cart['products'][$_id]['stored_discount'] = !empty($v['stored_discount']) ? $v['stored_discount'] : 'N';
                if ($cart['products'][$_id]['stored_discount'] == 'Y') {
                    $cart['products'][$_id]['discount'] = $v['discount'];
                }
            }
        }
    }
    /**
     * Additional cart products updates from passed params
     *
     * @param array $cart         Array of cart content and user information necessary for purchase
     * @param array $product_data Array of new products data
     * @param array $auth         Array of user authentication data (e.g. uid, usergroup_ids, etc.)
     */
    fn_set_hook('update_cart_products_post', $cart, $product_data, $auth);
    return true;
}
}
$_SESSION['cart'] = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();
$cart =& $_SESSION['cart'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if ($mode == 'update') {
        if (!empty($cart['products'])) {
            foreach ($cart['products'] as $_id => $product) {
                if (!empty($product['extra']['buy_together']) && !empty($product['prev_cart_id']) && $product['prev_cart_id'] != $_id) {
                    foreach ($cart['products'] as $aux_id => $aux_product) {
                        if (!empty($aux_product['extra']['parent']['buy_together']) && $aux_product['extra']['parent']['buy_together'] == $product['prev_cart_id']) {
                            $cart['products'][$aux_id]['extra']['parent']['buy_together'] = $_id;
                            $cart['products'][$aux_id]['update_c_id'] = true;
                        }
                    }
                }
            }
            foreach ($cart['products'] as $upd_id => $upd_product) {
                if (!empty($upd_product['update_c_id']) && $upd_product['update_c_id'] == true) {
                    $new_id = fn_generate_cart_id($upd_product['product_id'], $upd_product['extra'], false);
                    if (!isset($cart['products'][$new_id])) {
                        unset($upd_product['update_c_id']);
                        $cart['products'][$new_id] = $upd_product;
                        unset($cart['products'][$upd_id]);
                        // update taxes
                        fn_update_stored_cart_taxes($cart, $upd_id, $new_id, false);
                    }
                }
            }
        }
    }
}
Example #21
0
function fn_price_list_get_combination($product)
{
    $poptions = $product['product_options'];
    if (!empty($poptions)) {
        if ($product['tracking'] = ProductTracking::TRACK_WITH_OPTIONS) {
            $product['option_inventory'] = db_get_array("SELECT combination_hash as options, amount, product_code FROM ?:product_options_inventory WHERE product_id= ?i", $product['product_id']);
        }
        $product['product_code'] = db_get_field("SELECT product_code FROM ?:products WHERE product_id= ?i", $product['product_id']);
        //Get variants combinations
        $_options = array_keys($poptions);
        foreach ($_options as $key => $option_id) {
            $variants[$key] = array_keys($poptions[$option_id]['variants']);
        }
        $combinations = fn_price_list_build_combination($_options, $variants, '', 0);
        if (!empty($combinations)) {
            foreach ($combinations as $c_id => $c_value) {
                $m_price = 0;
                $m_weight = 0;
                foreach ($c_value as $option_id => $variant_id) {
                    if ($poptions[$option_id]['variants'][$variant_id]['modifier_type'] == 'A') {
                        $m_price += $poptions[$option_id]['variants'][$variant_id]['modifier'];
                        $m_weight += $poptions[$option_id]['variants'][$variant_id]['weight_modifier'];
                    } else {
                        $m_price += $product['base_price'] * $poptions[$option_id]['variants'][$variant_id]['modifier'] / 100;
                        $m_weight += $product['weight'] * $poptions[$option_id]['variants'][$variant_id]['weight_modifier'] / 100;
                    }
                }
                $product['combination_prices'][$c_id] = $product['base_price'] + $m_price;
                $product['combination_weight'][$c_id] = $product['weight'] + $m_weight;
                $amount = $product_code = '';
                if (!empty($product['option_inventory'])) {
                    $hash = fn_generate_cart_id($product['product_id'], array('product_options' => $c_value));
                    foreach ($product['option_inventory'] as $id => $inventory) {
                        if ($inventory['options'] == $hash) {
                            $amount = $inventory['amount'];
                            $product_code = $inventory['product_code'];
                            unset($product['option_inventory'][$id]);
                            break;
                        }
                    }
                }
                $product['combination_amount'][$c_id] = empty($amount) ? $product['amount'] : $amount;
                $product['combination_code'][$c_id] = empty($product_code) ? $product['product_code'] : $product_code;
            }
        }
        $product['combinations'] = $combinations;
    }
    return $product;
}
Example #22
0
function fn_buy_together_pre_add_to_cart(&$product_data, &$cart, &$auth, $update)
{
    if ($update == true) {
        foreach ($product_data as $key => $value) {
            if (!empty($cart['products'][$key]['extra']['buy_together'])) {
                $product_data[$key]['extra']['buy_together'] = $cart['products'][$key]['extra']['buy_together'];
                $product_data[$key]['extra']['buy_id'] = $cart['products'][$key]['extra']['buy_id'];
                $product_data[$key]['extra']['chain'] = $cart['products'][$key]['extra']['chain'];
                $product_data[$key]['extra']['min_qty'] = $cart['products'][$key]['extra']['min_qty'];
                $product_data[$key]['extra']['chain_amount'] = $cart['products'][$key]['extra']['chain_amount'];
                $product_data[$key]['original_amount'] = isset($cart['products'][$key]['original_amount']) ? $cart['products'][$key]['original_amount'] : 0;
                if ($value['amount'] < $cart['products'][$key]['extra']['chain_amount']) {
                    $product_data[$key]['amount'] = $cart['products'][$key]['extra']['chain_amount'];
                }
                if (!empty($value['product_options'])) {
                    $product_data[$key]['extra']['product_options'] = $value['product_options'];
                }
                $cart_id = fn_generate_cart_id($value['product_id'], $product_data[$key]['extra'], false);
                foreach ($cart['products'] as $k => $v) {
                    if (isset($v['extra']['parent']['buy_together']) && $v['extra']['parent']['buy_together'] == $key) {
                        if (isset($v['extra']['min_qty'])) {
                            $min_qty = $v['extra']['min_qty'];
                            $product_data[$k]['extra']['min_qty'] = $min_qty;
                        } else {
                            $min_qty = 1;
                        }
                        if (!empty($v['product_options'])) {
                            $product_data[$k]['product_options'] = $v['product_options'];
                        }
                        $product_data[$k]['product_id'] = $v['product_id'];
                        $product_data[$k]['amount'] = $value['amount'] * $min_qty;
                        $product_data[$k]['extra']['parent']['buy_together'] = $cart_id;
                        $product_data[$key]['buy_together'][$k] = $product_data[$k]['amount'];
                        $product_data[$k]['extra']['chain'] = $cart['products'][$k]['extra']['chain'];
                    }
                }
            }
        }
    } else {
        foreach ($product_data as $key => $value) {
            if (!empty($value['chain'])) {
                // Add a new product chain
                $params['status'] = 'A';
                $params['chain_id'] = $value['chain'];
                $params['simple'] = true;
                $params['full_info'] = true;
                $chain = fn_buy_together_get_chains($params, $auth);
                if (!empty($chain)) {
                    $product_data[$key] = array('product_id' => $chain['product_id'], 'product_options' => empty($value['product_options']) ? $chain['default_options'] : $value['product_options'], 'amount' => $chain['chain_amount'], 'original_amount' => $chain['chain_amount'], 'buy_together' => array(), 'chain_id' => intval($chain['chain_id']));
                    $buy_together = array();
                    foreach ($chain['products'] as $hash => $product) {
                        $product_options = '';
                        if (!empty($product['product_options_short'])) {
                            $product_options = $product['product_options_short'];
                        } elseif (isset($product_data[$product['product_id']]['product_options'])) {
                            $product_options = $product_data[$product['product_id']]['product_options'];
                        }
                        $product_id = $product['product_id'];
                        $product['product_id'] = uniqid();
                        $product_data[$chain['product_id'] . '_' . $chain['chain_id']]['buy_together'][$product['product_id']] = $product_options;
                        $product_data[$product['product_id']] = array('product_id' => $product_id, 'product_options' => $product_options, 'amount' => $product['amount'] * $product_data[$key]['amount'], 'original_amount' => $product['amount'] * $product_data[$key]['amount']);
                        $product_data[$product['product_id']]['extra'] = empty($product_data[$product_id]['extra']) ? array() : $product_data[$product_id]['extra'];
                        $product_data[$product['product_id']]['extra']['chain']['hash'] = $hash;
                        $product_data[$product['product_id']]['extra']['chain']['chain_id'] = $value['chain'];
                        if (!empty($product['options'])) {
                            foreach ($product['options'] as $option_id => $option) {
                                if ($option['option_type'] == 'F') {
                                    $product_data[$product['product_id']]['product_options'][$option_id] = $option_id;
                                }
                            }
                        }
                        unset($product_data[$product_id]);
                        $buy_together[$product['product_id']] = $product['amount'];
                    }
                    if (!empty($product_data[$key]['buy_together'])) {
                        $product_data[$key]['extra']['buy_together'] = $product_data[$key]['buy_together'];
                        $product_data[$key]['extra']['chain']['chain_id'] = $value['chain'];
                        $product_data[$key]['extra']['min_qty'] = $chain['min_qty'];
                        $product_data[$key]['extra']['chain_amount'] = $chain['chain_amount'];
                        if (!empty($product_data[$key]['product_options'])) {
                            $product_data[$key]['extra']['product_options'] = $product_data[$key]['product_options'];
                        }
                        $cart_id = fn_generate_cart_id($key, $product_data[$key]['extra'], false);
                        foreach ($product_data[$key]['buy_together'] as $_product_id => $_options) {
                            $product_data[$_product_id]['extra']['parent']['buy_together'] = $cart_id;
                            $product_data[$_product_id]['extra']['min_qty'] = $buy_together[$_product_id];
                        }
                    }
                    $product_data[$key]['buy_together'] = $buy_together;
                }
                unset($product_data[$key]['chain'], $product_data[$key]['chain_data']);
            }
        }
        fn_set_hook('buy_together_pre_add_to_cart', $product_data, $cart, $auth, $update);
        // Regenerate cart_id if needed
        foreach ($product_data as $key => $value) {
            if (!empty($value['buy_together'])) {
                $cart_id = fn_generate_cart_id($key, $value['extra'], false);
                foreach ($value['buy_together'] as $_product_id => $_options) {
                    $product_data[$_product_id]['extra']['parent']['buy_together'] = $cart_id;
                }
                $product_data[$key]['extra']['buy_id'] = $cart_id;
            }
        }
    }
    if (AREA != 'A') {
        $product_data = fn_buy_together_check_products_amount($product_data, $cart);
    }
}
Example #23
0
/**
 * Get product code by product_id
 *
 * @param int $product_id
 * @param array $product_options
 * @return (string) product code
 */
function fn_get_product_code($product_id, $product_options = array())
{
    if (!empty($product_id)) {
        $tracking = db_get_field("SELECT tracking FROM ?:products WHERE product_id = ?i", $product_id);
        $data['extra']['product_options'] = (array) $product_options;
        $combination_hash = fn_generate_cart_id($product_id, $data['extra']);
        $product_code = db_get_field("SELECT product_code FROM ?:product_options_inventory WHERE combination_hash = ?i AND product_id = ?i", $combination_hash, $product_id);
        if (empty($product_code) || $tracking != 'O') {
            $product_code = db_get_field("SELECT product_code FROM ?:products WHERE product_id = ?i", $product_id);
        }
        return $product_code;
    }
    return '';
}
Example #24
0
function fn_product_configurator_pre_add_to_cart(&$product_data, &$cart, &$auth, $update)
{
    if ($update == true) {
        foreach ($product_data as $key => $value) {
            if (!empty($cart['products'][$key]['extra']['configuration'])) {
                $product_data[$key]['extra']['configuration'] = $cart['products'][$key]['extra']['configuration'];
                if (!empty($value['product_options'])) {
                    $product_data[$key]['extra']['product_options'] = $value['product_options'];
                }
                $cart_id = fn_generate_cart_id($value['product_id'], $product_data[$key]['extra'], false);
                foreach ($cart['products'] as $k => $v) {
                    if (isset($v['extra']['parent']['configuration']) && $v['extra']['parent']['configuration'] == $key) {
                        $product_data[$k] = array('product_id' => $v['product_id'], 'amount' => $value['amount'], 'extra' => array('parent' => array('configuration' => $cart_id)));
                    }
                }
            }
        }
    } else {
        foreach ($product_data as $key => $value) {
            if (!empty($value['cart_id'])) {
                // if we're editing the configuration, just delete it and add new
                fn_delete_cart_product($cart, $value['cart_id']);
            }
            if (!empty($value['configuration'])) {
                $product_data[$key]['extra']['configuration'] = $value['configuration'];
                if (!empty($value['product_options'])) {
                    $product_data[$key]['extra']['product_options'] = $value['product_options'];
                }
                $cart_id = fn_generate_cart_id($key, $product_data[$key]['extra'], false);
                foreach ($value['configuration'] as $group_id => $_product_id) {
                    if (is_array($_product_id)) {
                        foreach ($_product_id as $_id) {
                            if (!isset($product_data[$_id])) {
                                $product_data[$_id] = array();
                                $product_data[$_id]['product_id'] = $_id;
                                $product_data[$_id]['amount'] = $value['amount'];
                                $product_data[$_id]['extra']['parent']['configuration'] = $cart_id;
                            } elseif (isset($product_data[$_id]['extra']['parent']['configuration']) && $product_data[$_id]['extra']['parent']['configuration'] == $cart_id) {
                                $product_data[$_id]['amount'] += $value['amount'];
                            }
                        }
                    } else {
                        if (!isset($product_data[$_product_id])) {
                            $product_data[$_product_id] = array();
                            $product_data[$_product_id]['product_id'] = $_product_id;
                            $product_data[$_product_id]['amount'] = $value['amount'];
                            $product_data[$_product_id]['extra']['parent']['configuration'] = $cart_id;
                        } elseif (isset($product_data[$_product_id]['extra']['parent']['configuration']) && $product_data[$_product_id]['extra']['parent']['configuration'] == $cart_id) {
                            $product_data[$_product_id]['amount'] += $value['amount'];
                        }
                    }
                }
                $product_data[$key]['extra']['configuration_id'] = $cart_id;
            }
        }
    }
}
Example #25
0
function fn_apply_options_rules($product)
{
    /*	Options type:
    			P - simultaneous/parallel
    			S - sequential
    	*/
    // Check for the options and exceptions types
    if (!isset($product['options_type']) || !isset($product['exceptions_type'])) {
        $product = array_merge($product, db_get_row('SELECT options_type, exceptions_type FROM ?:products WHERE product_id = ?i', $product['product_id']));
    }
    // Get the selected options or get the default options
    $product['selected_options'] = empty($product['selected_options']) ? array() : $product['selected_options'];
    $product['options_update'] = $product['options_type'] == 'S' ? true : false;
    // Conver the selected options text to the utf8 format
    if (!empty($product['product_options'])) {
        foreach ($product['product_options'] as $id => $option) {
            if (!empty($option['value'])) {
                $product['product_options'][$id]['value'] = fn_unicode_to_utf8($option['value']);
            }
            if (!empty($product['selected_options'][$option['option_id']])) {
                $product['selected_options'][$option['option_id']] = fn_unicode_to_utf8($product['selected_options'][$option['option_id']]);
            }
        }
    }
    $selected_options =& $product['selected_options'];
    $changed_option = empty($product['changed_option']) ? true : false;
    $simultaneous = array();
    $next = 0;
    foreach ($product['product_options'] as $_id => $option) {
        $simultaneous[$next] = $option['option_id'];
        $next = $option['option_id'];
        if (!empty($option['value'])) {
            $selected_options[$option['option_id']] = $option['value'];
        }
        if (!$changed_option && $product['changed_option'] == $option['option_id']) {
            $changed_option = true;
        }
        if (!empty($selected_options[$option['option_id']]) && ($selected_options[$option['option_id']] == 'checked' || $selected_options[$option['option_id']] == 'unchecked') && $option['option_type'] == 'C') {
            foreach ($option['variants'] as $variant) {
                if ($variant['position'] == 0 && $selected_options[$option['option_id']] == 'unchecked' || $variant['position'] == 1 && $selected_options[$option['option_id']] == 'checked') {
                    $selected_options[$option['option_id']] = $variant['variant_id'];
                    if ($changed_option) {
                        $product['changed_option'] = $option['option_id'];
                    }
                }
            }
        }
        // Check, if the product has any options modifiers
        if (!empty($product['product_options'][$_id]['variants'])) {
            foreach ($product['product_options'][$_id]['variants'] as $variant) {
                if (!empty($variant['modifier']) && floatval($variant['modifier'])) {
                    $product['options_update'] = true;
                }
            }
        }
    }
    if (!empty($product['changed_option']) && empty($selected_options[$product['changed_option']]) && $product['options_type'] == 'S') {
        $product['changed_option'] = array_search($product['changed_option'], $simultaneous);
        if ($product['changed_option'] == 0) {
            unset($product['changed_option']);
            if (!empty($selected_options)) {
                foreach ($selected_options as $option_id => $variant_id) {
                    if (!in_array($option['option_type'], array('I', 'T', 'F'))) {
                        unset($selected_options[$option_id]);
                    }
                }
            }
        }
    }
    if (empty($selected_options) && $product['options_type'] == 'P') {
        $selected_options = fn_get_default_product_options($product['product_id'], true);
    }
    if (empty($product['changed_option'])) {
        end($selected_options);
        $product['changed_option'] = key($selected_options);
    }
    if ($product['options_type'] == 'S') {
        empty($product['changed_option']) ? $allow = 1 : ($allow = 0);
        foreach ($product['product_options'] as $_id => $option) {
            $product['product_options'][$_id]['disabled'] = false;
            if (in_array($option['option_type'], array('I', 'T', 'F'))) {
                continue;
            }
            $option_id = $option['option_id'];
            if ($allow >= 1) {
                unset($selected_options[$option_id]);
                $product['product_options'][$_id]['value'] = '';
            }
            if ($allow >= 2) {
                $product['product_options'][$_id]['disabled'] = true;
                continue;
            }
            if (empty($product['changed_option']) || !empty($product['changed_option']) && $product['changed_option'] == $option_id || $allow > 0) {
                $allow++;
            }
        }
        $product['simultaneous'] = $simultaneous;
    }
    // Restore selected values
    if (!empty($selected_options)) {
        foreach ($product['product_options'] as $_id => $option) {
            if (isset($selected_options[$option['option_id']])) {
                $product['product_options'][$_id]['value'] = $selected_options[$option['option_id']];
            }
        }
    }
    // Change price
    if (empty($product['modifiers_price'])) {
        $product['base_modifier'] = fn_apply_options_modifiers($selected_options, $product['base_price'], 'P');
        $old_price = $product['price'];
        $product['price'] = $product['original_price'] = fn_apply_options_modifiers($selected_options, $product['price'], 'P');
        $product['modifiers_price'] = $product['price'] - $old_price;
    }
    if (!empty($product['list_price'])) {
        $product['list_price'] = fn_apply_options_modifiers($selected_options, $product['list_price'], 'P');
    }
    // Generate combination hash to get images. (Also, if the tracking with options, get amount and product code)
    $combination_hash = fn_generate_cart_id($product['product_id'], array('product_options' => $selected_options), true);
    $product['combination_hash'] = $combination_hash;
    // Change product code and amount
    if (!empty($product['tracking']) && $product['tracking'] == 'O') {
        $combination = db_get_row("SELECT product_code, amount FROM ?:product_options_inventory WHERE combination_hash = ?i", $combination_hash);
        if (!empty($combination['product_code'])) {
            $product['product_code'] = $combination['product_code'];
        }
        if (isset($combination['amount'])) {
            $product['inventory_amount'] = $combination['amount'];
        } else {
            $product['inventory_amount'] = $product['amount'] = 0;
        }
    }
    if (!$product['options_update']) {
        $product['options_update'] = db_get_field('SELECT COUNT(*) FROM ?:product_options_inventory WHERE product_id = ?i', $product['product_id']);
    }
    fn_set_hook('apply_options_rules', $product);
    return $product;
}