/** * This function clones options to product from a product or from a global option * * @param int $from_product_id Identifier of product from that options are copied * @param int $to_product_id Identifier of product to that options are copied * @param type $from_global_option_id Identifier of the global option or false (if options are copied from product) */ function fn_clone_product_options($from_product_id, $to_product_id, $from_global_option_id = false) { /** * Adds additional actions before poduct options clone * * @param int $from_product_id Identifier of product from that options are copied * @param int $to_product_id Identifier of product to that options are copied * @param int/boolean $from_global_option_id Identifier of the global option or false (if options are copied from product) */ fn_set_hook('clone_product_options_pre', $from_product_id, $to_product_id, $from_global_option_id); // Get all product options assigned to the product $id_req = empty($from_global_option_id) ? db_quote('product_id = ?i', $from_product_id) : db_quote('option_id = ?i', $from_global_option_id); $data = db_get_array("SELECT * FROM ?:product_options WHERE {$id_req}"); $linked = db_get_field("SELECT COUNT(option_id) FROM ?:product_global_option_links WHERE product_id = ?i", $from_product_id); if (!empty($data) || !empty($linked)) { // Get all exceptions for the product if (!empty($from_product_id)) { if (!fn_allowed_for('ULTIMATE:FREE')) { $exceptions = fn_get_product_exceptions($from_product_id); } $inventory = db_get_field("SELECT COUNT(*) FROM ?:product_options_inventory WHERE product_id = ?i", $from_product_id); } // Fill array of options for linked global options options $change_options = array(); $change_varaiants = array(); // If global option are linked than ids will be the same $change_options = db_get_hash_single_array("SELECT option_id FROM ?:product_global_option_links WHERE product_id = ?i", array('option_id', 'option_id'), $from_product_id); if (!empty($change_options)) { foreach ($change_options as $value) { $change_varaiants = fn_array_merge(db_get_hash_single_array("SELECT variant_id FROM ?:product_option_variants WHERE option_id = ?i", array('variant_id', 'variant_id'), $value), $change_varaiants, true); } } foreach ($data as $v) { // Clone main data $option_id = $v['option_id']; $v['product_id'] = $to_product_id; if (fn_allowed_for('ULTIMATE') || fn_allowed_for('MULTIVENDOR')) { $product_company_id = db_get_field("SELECT company_id FROM ?:products WHERE product_id = ?i", $to_product_id); $v['company_id'] = Registry::ifGet('runtime.company_id', $product_company_id); } else { $v['company_id'] = Registry::get('runtime.company_id'); } unset($v['option_id']); $new_option_id = db_query("INSERT INTO ?:product_options ?e", $v); if (fn_allowed_for('ULTIMATE')) { fn_ult_share_product_option($new_option_id, $to_product_id); } // Clone descriptions $_data = db_get_array("SELECT * FROM ?:product_options_descriptions WHERE option_id = ?i", $option_id); foreach ($_data as $_v) { $_v['option_id'] = $new_option_id; db_query("INSERT INTO ?:product_options_descriptions ?e", $_v); } $change_options[$option_id] = $new_option_id; // Clone variants if exists if ($v['option_type'] == 'S' || $v['option_type'] == 'R' || $v['option_type'] == 'C' || $v['option_type'] == 'Y') { $_data = db_get_array("SELECT * FROM ?:product_option_variants WHERE option_id = ?i", $option_id); foreach ($_data as $_v) { $variant_id = $_v['variant_id']; $_v['option_id'] = $new_option_id; unset($_v['variant_id']); $new_variant_id = db_query("INSERT INTO ?:product_option_variants ?e", $_v); if (!fn_allowed_for('ULTIMATE:FREE')) { // Clone Exceptions if (!empty($exceptions)) { fn_clone_options_exceptions($exceptions, $option_id, $variant_id, $new_option_id, $new_variant_id); } } $change_varaiants[$variant_id] = $new_variant_id; // Clone descriptions $__data = db_get_array("SELECT * FROM ?:product_option_variants_descriptions WHERE variant_id = ?i", $variant_id); foreach ($__data as $__v) { $__v['variant_id'] = $new_variant_id; db_query("INSERT INTO ?:product_option_variants_descriptions ?e", $__v); } // Clone variant images fn_clone_image_pairs($new_variant_id, $variant_id, 'variant_image'); } unset($_data, $__data); } /** * Adds additional actions after cloning each product option * * @param int $from_product_id Identifier of product from that options are copied * @param int $to_product_id Identifier of product to that options are copied * @param int/boolean $from_global_option_id Identifier of the global option or false (if options are copied from product) * @param array $v Product option data */ fn_set_hook('clone_product_option_post', $from_product_id, $to_product_id, $from_global_option_id, $v); } // Clone Inventory if (!empty($inventory)) { fn_clone_options_inventory($from_product_id, $to_product_id, $change_options, $change_varaiants); } if (!fn_allowed_for('ULTIMATE:FREE')) { if (!empty($exceptions)) { foreach ($exceptions as $k => $v) { $_data = array('product_id' => $to_product_id, 'combination' => serialize($v['combination'])); db_query("INSERT INTO ?:product_options_exceptions ?e", $_data); } } } } /** * Adds additional actions after poduct options clone * * @param int $from_product_id Identifier of product from that options are copied * @param int $to_product_id Identifier of product to that options are copied * @param int/boolean $from_global_option_id Identifier of the global option or false (if options are copied from product) */ fn_set_hook('clone_product_options_post', $from_product_id, $to_product_id, $from_global_option_id); }
function fn_clone_product_options($from_product_id, $to_product_id, $from_global = false) { // Get all product options assigned to the product $id_req = empty($from_global) ? db_quote('product_id = ?i', $from_product_id) : db_quote('option_id = ?i', $from_global); $data = db_get_array("SELECT * FROM ?:product_options WHERE {$id_req}"); $linked = db_get_field("SELECT COUNT(option_id) FROM ?:product_global_option_links WHERE product_id = ?i", $from_product_id); if (!empty($data) || !empty($linked)) { // Get all exceptions for the product if (!empty($from_product_id)) { $exceptions = fn_get_product_exceptions($from_product_id); $inventory = db_get_field("SELECT COUNT(*) FROM ?:product_options_inventory WHERE product_id = ?i", $from_product_id); } // Fill array of options for linked global options options $change_options = array(); $change_varaiants = array(); // If global option are linked than ids will be the same $change_options = db_get_hash_single_array("SELECT option_id FROM ?:product_global_option_links WHERE product_id = ?i", array('option_id', 'option_id'), $from_product_id); if (!empty($change_options)) { foreach ($change_options as $value) { $change_varaiants = fn_array_merge(db_get_hash_single_array("SELECT variant_id FROM ?:product_option_variants WHERE option_id = ?i", array('variant_id', 'variant_id'), $value), $change_varaiants, true); } } foreach ($data as $v) { // Clone main data $option_id = $v['option_id']; $v['product_id'] = $to_product_id; unset($v['option_id']); $new_option_id = db_query("INSERT INTO ?:product_options ?e", $v); // Clone descriptions $_data = db_get_array("SELECT * FROM ?:product_options_descriptions WHERE option_id = ?i", $option_id); foreach ($_data as $_v) { $_v['option_id'] = $new_option_id; db_query("INSERT INTO ?:product_options_descriptions ?e", $_v); } $change_options[$option_id] = $new_option_id; // Clone variants if exists if ($v['option_type'] == 'S' || $v['option_type'] == 'R' || $v['option_type'] == 'C') { $_data = db_get_array("SELECT * FROM ?:product_option_variants WHERE option_id = ?i", $option_id); foreach ($_data as $_v) { $variant_id = $_v['variant_id']; $_v['option_id'] = $new_option_id; unset($_v['variant_id']); $new_variant_id = db_query("INSERT INTO ?:product_option_variants ?e", $_v); // Clone Exceptions if (!empty($exceptions)) { fn_clone_options_exceptions($exceptions, $option_id, $variant_id, $new_option_id, $new_variant_id); } $change_varaiants[$variant_id] = $new_variant_id; // Clone descriptions $__data = db_get_array("SELECT * FROM ?:product_option_variants_descriptions WHERE variant_id = ?i", $variant_id); foreach ($__data as $__v) { $__v['variant_id'] = $new_variant_id; db_query("INSERT INTO ?:product_option_variants_descriptions ?e", $__v); } // Clone variant images fn_clone_image_pairs($new_variant_id, $variant_id, 'variant_image'); } unset($_data, $__data); } } // Clone Inventory if (!empty($inventory)) { fn_clone_options_inventory($from_product_id, $to_product_id, $change_options, $change_varaiants); } if (!empty($exceptions)) { foreach ($exceptions as $k => $v) { $_data = array('product_id' => $to_product_id, 'combination' => serialize($v['combination'])); db_query("INSERT INTO ?:product_options_exceptions ?e", $_data); } } } }