function fn_exim_get_product_combination($product_id, $combination, $set_delimiter, $lang_code = CART_LANGUAGE) { $selected_options = fn_get_product_options_by_combination($combination); $options = fn_get_selected_product_options($product_id, $selected_options, $lang_code); $return = array(); if (!empty($options)) { foreach ($options as $option) { if (isset($selected_options[$option['option_id']])) { $return[] = $option['option_name'] . ': ' . $option['variants'][$selected_options[$option['option_id']]]['variant_name']; } } } return implode($set_delimiter, $return); }
function fn_get_product_options_inventory($params, $items_per_page = 0, $lang_code = DESCR_SL) { $default_params = array('page' => 1, 'product_id' => 0, 'items_per_page' => $items_per_page); $params = array_merge($default_params, $params); $limit = ''; if (!empty($params['items_per_page'])) { $params['total_items'] = db_get_field("SELECT COUNT(*) FROM ?:product_options_inventory WHERE product_id = ?i", $params['product_id']); $limit = db_paginate($params['page'], $params['items_per_page']); } $inventory = db_get_array("SELECT * FROM ?:product_options_inventory WHERE product_id = ?i ORDER BY position {$limit}", $params['product_id']); foreach ($inventory as $k => $v) { $inventory[$k]['combination'] = fn_get_product_options_by_combination($v['combination']); $inventory[$k]['image_pairs'] = fn_get_image_pairs($v['combination_hash'], 'product_option', 'M', true, true, $lang_code); $inventory[$k]['additional_image_pairs'] = fn_get_image_pairs($v['combination_hash'], 'product_option', 'A', true, true, $lang_code); } $product_options = fn_get_product_options($params['product_id'], $lang_code, true, true); $product_inventory = db_get_field("SELECT tracking FROM ?:products WHERE product_id = ?i", $params['product_id']); return array($inventory, $params, $product_options, $product_inventory); }
function fn_twg_get_api_product_options_inventory($product_id, $lang_code = CART_LANGUAGE) { $inventory = db_get_array("SELECT *\n FROM ?:product_options_inventory\n WHERE product_id = ?i\n ORDER BY position", $product_id); if (empty($inventory)) { return array(); } $inventory_ids = array(); foreach ($inventory as $k => $v) { $inventory_ids[] = $v['combination_hash']; } $image_pairs = fn_get_image_pairs($inventory_ids, 'product_option', 'M', false, true, $lang_code); foreach ($inventory as $k => $v) { $inventory[$k]['combination'] = array(); $_comb = fn_get_product_options_by_combination($v['combination']); if (!empty($image_pairs[$v['combination_hash']])) { $inventory[$k]['image'] = TwigmoImage::getApiImageData(current($image_pairs[$v['combination_hash']]), 'product_option', 'detailed', $_REQUEST); } foreach ($_comb as $option_id => $variant_id) { $inventory[$k]['combination'][] = array('option_id' => $option_id, 'variant_id' => $variant_id); } } return $inventory; }
/** * Gets default product options * * @param integer $product_id Product identifier * @param bool $get_all Whether to get all the default options or not * @param array $product Product data * @return array The resulting array */ function fn_get_default_product_options($product_id, $get_all = false, $product = array()) { $result = $default = $exceptions = $product_options = array(); $selectable_option_types = array('S', 'R', 'C', 'Y'); /** * Get default product options ( at the beginning of fn_get_default_product_options() ) * * @param integer $product_id Product id * @param bool $get_all Whether to get all the default options or not * @param array $product Product data * @param array $selectable_option_types Selectable option types */ fn_set_hook('get_default_product_options_pre', $product_id, $get_all, $product, $selectable_option_types); if (!fn_allowed_for('ULTIMATE:FREE')) { $exceptions = fn_get_product_exceptions($product_id, true); $exceptions_type = empty($product['exceptions_type']) ? db_get_field('SELECT exceptions_type FROM ?:products WHERE product_id = ?i', $product_id) : $product['exceptions_type']; } $track_with_options = empty($product['tracking']) ? db_get_field("SELECT tracking FROM ?:products WHERE product_id = ?i", $product_id) : $product['tracking']; if (!empty($product['product_options'])) { //filter out only selectable options foreach ($product['product_options'] as $option_id => $option) { if (in_array($option['option_type'], $selectable_option_types)) { $product_options[$option_id] = $option; } } } else { $product_options = fn_get_product_options($product_id, CART_LANGUAGE, true); } if (!empty($product_options)) { foreach ($product_options as $option_id => $option) { if (!empty($option['variants'])) { $default[$option_id] = key($option['variants']); foreach ($option['variants'] as $variant_id => $variant) { $options[$option_id][$variant_id] = true; } } } } else { return array(); } $inventoryOptionsInactive = db_get_array("SELECT option_id, variant_id FROM ?:product_option_variants_disabled WHERE product_id =?i", $product_id); if (!empty($inventoryOptionsInactive)) { foreach ($inventoryOptionsInactive as $inventoryOptionInactive) { unset($product_options[$inventoryOptionInactive['option_id']]['variants'][$inventoryOptionInactive['variant_id']]); } } unset($product_options); if (!fn_allowed_for('ULTIMATE:FREE')) { if (empty($exceptions)) { return $default; } } $inventory_combinations = array(); if ($track_with_options == 'O') { $inventory_combinations = db_get_array("SELECT combination FROM ?:product_options_inventory WHERE product_id = ?i AND amount > 0 AND combination != ''", $product_id); if (!empty($inventory_combinations)) { $_combinations = array(); foreach ($inventory_combinations as $_combination) { $_combinations[] = fn_get_product_options_by_combination($_combination['combination']); } $inventory_combinations = $_combinations; unset($_combinations); } } if (!fn_allowed_for('ULTIMATE:FREE')) { if ($exceptions_type == 'F') { // Forbidden combinations $_options = array_keys($options); $_variants = array_values($options); if (!empty($_variants)) { foreach ($_variants as $key => $variants) { $_variants[$key] = array_keys($variants); } } list($result) = fn_get_allowed_options_combination($_options, $_variants, '', 0, $exceptions, $inventory_combinations); } else { // Allowed combinations foreach ($exceptions as $exception) { $result = array(); foreach ($exception as $option_id => $variant_id) { if (isset($options[$option_id][$variant_id]) || $variant_id == -1) { $result[$option_id] = $variant_id != -1 ? $variant_id : (isset($options[$option_id]) ? key($options[$option_id]) : ''); } else { continue 2; } } $_opt = array_diff_key($options, $result); if (!empty($_opt)) { foreach ($_opt as $option_id => $variants) { $result[$option_id] = key($variants); } } if (empty($inventory_combinations)) { break; } else { foreach ($inventory_combinations as $_icombination) { $_res = array_diff($_icombination, $result); if (empty($_res)) { break 2; } } } } } } /** * Get default product options ( at the end of fn_get_default_product_options() ) * * @param integer $product_id Product id * @param bool $get_all Whether to get all the default options or not * @param array $product Product data * @param array $result The resulting array */ fn_set_hook('get_default_product_options_post', $product_id, $get_all, $product, $result); return empty($result) ? $default : $result; }
public static function importProductOffersFile($data_offers, $import_params) { $cml = self::$cml; $create_prices = self::$s_commerceml['exim_1c_create_prices']; $schema_version = self::$s_commerceml['exim_1c_schema_version']; $import_mode = self::$s_commerceml['exim_1c_import_mode_offers']; $negative_amount = Registry::get('settings.General.allow_negative_amount'); if (isset($data_offers->{$cml}['prices_types']->{$cml}['price_type'])) { $price_offers = self::dataPriceOffers($data_offers->{$cml}['prices_types']->{$cml}['price_type']); if ($create_prices == 'Y') { $data_prices = db_get_array("SELECT price_1c, type, usergroup_id FROM ?:rus_exim_1c_prices"); $prices_commerseml = self::dataPriceFile($data_offers->{$cml}['prices_types']->{$cml}['price_type'], $data_prices); } } $options_data = $global_options_data = array(); foreach ($data_offers->{$cml}['offers']->{$cml}['offer'] as $offer) { $product = array(); $amount = 0; $combination_id = 0; $ids = fn_explode('#', strval($offer->{$cml}['id'])); $guid_product = array_shift($ids); if (!empty($ids)) { $combination_id = reset($ids); } $product_data = db_get_row("SELECT product_id, update_1c, status FROM ?:products WHERE external_id = ?s", $guid_product); $product_id = !empty($product_data['product_id']) ? $product_data['product_id'] : 0; $update_product = !empty($product_data['update_1c']) ? $product_data['update_1c'] : 'N'; if (!self::checkImportPrices($product_data)) { continue; } if (isset($offer->{$cml}['amount']) && !empty($offer->{$cml}['amount'])) { $amount = strval($offer->{$cml}['amount']); } elseif (isset($offer->{$cml}['store'])) { foreach ($offer->{$cml}['store'] as $store) { $amount += strval($store[$cml['in_stock']]); } } $prices = array(); if (isset($offer->{$cml}['prices']) && !empty($price_offers)) { $product_prices = self::conversionProductPrices($offer->{$cml}['prices']->{$cml}['price'], $price_offers); if ($create_prices == 'Y') { $prices = self::dataProductPrice($product_prices, $prices_commerseml); } elseif (!empty($product_prices[strval($offer->{$cml}['prices']->{$cml}['price']->{$cml}['price_id'])]['price'])) { $prices['base_price'] = $product_prices[strval($offer->{$cml}['prices']->{$cml}['price']->{$cml}['price_id'])]['price']; } else { $prices['base_price'] = 0; } } if (empty($prices)) { $prices['base_price'] = 0; } if ($amount < 0 && $negative_amount == 'N') { $amount = 0; } $o_amount = $amount; if (!empty($product_amount[$product_id])) { $o_amount = $o_amount + $product_amount[$product_id]['amount']; } $product_amount[$product_id]['amount'] = $o_amount; $product['status'] = self::updateProductStatus($product_id, $product_data['status'], $product_amount[$product_id]['amount']); if (empty($combination_id)) { $product['amount'] = $amount; db_query("UPDATE ?:products SET ?u WHERE product_id = ?i", $product, $product_id); self::addProductPrice($product_id, $prices); self::addMessageLog('Added product = ' . strval($offer->{$cml}['name']) . ', price = ' . $prices['base_price'] . ' and amount = ' . $amount); } else { $product['tracking'] = 'O'; db_query("UPDATE ?:products SET ?u WHERE product_id = ?i", $product, $product_id); if ($schema_version == '2.07') { self::addProductPrice($product_id, array('base_price' => 0)); $option_id = self::dataProductOption($product_id, $import_params['lang_code']); $variant_id = db_get_field("SELECT variant_id FROM ?:product_option_variants WHERE external_id = ?s AND option_id = ?i", $combination_id, $option_id); if (!empty($option_id) && !empty($variant_id)) { $price = $prices['base_price']; if (self::$s_commerceml['exim_1c_option_price'] == 'Y') { $price = '0.00'; } db_query("UPDATE ?:product_option_variants SET modifier = ?i WHERE variant_id = ?i", $price, $variant_id); $add_options_combination = array($option_id => $variant_id); self::addNewCombination($product_id, $combination_id, $add_options_combination, $import_params, $amount); self::addMessageLog('Added product = ' . strval($offer->{$cml}['name']) . ', option_id = ' . $option_id . ', variant_id = ' . $variant_id . ', price = ' . $prices['base_price'] . ' and amount = ' . $amount); } elseif (empty($variant_id) && $import_mode == 'global_option') { $data_combination = db_get_row("SELECT combination_hash, combination FROM ?:product_options_inventory WHERE external_id = ?s AND product_id = ?i", $combination_id, $product_id); $add_options_combination = fn_get_product_options_by_combination($data_combination['combination']); self::addProductOptionException($add_options_combination, $product_id, $import_params, $amount); if (!empty($data_combination['combination_hash'])) { $image_pair_id = db_get_field("SELECT pair_id FROM ?:images_links WHERE object_id = ?i", $data_combination['combination_hash']); db_query("UPDATE ?:product_options_inventory SET amount = ?i WHERE combination_hash = ?i", $amount, $data_combination['combination_hash']); if (!empty($image_pair_id)) { db_query("UPDATE ?:images_links SET object_id = ?i WHERE pair_id = ?i", $data_combination['combination_hash'], $image_pair_id); } } self::addMessageLog('Added global option product = ' . strval($offer->{$cml}['name']) . ', price = ' . $prices['base_price'] . ' and amount = ' . $amount); } elseif (empty($variant_id) && $import_mode == 'individual_option') { $data_combination = db_get_row("SELECT combination_hash, combination FROM ?:product_options_inventory WHERE external_id = ?s AND product_id = ?i", $combination_id, $product_id); $add_options_combination = fn_get_product_options_by_combination($data_combination['combination']); self::addProductOptionException($add_options_combination, $product_id, $import_params, $amount); if (!empty($data_combination['combination_hash'])) { $image_pair_id = db_get_field("SELECT pair_id FROM ?:images_links WHERE object_id = ?i", $data_combination['combination_hash']); db_query("UPDATE ?:product_options_inventory SET amount = ?i WHERE combination_hash = ?i", $amount, $data_combination['combination_hash']); if (!empty($image_pair_id)) { db_query("UPDATE ?:images_links SET object_id = ?i WHERE pair_id = ?i", $data_combination['combination_hash'], $image_pair_id); } } self::addMessageLog('Added individual option product = ' . strval($offer->{$cml}['name']) . ', price = ' . $prices['base_price'] . ' and amount = ' . $amount); } } else { if (!empty($offer->{$cml}['product_features']->{$cml}['product_feature'])) { $variant_data = array('amount' => $amount); if ($import_mode == 'standart') { self::addProductPrice($product_id, array('base_price' => 0)); $variant_data['price'] = $prices['base_price']; } if (!empty($product_amount[$product_id][$combination_id])) { $amount = $amount + $product_amount[$product_id]['amount']; } $product_amount[$product_id]['amount'] = $amount; self::addProductCombinationsNewSchema($offer->{$cml}['product_features']->{$cml}['product_feature'], $product_id, $import_params, $combination_id, $variant_data); self::addMessageLog('Added option product = ' . strval($offer->{$cml}['name']) . ', price = ' . $prices['base_price'] . ' and amount = ' . $amount); } } if (self::$s_commerceml['exim_1c_option_price'] == 'Y') { self::addProductPrice($product_id, $prices); } } if (empty($import_params['service_exchange'])) { fn_echo(' '); } else { fn_echo(''); } } }
/** * Gets combination data by combination hash * * @param string $combination_hash Combination unique hash * @return array Combination data */ function fn_get_product_options_combination_data($combination_hash, $lang_code = DESCR_SL) { $combination = db_get_row("SELECT * FROM ?:product_options_inventory WHERE combination_hash = ?i", $combination_hash); $combination['combination'] = fn_get_product_options_by_combination($combination['combination']); $combination['image_pairs'] = fn_get_image_pairs($combination['combination_hash'], 'product_option', 'M', true, true, $lang_code); /** * Modifies selected combination data * * @param string $combination_hash Combination unique hash * @param array $combination_hash Combination data */ fn_set_hook('get_product_options_combination_data_post', $combination_hash, $combination); return $combination; }
} return array(CONTROLLER_STATUS_OK, "product_options{$suffix}"); } // // Product options combination inventory tracking // if ($mode == 'inventory') { fn_add_breadcrumb(fn_get_product_name($_REQUEST['product_id']), "products.update?product_id={$_REQUEST['product_id']}&selected_section=options"); // I think this should be removed, not good, must be done on xml menu level Registry::set('navigation.selected_tab', 'catalog'); Registry::set('navigation.subsection', 'products'); $inv_count = db_get_field("SELECT COUNT(*) FROM ?:product_options_inventory WHERE product_id = ?i", $_REQUEST['product_id']); $limit = fn_paginate(empty($_REQUEST['page']) ? 1 : $_REQUEST['page'], $inv_count, Registry::get('settings.Appearance.admin_elements_per_page')); $inventory = db_get_array("SELECT * FROM ?:product_options_inventory WHERE product_id = ?i ORDER BY position {$limit}", $_REQUEST['product_id']); foreach ($inventory as $k => $v) { $inventory[$k]['combination'] = fn_get_product_options_by_combination($v['combination']); $inventory[$k]['image_pairs'] = fn_get_image_pairs($v['combination_hash'], 'product_option', 'M', true, true, DESCR_SL); } $product_options = fn_get_product_options($_REQUEST['product_id'], DESCR_SL, true, true); $view->assign('product_inventory', db_get_field("SELECT tracking FROM ?:products WHERE product_id = ?i", $_REQUEST['product_id'])); $view->assign('product_options', $product_options); $view->assign('inventory', $inventory); // // Product options exceptions // } elseif ($mode == 'exceptions') { fn_add_breadcrumb(fn_get_product_name($_REQUEST['product_id']), "products.update?product_id={$_REQUEST['product_id']}&selected_section=options"); // I think this should be removed, not good, must be done on xml menu level Registry::set('navigation.selected_tab', 'catalog'); Registry::set('navigation.subsection', 'products'); $exceptions = fn_get_product_exceptions($_REQUEST['product_id']);
function fn_get_default_product_options($product_id, $get_all = false) { $result = $default = $exceptions = array(); $exceptions = fn_get_product_exceptions($product_id, true); $exceptions_type = db_get_field('SELECT exceptions_type FROM ?:products WHERE product_id = ?i', $product_id); $track_with_options = db_get_field("SELECT tracking FROM ?:products WHERE product_id = ?i", $product_id); $product_options = fn_get_product_options($product_id, CART_LANGUAGE, true); if (!empty($product_options)) { foreach ($product_options as $option_id => $option) { if (!empty($option['variants'])) { $default[$option_id] = key($option['variants']); foreach ($option['variants'] as $variant_id => $variant) { $options[$option_id][$variant_id] = true; } } } } else { return array(); } unset($product_options); if (empty($exceptions)) { if ($track_with_options == 'O') { $combination = db_get_field("SELECT combination FROM ?:product_options_inventory WHERE product_id = ?i AND amount > 0 AND combination != '' LIMIT 1", $product_id); if (!empty($combination)) { $result = fn_get_product_options_by_combination($combination); } } if (empty($result)) { foreach ((array) $options as $option_id => $variants) { $result[$option_id] = key($variants); } } return $result; } $inventory_combinations = array(); if ($track_with_options == 'O') { $inventory_combinations = db_get_array("SELECT combination FROM ?:product_options_inventory WHERE product_id = ?i AND amount > 0 AND combination != ''", $product_id); if (!empty($inventory_combinations)) { $_combinations = array(); foreach ($inventory_combinations as $_combination) { $_combinations[] = fn_get_product_options_by_combination($_combination['combination']); } $inventory_combinations = $_combinations; unset($_combinations); } } if ($exceptions_type == 'F') { // Forbidden combinations $_options = array_keys($options); $_variants = array_values($options); if (!empty($_variants)) { foreach ($_variants as $key => $variants) { $_variants[$key] = array_keys($variants); } } $combinations = fn_build_options_combinations($_options, $_variants, '', 0); foreach ($combinations as $combination) { $allowed = true; foreach ($exceptions as $exception) { $res = array_diff($exception, $combination); if (empty($res)) { $allowed = false; break; } else { foreach ($res as $option_id => $variant_id) { if ($variant_id == -1) { unset($res[$option_id]); } } if (empty($res)) { $allowed = false; break; } } } if ($allowed) { $result = $combination; if (empty($inventory_combinations)) { break; } else { foreach ($inventory_combinations as $_icombination) { $_res = array_diff($_icombination, $combination); if (empty($_res)) { break 2; } } } } } } else { // Allowed combinations foreach ($exceptions as $exception) { $result = array(); foreach ($exception as $option_id => $variant_id) { if (isset($options[$option_id][$variant_id]) || $variant_id == -1) { $result[$option_id] = $variant_id != -1 ? $variant_id : (isset($options[$option_id]) ? key($options[$option_id]) : ''); } else { continue 2; } } $_opt = array_diff_key($options, $result); if (!empty($_opt)) { foreach ($_opt as $option_id => $variants) { $result[$option_id] = key($variants); } } if (empty($inventory_combinations)) { break; } else { foreach ($inventory_combinations as $_icombination) { $_res = array_diff($_icombination, $result); if (empty($_res)) { break 2; } } } } } return empty($result) ? $default : $result; }