function fn_update_company($company_data, $company_id = 0, $lang_code = CART_LANGUAGE) { $can_update = true; /** * Update company data (running before fn_update_company() function) * * @param array $company_data Company data * @param int $company_id Company identifier * @param string $lang_code Two-letter language code (e.g. 'en', 'ru', etc.) * @param boolean $can_update Flag, allows addon to forbid to create/update company */ fn_set_hook('update_company_pre', $company_data, $company_id, $lang_code, $can_update); if ($can_update == false) { return false; } if (fn_allowed_for('MULTIVENDOR') && Registry::get('runtime.company_id')) { unset($company_data['comission'], $company_data['comission_type'], $company_data['categories'], $company_data['shippings']); } elseif (fn_allowed_for('ULTIMATE') && Registry::get('runtime.company_id')) { unset($company_data['storefront'], $company_data['secure_storefront']); } if (fn_allowed_for('ULTIMATE') && !Registry::get('runtime.company_id')) { if (isset($company_data['storefront'])) { if (empty($company_data['storefront'])) { fn_set_notification('E', __('error'), __('storefront_url_not_defined')); return false; } else { if (empty($company_data['secure_storefront'])) { $company_data['secure_storefront'] = $company_data['storefront']; } $company_data['storefront'] = Url::clean($company_data['storefront']); $company_data['secure_storefront'] = Url::clean($company_data['secure_storefront']); } } } unset($company_data['company_id']); $_data = $company_data; if (fn_allowed_for('MULTIVENDOR')) { // Check if company with same email already exists $is_exist = db_get_field("SELECT email FROM ?:companies WHERE company_id != ?i AND email = ?s", $company_id, $_data['email']); if (!empty($is_exist)) { $_text = 'error_vendor_exists'; fn_set_notification('E', __('error'), __($_text)); return false; } } if (fn_allowed_for('ULTIMATE') && !empty($company_data['storefront'])) { // Check if company with the same Storefront URL already exists $http_exist = db_get_row('SELECT company_id, storefront FROM ?:companies WHERE storefront = ?s', $company_data['storefront']); $https_exist = db_get_row('SELECT company_id, secure_storefront FROM ?:companies WHERE secure_storefront = ?s', $company_data['secure_storefront']); if (!empty($http_exist) || !empty($https_exist)) { if (empty($company_id)) { if (!empty($http_exist)) { fn_set_notification('E', __('error'), __('storefront_url_already_exists')); } else { fn_set_notification('E', __('error'), __('secure_storefront_url_already_exists')); } return false; } elseif (!empty($http_exist) && $company_id != $http_exist['company_id'] || !empty($https_exist) && $company_id != $https_exist['company_id']) { if (!empty($http_exist) && $company_id != $http_exist['company_id']) { fn_set_notification('E', __('error'), __('storefront_url_already_exists')); unset($_data['storefront']); } else { fn_set_notification('E', __('error'), __('secure_storefront_url_already_exists')); unset($_data['secure_storefront']); } return false; } } } if (isset($company_data['shippings'])) { $_data['shippings'] = empty($company_data['shippings']) ? '' : fn_create_set($company_data['shippings']); } if (!empty($_data['countries_list'])) { $_data['countries_list'] = implode(',', $_data['countries_list']); } else { $_data['countries_list'] = ''; } // add new company if (empty($company_id)) { // company title can't be empty if (empty($company_data['company'])) { fn_set_notification('E', __('error'), __('error_empty_company_name')); return false; } $_data['timestamp'] = TIME; $company_id = db_query("INSERT INTO ?:companies ?e", $_data); if (empty($company_id)) { return false; } $_data['company_id'] = $company_id; foreach (fn_get_translation_languages() as $_data['lang_code'] => $_v) { db_query("INSERT INTO ?:company_descriptions ?e", $_data); } $action = 'add'; // update company information } else { if (isset($company_data['company']) && empty($company_data['company'])) { unset($company_data['company']); } if (!empty($_data['status'])) { $status_from = db_get_field("SELECT status FROM ?:companies WHERE company_id = ?i", $company_id); } db_query("UPDATE ?:companies SET ?u WHERE company_id = ?i", $_data, $company_id); if (isset($status_from) && $status_from != $_data['status']) { fn_companies_change_status($company_id, $_data['status'], '', $status_from, true); } // unset data lang code as it determines company main language not description language unset($_data['lang_code']); db_query("UPDATE ?:company_descriptions SET ?u WHERE company_id = ?i AND lang_code = ?s", $_data, $company_id, $lang_code); $action = 'update'; } /** * Update company data (running after fn_update_company() function) * * @param array $company_data Company data * @param int $company_id Company integer identifier * @param string $lang_code Two-letter language code (e.g. 'en', 'ru', etc.) * @param string $action Flag determines if company was created (add) or just updated (update). */ fn_set_hook('update_company', $company_data, $company_id, $lang_code, $action); $logo_ids = array(); if ($action == 'add') { $theme_name = !empty($company_data['theme_name']) ? $company_data['theme_name'] : Registry::get('config.base_theme'); if (fn_allowed_for('ULTIMATE')) { $clone_from = !empty($company_data['clone_from']) && $company_data['clone_from'] != 'all' ? $company_data['clone_from'] : null; if (!is_null($clone_from)) { $theme_name = fn_get_theme_path('[theme]', 'C', $clone_from); } } if (fn_allowed_for('ULTIMATE')) { $logo_ids = fn_install_theme($theme_name, $company_id, false); } else { $logo_ids = fn_create_theme_logos_by_layout_id($theme_name, 0, $company_id, true); } } fn_attach_image_pairs('logotypes', 'logos', 0, $lang_code, $logo_ids); return $company_id; }
function fn_update_product($product_data, $product_id = 0, $lang_code = CART_LANGUAGE) { $_data = $product_data; if (!empty($product_data['timestamp'])) { $_data['timestamp'] = fn_parse_date($product_data['timestamp']); // Minimal data for product record } if (!empty($product_data['avail_since'])) { $_data['avail_since'] = fn_parse_date($product_data['avail_since']); } if (isset($product_data['tax_ids'])) { $_data['tax_ids'] = empty($product_data['tax_ids']) ? '' : fn_create_set($product_data['tax_ids']); } if (isset($product_data['localization'])) { $_data['localization'] = empty($product_data['localization']) ? '' : fn_implode_localizations($_data['localization']); } if (isset($product_data['usergroup_ids'])) { $_data['usergroup_ids'] = empty($product_data['usergroup_ids']) ? '' : implode(',', $_data['usergroup_ids']); } if (Registry::get('settings.General.allow_negative_amount') == 'N' && isset($_data['amount'])) { $_data['amount'] = abs($_data['amount']); } // add new product if (empty($product_id)) { $create = true; // product title can't be empty if (empty($product_data['product'])) { return false; } $product_id = db_query("INSERT INTO ?:products ?e", $_data); if (empty($product_id)) { return false; } // // Adding same product descriptions for all cart languages // $_data = $product_data; $_data['product_id'] = $product_id; $_data['product'] = trim($_data['product'], " -"); foreach ((array) Registry::get('languages') as $_data['lang_code'] => $_v) { db_query("INSERT INTO ?:product_descriptions ?e", $_data); } // update product } else { if (isset($product_data['product']) && empty($product_data['product'])) { unset($product_data['product']); } db_query("UPDATE ?:products SET ?u WHERE product_id = ?i", $_data, $product_id); $_data = $product_data; if (!empty($_data['product'])) { $_data['product'] = trim($_data['product'], " -"); } db_query("UPDATE ?:product_descriptions SET ?u WHERE product_id = ?i AND lang_code = ?s", $_data, $product_id, $lang_code); } // Log product add/update fn_log_event('products', !empty($create) ? 'create' : 'update', array('product_id' => $product_id)); if (!empty($product_data['product_features'])) { $i_data = array('product_id' => $product_id, 'lang_code' => $lang_code); foreach ($product_data['product_features'] as $feature_id => $value) { // Check if feature is applicable for this product $id_paths = db_get_fields("SELECT ?:categories.id_path FROM ?:products_categories LEFT JOIN ?:categories ON ?:categories.category_id = ?:products_categories.category_id WHERE product_id = ?i", $product_id); $_params = array('category_ids' => array_unique(explode('/', implode('/', $id_paths))), 'feature_id' => $feature_id); list($_feature) = fn_get_product_features($_params); if (empty($_feature)) { $_feature = db_get_field("SELECT description FROM ?:product_features_descriptions WHERE feature_id = ?i AND lang_code = ?s", $feature_id, CART_LANGUAGE); $_product = db_get_field("SELECT product FROM ?:product_descriptions WHERE product_id = ?i AND lang_code = ?s", $product_id, CART_LANGUAGE); fn_set_notification('E', fn_get_lang_var('error'), str_replace(array('[feature_name]', '[product_name]'), array($_feature, $_product), fn_get_lang_var('product_feature_cannot_assigned'))); continue; } $i_data['feature_id'] = $feature_id; unset($i_data['value']); unset($i_data['variant_id']); unset($i_data['value_int']); $feature_type = db_get_field("SELECT feature_type FROM ?:product_features WHERE feature_id = ?i", $feature_id); // Delete variants in current language if ($feature_type == 'T') { db_query("DELETE FROM ?:product_features_values WHERE feature_id = ?i AND product_id = ?i AND lang_code = ?s", $feature_id, $product_id, $lang_code); } else { db_query("DELETE FROM ?:product_features_values WHERE feature_id = ?i AND product_id = ?i", $feature_id, $product_id); } if ($feature_type == 'D') { $i_data['value_int'] = fn_parse_date($value); } elseif ($feature_type == 'M') { if (!empty($product_data['add_new_variant'][$feature_id]['variant'])) { $value = empty($value) ? array() : $value; $value[] = fn_add_feature_variant($feature_id, $product_data['add_new_variant'][$feature_id]); } if (!empty($value)) { foreach ($value as $variant_id) { foreach (Registry::get('languages') as $i_data['lang_code'] => $_d) { // insert for all languages $i_data['variant_id'] = $variant_id; db_query("REPLACE INTO ?:product_features_values ?e", $i_data); } } } continue; } elseif (in_array($feature_type, array('S', 'N', 'E'))) { if (!empty($product_data['add_new_variant'][$feature_id]['variant'])) { $i_data['variant_id'] = fn_add_feature_variant($feature_id, $product_data['add_new_variant'][$feature_id]); } elseif (!empty($value) && $value != 'disable_select') { if ($feature_type == 'N') { $i_data['value_int'] = db_get_field("SELECT variant FROM ?:product_feature_variant_descriptions WHERE variant_id = ?i AND lang_code = ?s", $value, CART_LANGUAGE); } $i_data['variant_id'] = $value; } else { continue; } } else { if ($value == '') { continue; } if ($feature_type == 'O') { $i_data['value_int'] = $value; } else { $i_data['value'] = $value; } } if ($feature_type != 'T') { // feature values are common for all languages, except text (T) foreach (Registry::get('languages') as $i_data['lang_code'] => $_d) { db_query("REPLACE INTO ?:product_features_values ?e", $i_data); } } else { // for text feature, update current language only $i_data['lang_code'] = $lang_code; db_query("INSERT INTO ?:product_features_values ?e", $i_data); } } } // Update product prices if (isset($product_data['price'])) { if (!isset($product_data['prices'])) { $product_data['prices'] = array(); $skip_price_delete = true; } $_price = array('price' => abs($product_data['price']), 'lower_limit' => 1); array_unshift($product_data['prices'], $_price); } if (!empty($product_data['prices'])) { if (empty($skip_price_delete)) { db_query("DELETE FROM ?:product_prices WHERE product_id = ?i", $product_id); } foreach ($product_data['prices'] as $v) { if (!empty($v['lower_limit'])) { $v['product_id'] = $product_id; db_query("REPLACE INTO ?:product_prices ?e", $v); } } } if (!empty($product_data['popularity'])) { $_data = array('product_id' => $product_id, 'total' => intval($product_data['popularity'])); db_query("INSERT INTO ?:product_popularity ?e ON DUPLICATE KEY UPDATE total = ?i", $_data, $product_data['popularity']); } fn_set_hook('update_product', $product_data, $product_id, $lang_code); return $product_id; }
function fn_update_company($company_data, $company_id = 0, $lang_code = CART_LANGUAGE) { $_data = $company_data; // Check if company with same email already exists $is_exist = db_get_field("SELECT email FROM ?:companies WHERE email = ?s AND company_id != ?i", $_data['email'], $company_id); if (!empty($is_exist)) { fn_save_post_data(); $_text = PRODUCT_TYPE == 'MULTIVENDOR' ? 'error_vendor_exists' : 'error_supplier_exists'; fn_set_notification('E', fn_get_lang_var('error'), fn_get_lang_var($_text)); return false; } $_data['shippings'] = empty($company_data['shippings']) ? '' : fn_create_set($company_data['shippings']); // add new company if (empty($company_id)) { // company title can't be empty if (empty($company_data['company'])) { return false; } $company_id = db_query("INSERT INTO ?:companies ?e", $_data); if (empty($company_id)) { return false; } $old_logos = array(); // Adding same company descriptions for all cart languages $_data = array('company_id' => $company_id, 'company_description' => !empty($company_data['company_description']) ? $company_data['company_description'] : ''); if (PRODUCT_TYPE == 'MULTIVENDOR') { foreach ((array) Registry::get('languages') as $_data['lang_code'] => $_v) { db_query("INSERT INTO ?:company_descriptions ?e", $_data); } } // update product } else { if (isset($company_data['company']) && empty($company_data['company'])) { unset($company_data['company']); } db_query("UPDATE ?:companies SET ?u WHERE company_id = ?i", $_data, $company_id); $old_logos = db_get_field("SELECT logos FROM ?:companies WHERE company_id = ?i", $company_id); $old_logos = !empty($old_logos) ? unserialize($old_logos) : array(); if (PRODUCT_TYPE == 'MULTIVENDOR') { // Updating company description $descr = !empty($company_data['company_description']) ? $company_data['company_description'] : ''; db_query("UPDATE ?:company_descriptions SET company_description = ?s WHERE company_id = ?i AND lang_code = ?s", $descr, $company_id, DESCR_SL); } } // Do not upload logo if a dummy company is being added. if (!empty($_data['email'])) { fn_companies_update_logos($company_id, $old_logos); } /* if (empty($product_id)) { $create = true; // product title can't be empty if(empty($product_data['product'])) { return false; } $product_id = db_query("INSERT INTO ?:products ?e", $_data); if (empty($product_id)) { return false; } // // Adding same product descriptions for all cart languages // $_data = $product_data; $_data['product_id'] = $product_id; $_data['product'] = trim($_data['product'], " -"); foreach ((array)Registry::get('languages') as $_data['lang_code'] => $_v) { db_query("INSERT INTO ?:product_descriptions ?e", $_data); } // update product } else { if (isset($product_data['product']) && empty($product_data['product'])) { unset($product_data['product']); } db_query("UPDATE ?:products SET ?u WHERE product_id = ?i", $_data, $product_id); $_data = $product_data; if (!empty($_data['product'])){ $_data['product'] = trim($_data['product'], " -"); } db_query("UPDATE ?:product_descriptions SET ?u WHERE product_id = ?i AND lang_code = ?s", $_data, $product_id, $lang_code); } // Log product add/update fn_log_event('products', !empty($create) ? 'create' : 'update', array( 'product_id' => $product_id )); if (!empty($product_data['product_features'])) { $i_data = array( 'product_id' => $product_id, 'lang_code' => $lang_code ); foreach ($product_data['product_features'] as $feature_id => $value) { // Check if feature is applicable for this product $id_paths = db_get_fields("SELECT ?:categories.id_path FROM ?:products_categories LEFT JOIN ?:categories ON ?:categories.category_id = ?:products_categories.category_id WHERE product_id = ?i", $product_id); $_params = array( 'category_ids' => array_unique(explode('/', implode('/', $id_paths))), 'feature_id' => $feature_id ); list($_feature) = fn_get_product_features($_params); if (empty($_feature)) { $_feature = db_get_field("SELECT description FROM ?:product_features_descriptions WHERE feature_id = ?i AND lang_code = ?s", $feature_id, CART_LANGUAGE); $_product = db_get_field("SELECT product FROM ?:product_descriptions WHERE product_id = ?i AND lang_code = ?s", $product_id, CART_LANGUAGE); fn_set_notification('E', fn_get_lang_var('error'), str_replace(array('[feature_name]', '[product_name]'), array($_feature, $_product), fn_get_lang_var('product_feature_cannot_assigned'))); continue; } $i_data['feature_id'] = $feature_id; unset($i_data['value']); unset($i_data['variant_id']); unset($i_data['value_int']); $feature_type = db_get_field("SELECT feature_type FROM ?:product_features WHERE feature_id = ?i", $feature_id); // Delete variants in current language if ($feature_type == 'T') { db_query("DELETE FROM ?:product_features_values WHERE feature_id = ?i AND product_id = ?i AND lang_code = ?s", $feature_id, $product_id, $lang_code); } else { db_query("DELETE FROM ?:product_features_values WHERE feature_id = ?i AND product_id = ?i", $feature_id, $product_id); } if ($feature_type == 'D') { $i_data['value_int'] = fn_parse_date($value); } elseif ($feature_type == 'M') { if (!empty($product_data['add_new_variant'][$feature_id]['variant'])) { $value = empty($value) ? array() : $value; $value[] = fn_add_feature_variant($feature_id, $product_data['add_new_variant'][$feature_id]); } if (!empty($value)) { foreach ($value as $variant_id) { foreach (Registry::get('languages') as $i_data['lang_code'] => $_d) { // insert for all languages $i_data['variant_id'] = $variant_id; db_query("REPLACE INTO ?:product_features_values ?e", $i_data); } } } continue; } elseif (in_array($feature_type, array('S', 'N', 'E'))) { if (!empty($product_data['add_new_variant'][$feature_id]['variant'])) { $i_data['variant_id'] = fn_add_feature_variant($feature_id, $product_data['add_new_variant'][$feature_id]); } elseif (!empty($value) && $value != 'disable_select') { if ($feature_type == 'N') { $i_data['value_int'] = db_get_field("SELECT variant FROM ?:product_feature_variant_descriptions WHERE variant_id = ?i AND lang_code = ?s", $value, CART_LANGUAGE); } $i_data['variant_id'] = $value; } else { continue; } } else { if ($value == '') { continue; } if ($feature_type == 'O') { $i_data['value_int'] = $value; } else { $i_data['value'] = $value; } } if ($feature_type != 'T') { // feature values are common for all languages, except text (T) foreach (Registry::get('languages') as $i_data['lang_code'] => $_d) { db_query("REPLACE INTO ?:product_features_values ?e", $i_data); } } else { // for text feature, update current language only $i_data['lang_code'] = $lang_code; db_query("INSERT INTO ?:product_features_values ?e", $i_data); } } } // Update product prices if (isset($product_data['price'])) { if (!isset($product_data['prices'])) { $product_data['prices'] = array(); $skip_price_delete = true; } $_price = array ( 'price' => abs($product_data['price']), 'lower_limit' => 1, ); array_unshift($product_data['prices'], $_price); } if (!empty($product_data['prices'])) { if (empty($skip_price_delete)) { db_query("DELETE FROM ?:product_prices WHERE product_id = ?i", $product_id); } foreach ($product_data['prices'] as $v) { if (!empty($v['lower_limit'])) { $v['product_id'] = $product_id; db_query("REPLACE INTO ?:product_prices ?e", $v); } } } if (!empty($product_data['popularity'])) { $_data = array ( 'product_id' => $product_id, 'total' => intval($product_data['popularity']) ); db_query("INSERT INTO ?:product_popularity ?e ON DUPLICATE KEY UPDATE total = ?i", $_data, $product_data['popularity']); } fn_set_hook('update_product', $product_data, $product_id, $lang_code); */ return $company_id; }
/** * Adds or updates product * * @param array $product_data Product data * @param int $product_id Product identifier * @param string $lang_code Two-letter language code (e.g. 'en', 'ru', etc.) * @return mixed ID of created/updated product or false in case of error */ function fn_update_product($product_data, $product_id = 0, $lang_code = CART_LANGUAGE) { $can_update = true; /** * Update product data (running before fn_update_product() function) * * @param array $product_data Product data * @param int $product_id Product identifier * @param string $lang_code Two-letter language code (e.g. 'en', 'ru', etc.) * @param boolean $can_update Flag, allows addon to forbid to create/update product */ fn_set_hook('update_product_pre', $product_data, $product_id, $lang_code, $can_update); if ($can_update == false) { return false; } if (fn_allowed_for('ULTIMATE')) { // check that product owner was not changed by store administrator if (Registry::get('runtime.company_id') || empty($product_data['company_id'])) { $product_company_id = db_get_field('SELECT company_id FROM ?:products WHERE product_id = ?i', $product_id); if (!empty($product_company_id)) { $product_data['company_id'] = $product_company_id; } else { if (Registry::get('runtime.company_id')) { $product_company_id = $product_data['company_id'] = Registry::get('runtime.company_id'); } else { $product_company_id = $product_data['company_id'] = fn_get_default_company_id(); } } } else { $product_company_id = $product_data['company_id']; } if (!empty($product_data['category_ids']) && !fn_check_owner_categories($product_company_id, $product_data['category_ids'])) { fn_set_notification('E', __('error'), __('product_must_have_owner_category')); return false; } if (fn_ult_is_shared_product($product_id) == 'Y') { $_product_id = fn_ult_update_shared_product($product_data, $product_id, Registry::ifGet('runtime.company_id', $product_company_id), $lang_code); } } if (fn_allowed_for('ULTIMATE') && Registry::get('runtime.company_id') && !empty($product_company_id) && Registry::get('runtime.company_id') != $product_company_id && !empty($_product_id)) { $product_id = $_product_id; $create = false; } else { $product_data['updated_timestamp'] = time(); $_data = $product_data; if (!empty($product_data['timestamp'])) { $_data['timestamp'] = fn_parse_date($product_data['timestamp']); // Minimal data for product record } elseif (empty($product_id) || isset($product_data['timestamp'])) { $_data['timestamp'] = time(); } if (empty($product_id) && Registry::get('runtime.company_id')) { $_data['company_id'] = Registry::get('runtime.company_id'); } if (!empty($product_data['avail_since'])) { $_data['avail_since'] = fn_parse_date($product_data['avail_since']); } if (isset($product_data['tax_ids'])) { $_data['tax_ids'] = empty($product_data['tax_ids']) ? '' : fn_create_set($product_data['tax_ids']); } if (isset($product_data['localization'])) { $_data['localization'] = empty($product_data['localization']) ? '' : fn_implode_localizations($_data['localization']); } if (isset($product_data['usergroup_ids'])) { $_data['usergroup_ids'] = empty($product_data['usergroup_ids']) ? '0' : implode(',', $_data['usergroup_ids']); } if (!empty($product_data['list_qty_count']) && $product_data['list_qty_count'] < 0) { $_data['list_qty_count'] = 0; } if (!empty($product_data['qty_step']) && $product_data['qty_step'] < 0) { $_data['qty_step'] = 0; } if (!empty($product_data['min_qty'])) { $_data['min_qty'] = fn_ceil_to_step(abs($product_data['min_qty']), $_data['qty_step']); } if (!empty($product_data['max_qty'])) { $_data['max_qty'] = fn_ceil_to_step(abs($product_data['max_qty']), $_data['qty_step']); } if (Registry::get('settings.General.inventory_tracking') == "N" && isset($_data['tracking'])) { unset($_data['tracking']); } if (Registry::get('settings.General.allow_negative_amount') == 'N' && isset($_data['amount'])) { $_data['amount'] = abs($_data['amount']); } $shipping_params = array(); if (!empty($product_id)) { $shipping_params = db_get_field('SELECT shipping_params FROM ?:products WHERE product_id = ?i', $product_id); if (!empty($shipping_params)) { $shipping_params = unserialize($shipping_params); } } // Save the product shipping params $_shipping_params = array('min_items_in_box' => isset($_data['min_items_in_box']) ? intval($_data['min_items_in_box']) : (!empty($shipping_params['min_items_in_box']) ? $shipping_params['min_items_in_box'] : 0), 'max_items_in_box' => isset($_data['max_items_in_box']) ? intval($_data['max_items_in_box']) : (!empty($shipping_params['max_items_in_box']) ? $shipping_params['max_items_in_box'] : 0), 'box_length' => isset($_data['box_length']) ? intval($_data['box_length']) : (!empty($shipping_params['box_length']) ? $shipping_params['box_length'] : 0), 'box_width' => isset($_data['box_width']) ? intval($_data['box_width']) : (!empty($shipping_params['box_width']) ? $shipping_params['box_width'] : 0), 'box_height' => isset($_data['box_height']) ? intval($_data['box_height']) : (!empty($shipping_params['box_height']) ? $shipping_params['box_height'] : 0)); $_data['shipping_params'] = serialize($_shipping_params); unset($_shipping_params); // add new product if (empty($product_id)) { $create = true; $product_data['create'] = true; // product title can't be empty and not set product_id if (empty($product_data['product']) || !empty($product_data['product_id'])) { fn_set_notification('E', __('error'), __('need_product_name')); return false; } $product_id = db_query("INSERT INTO ?:products ?e", $_data); if (empty($product_id)) { $product_id = false; } // // Adding same product descriptions for all cart languages // $_data = $product_data; $_data['product_id'] = $product_id; $_data['product'] = trim($_data['product'], " -"); foreach (fn_get_translation_languages() as $_data['lang_code'] => $_v) { db_query("INSERT INTO ?:product_descriptions ?e", $_data); } // update product } else { $create = false; if (isset($product_data['product']) && empty($product_data['product'])) { unset($product_data['product']); } $old_product_data = fn_get_product_data($product_id, $auth, $lang_code, '', false, false, false, false); if (isset($old_product_data['amount']) && isset($_data['amount']) && $old_product_data['amount'] <= 0 && $_data['amount'] > 0) { fn_send_product_notifications($product_id); } $arow = db_query("UPDATE ?:products SET ?u WHERE product_id = ?i", $_data, $product_id); $_data = $product_data; if (!empty($_data['product'])) { $_data['product'] = trim($_data['product'], " -"); } db_query("UPDATE ?:product_descriptions SET ?u WHERE product_id = ?i AND lang_code = ?s", $_data, $product_id, $lang_code); if ($arow === false) { fn_set_notification('E', __('error'), __('object_not_found', array('[object]' => __('product'))), '', '404'); $product_id = false; } } if ($product_id) { // Log product add/update fn_log_event('products', !empty($create) ? 'create' : 'update', array('product_id' => $product_id)); // Update product features value $product_data['product_features'] = !empty($product_data['product_features']) ? $product_data['product_features'] : array(); $product_data['add_new_variant'] = !empty($product_data['add_new_variant']) ? $product_data['add_new_variant'] : array(); fn_update_product_features_value($product_id, $product_data['product_features'], $product_data['add_new_variant'], $lang_code); fn_attach_image_pairs('p_feature_var_extra', 'p_feature_var_extra', 0, $lang_code); // Update product prices $product_data = fn_update_product_prices($product_id, $product_data); if (!empty($product_data['popularity'])) { $_data = array('product_id' => $product_id, 'total' => intval($product_data['popularity'])); db_query("INSERT INTO ?:product_popularity ?e ON DUPLICATE KEY UPDATE total = ?i", $_data, $product_data['popularity']); } fn_update_product_categories($product_id, $product_data); // Update main images pair fn_attach_image_pairs('product_main', 'product', $product_id, $lang_code); // Update additional images fn_attach_image_pairs('product_additional', 'product', $product_id, $lang_code); // Adding new additional images fn_attach_image_pairs('product_add_additional', 'product', $product_id, $lang_code); if (fn_allowed_for('ULTIMATE')) { fn_check_and_update_product_sharing($product_id); } } } /** * Update product data (running after fn_update_product() function) * * @param array $product_data Product data * @param int $product_id Product integer identifier * @param string $lang_code Two-letter language code (e.g. 'en', 'ru', etc.) * @param boolean $create Flag determines if product was created (true) or just updated (false). */ fn_set_hook('update_product_post', $product_data, $product_id, $lang_code, $create); return (int) $product_id; }
/** * Create/Update shipping name * * @param array $shipping_data shipping info * @param int $shipping_id shipping identifier * @param string $lang_code 2-letter language code (e.g. 'en', 'ru', etc.) * @return string Shipping name if shipping identifier is not null; false otherwise */ function fn_update_shipping($shipping_data, $shipping_id, $lang_code = DESCR_SL) { if (!empty($shipping_data['shipping']) || !empty($shipping_id)) { $shipping_data['localization'] = empty($shipping_data['localization']) ? '' : fn_implode_localizations($shipping_data['localization']); $shipping_data['tax_ids'] = !empty($shipping_data['tax_ids']) ? fn_create_set($shipping_data['tax_ids']) : ''; $shipping_data['usergroup_ids'] = empty($shipping_data['usergroup_ids']) ? USERGROUP_ALL : (is_array($shipping_data['usergroup_ids']) ? implode(',', $shipping_data['usergroup_ids']) : $shipping_data['usergroup_ids']); unset($shipping_data['shipping_id']); if (isset($shipping_data['rate_calculation']) && $shipping_data['rate_calculation'] == 'M') { $shipping_data['service_id'] = 0; $shipping_data['service_params'] = array(); } if (isset($shipping_data['service_params'])) { $shipping_data['service_params'] = serialize($shipping_data['service_params']); } fn_set_hook('update_shipping', $shipping_data, $shipping_id, $lang_code); if (!empty($shipping_id)) { $action = 'update'; $arow = db_query("UPDATE ?:shippings SET ?u WHERE shipping_id = ?i", $shipping_data, $shipping_id); db_query("UPDATE ?:shipping_descriptions SET ?u WHERE shipping_id = ?i AND lang_code = ?s", $shipping_data, $shipping_id, $lang_code); if ($arow === false) { fn_set_notification('E', __('error'), __('object_not_found', array('[object]' => __('shipping'))), '', '404'); $shipping_id = false; } } else { $action = 'add'; $shipping_id = $shipping_data['shipping_id'] = db_query("INSERT INTO ?:shippings ?e", $shipping_data); foreach (fn_get_translation_languages() as $shipping_data['lang_code'] => $_v) { db_query("INSERT INTO ?:shipping_descriptions ?e", $shipping_data); } } fn_set_hook('update_shipping_post', $shipping_data, $shipping_id, $lang_code, $action); if ($shipping_id) { fn_attach_image_pairs('shipping', 'shipping', $shipping_id, $lang_code); if (!empty($shipping_data['rates'])) { fn_update_shipping_rates($shipping_data, $shipping_id); } } } return $shipping_id; }
/** * Assign localizations to the product * * @param int $product_id Product ID * @param string $data - comma delimited list of localizations * @return boolean always true */ function fn_exim_set_localizations($product_id, $data) { if (empty($data)) { db_query("UPDATE ?:products SET localization = ''"); return true; } $multi_lang = array_keys($data); $main_lang = reset($multi_lang); $loc_ids = db_get_fields("SELECT localization_id FROM ?:localization_descriptions WHERE localization IN (?a) AND lang_code = ?s", fn_explode(',', $data[$main_lang]), $main_lang); $_data = array('localization' => fn_create_set($loc_ids)); db_query('UPDATE ?:products SET ?u WHERE product_id = ?i', $_data, $product_id); return true; }
function fn_update_shipping($data, $shipping_id, $lang_code = DESCR_SL) { $data['localization'] = empty($data['localization']) ? '' : fn_implode_localizations($data['localization']); $data['tax_ids'] = !empty($data['tax_ids']) ? fn_create_set($data['tax_ids']) : ''; $data['usergroup_ids'] = empty($data['usergroup_ids']) ? '' : implode(',', $data['usergroup_ids']); fn_set_hook('update_shipping', $data, $shipping_id, $lang_code); if (!empty($shipping_id)) { db_query("UPDATE ?:shippings SET ?u WHERE shipping_id = ?i", $data, $shipping_id); db_query("UPDATE ?:shipping_descriptions SET ?u WHERE shipping_id = ?i AND lang_code = ?s", $data, $shipping_id, $lang_code); } else { $shipping_id = $data['shipping_id'] = db_query("INSERT INTO ?:shippings ?e", $data); foreach ((array) Registry::get('languages') as $data['lang_code'] => $_v) { db_query("INSERT INTO ?:shipping_descriptions ?e", $data); } } if ($shipping_id) { fn_attach_image_pairs('shipping', 'shipping', $shipping_id, $lang_code); } return $shipping_id; }
/** * Order placing function * * @param array $cart * @param array $auth * @param string $action * @return int order_id or bool FALSE */ function fn_place_order(&$cart, &$auth, $action = '', $parent_order_id = 0) { $allow = true; fn_set_hook('pre_place_order', $cart, $allow); if ($allow == true && !fn_cart_is_empty($cart)) { $ip = fn_get_ip(); $__order_status = STATUS_INCOMPLETED_ORDER; $order = fn_check_table_fields($cart, 'orders'); $order = fn_array_merge($order, fn_check_table_fields($cart['user_data'], 'orders')); // filter hidden fields, which were hidden to checkout fn_filter_hidden_profile_fields($order, 'O'); // If the contact information fields were disabled, fill the information from the billing/shipping Registry::get('settings.General.address_position') == 'billing_first' ? $address_zone = 'b' : ($address_zone = 's'); if (!empty($order['firstname']) || !empty($order[$address_zone . '_firstname'])) { $order['firstname'] = empty($order['firstname']) && !empty($order[$address_zone . '_firstname']) ? $order[$address_zone . '_firstname'] : $order['firstname']; } if (!empty($order['lastname']) || !empty($order[$address_zone . '_lastname'])) { $order['lastname'] = empty($order['lastname']) && !empty($order[$address_zone . '_lastname']) ? $order[$address_zone . '_lastname'] : $order['lastname']; } if (!empty($order['phone']) || !empty($order[$address_zone . '_phone'])) { $order['phone'] = empty($order['phone']) && !empty($order[$address_zone . '_phone']) ? $order[$address_zone . '_phone'] : $order['phone']; } $order['user_id'] = $auth['user_id']; $order['timestamp'] = TIME; $order['lang_code'] = CART_LANGUAGE; $order['tax_exempt'] = $auth['tax_exempt']; $order['status'] = STATUS_INCOMPLETED_ORDER; // incomplete by default to increase inventory $order['ip_address'] = $ip['host']; $cart['companies'] = fn_get_products_companies($cart['products']); $order['is_parent_order'] = 'N'; if (PRODUCT_TYPE == 'MULTIVENDOR') { $order['parent_order_id'] = $parent_order_id; if (count($cart['companies']) > 1) { $order['is_parent_order'] = 'Y'; $__order_status = $order['status'] = STATUS_PARENT_ORDER; } else { $order['company_id'] = key($cart['companies']); } } $order['promotions'] = serialize(!empty($cart['promotions']) ? $cart['promotions'] : array()); if (!empty($cart['promotions'])) { $order['promotion_ids'] = implode(', ', array_keys($cart['promotions'])); } $order['shipping_ids'] = !empty($cart['shipping']) ? fn_create_set(array_keys($cart['shipping'])) : ''; if (!empty($cart['payment_surcharge'])) { $cart['total'] += $cart['payment_surcharge']; $order['total'] = $cart['total']; } if (!empty($cart['payment_info'])) { $ccards = fn_get_static_data_section('C', true); if (!empty($cart['payment_info']['card']) && !empty($ccards[$cart['payment_info']['card']])) { // Check if cvv2 number required and unset it if not if ($ccards[$cart['payment_info']['card']]['param_2'] != 'Y') { unset($cart['payment_info']['cvv2']); } // Check if start date exists and required and convert it to string if ($ccards[$cart['payment_info']['card']]['param_3'] != 'Y') { unset($cart['payment_info']['start_year'], $cart['payment_info']['start_month']); } // Check if issue number required if ($ccards[$cart['payment_info']['card']]['param_4'] != 'Y') { unset($cart['payment_info']['issue_number']); } } } // We're editing existing order if (!empty($order['order_id']) && $order['is_parent_order'] != 'Y') { $_tmp = db_get_row("SELECT status, ip_address, details, timestamp, lang_code FROM ?:orders WHERE order_id = ?i", $order['order_id']); $order['ip_address'] = $_tmp['ip_address']; // Leave original customers IP address $order['details'] = $_tmp['details']; // Leave order details $order['timestamp'] = $_tmp['timestamp']; // Leave the original date $order['lang_code'] = $_tmp['lang_code']; // Leave the original language if ($action == 'save') { $__order_status = $_tmp['status']; // Get the original order status } fn_change_order_status($order['order_id'], STATUS_INCOMPLETED_ORDER, $_tmp['status'], fn_get_notification_rules(array(), false)); // incomplete the order to increase inventory amount. db_query("DELETE FROM ?:orders WHERE order_id = ?i", $order['order_id']); db_query("DELETE FROM ?:order_details WHERE order_id = ?i", $order['order_id']); db_query("DELETE FROM ?:profile_fields_data WHERE object_id = ?i AND object_type = 'O'", $order['order_id']); db_query("DELETE FROM ?:order_data WHERE order_id = ?i AND type IN ('T', 'C', 'P')", $order['order_id']); fn_set_hook('edit_place_order', $order['order_id']); } if (!empty($cart['rewrite_order_id'])) { $order['order_id'] = array_shift($cart['rewrite_order_id']); } $order['referer'] = $_SESSION['referer']; $order_id = db_query("INSERT INTO ?:orders ?e", $order); // Log order creation fn_log_event('orders', 'create', array('order_id' => $order_id)); fn_store_profile_fields($cart['user_data'], $order_id, 'O'); $order['order_id'] = $order_id; // If customer is not logged in, store order ids in the session if (empty($auth['user_id'])) { $auth['order_ids'][] = $order_id; } // Add order details data if (!empty($order_id)) { if (!empty($cart['products'])) { foreach ((array) $cart['products'] as $k => $v) { $product_code = ''; $extra = empty($v['extra']) ? array() : $v['extra']; $v['discount'] = empty($v['discount']) ? 0 : $v['discount']; $extra['product'] = empty($v['product']) ? fn_get_product_name($v['product_id']) : $v['product']; $extra['company_id'] = $v['company_id']; if (isset($v['is_edp'])) { $extra['is_edp'] = $v['is_edp']; } if (isset($v['edp_shipping'])) { $extra['edp_shipping'] = $v['edp_shipping']; } if (!empty($v['discount'])) { $extra['discount'] = $v['discount']; } if (isset($v['base_price'])) { $extra['base_price'] = floatval($v['base_price']); } if (!empty($v['promotions'])) { $extra['promotions'] = $v['promotions']; } if (!empty($v['stored_price']) && $v['stored_price'] == 'Y') { $extra['stored_price'] = 'Y'; } if (!empty($v['product_options'])) { $_options = fn_get_product_options($v['product_id']); if (!empty($_options)) { foreach ($_options as $option_id => $option) { if (!isset($v['product_options'][$option_id])) { $v['product_options'][$option_id] = ''; } } } $extra['product_options'] = $v['product_options']; $cart_id = fn_generate_cart_id($v['product_id'], array('product_options' => $v['product_options']), true); $tracking = db_get_field("SELECT tracking FROM ?:products WHERE product_id = ?i", $v['product_id']); if ($tracking == 'O') { $product_code = db_get_field("SELECT product_code FROM ?:product_options_inventory WHERE combination_hash = ?i", $cart_id); } $extra['product_options_value'] = fn_get_selected_product_options_info($v['product_options']); } else { $v['product_options'] = array(); } if (empty($product_code)) { $product_code = db_get_field("SELECT product_code FROM ?:products WHERE product_id = ?i", $v['product_id']); } // Check the cart custom files if (isset($extra['custom_files'])) { $dir_path = DIR_CUSTOM_FILES . 'order_data/' . $order_id; $sess_dir_path = DIR_CUSTOM_FILES . 'sess_data'; if (!is_dir($dir_path)) { fn_mkdir($dir_path); } foreach ($extra['custom_files'] as $option_id => $files) { if (is_array($files)) { foreach ($files as $file_id => $file) { $file['path'] = $sess_dir_path . '/' . basename($file['path']); fn_copy($file['path'], $dir_path . '/' . $file['file']); fn_rm($file['path']); fn_rm($file['path'] . '_thumb'); $extra['custom_files'][$option_id][$file_id]['path'] = $dir_path . '/' . $file['file']; } } } } $order_details = array('item_id' => $k, 'order_id' => $order_id, 'product_id' => $v['product_id'], 'product_code' => $product_code, 'price' => !empty($v['stored_price']) && $v['stored_price'] == 'Y' ? $v['price'] - $v['discount'] : $v['price'], 'amount' => $v['amount'], 'extra' => serialize($extra)); db_query("INSERT INTO ?:order_details ?e", $order_details); // Increase product popularity $_data = array('product_id' => $v['product_id'], 'bought' => 1, 'total' => POPULARITY_BUY); db_query("INSERT INTO ?:product_popularity ?e ON DUPLICATE KEY UPDATE bought = bought + 1, total = total + ?i", $_data, POPULARITY_BUY); } } // Save shipping information if (!empty($cart['shipping'])) { if ($_SESSION['selfService']) { foreach ($cart['shipping'] as $sh_id => $_d) { $cart['shipping'][$sh_id]['selfService'] = $_SESSION['selfService']; } } // Get carriers and tracking number $data = db_get_field("SELECT data FROM ?:order_data WHERE order_id = ?i AND type = 'L'", $order_id); if (!empty($data)) { $data = unserialize($data); foreach ($cart['shipping'] as $sh_id => $_d) { if (!empty($data[$sh_id]['carrier'])) { $cart['shipping'][$sh_id]['carrier'] = $data[$sh_id]['carrier']; } if (!empty($data[$sh_id]['tracking_number'])) { $cart['shipping'][$sh_id]['tracking_number'] = $data[$sh_id]['tracking_number']; } } } $_data = array('order_id' => $order_id, 'type' => 'L', 'data' => serialize($cart['shipping'])); db_query("REPLACE INTO ?:order_data ?e", $_data); } // Save taxes if (!empty($cart['taxes'])) { $_data = array('order_id' => $order_id, 'type' => 'T', 'data' => serialize($cart['taxes'])); db_query("REPLACE INTO ?:order_data ?e", $_data); } // Save payment information if (!empty($cart['payment_info'])) { $_data = array('order_id' => $order_id, 'type' => 'P', 'data' => fn_encrypt_text(serialize($cart['payment_info']))); db_query("REPLACE INTO ?:order_data ?e", $_data); } // Save coupons information if (!empty($cart['coupons'])) { $_data = array('order_id' => $order_id, 'type' => 'C', 'data' => serialize($cart['coupons'])); db_query("REPLACE INTO ?:order_data ?e", $_data); } // Save secondary currency (for order notifications from payments with feedback requests) $_data = array('order_id' => $order_id, 'type' => 'R', 'data' => serialize(CART_SECONDARY_CURRENCY)); db_query("REPLACE INTO ?:order_data ?e", $_data); // // Place the order_id to new_orders table for all admin profiles // $admins = db_get_fields("SELECT user_id FROM ?:users WHERE user_type = 'A'"); foreach ($admins as $k => $v) { db_query("REPLACE INTO ?:new_orders (order_id, user_id) VALUES (?i, ?i)", $order_id, $v); } fn_set_hook('place_order', $order_id, $action, $__order_status, $cart); // If order total is zero, just save the order without any processing procedures if (floatval($cart['total']) == 0) { $action = 'save'; $__order_status = 'P'; } list($is_processor_script, ) = fn_check_processor_script($cart['payment_id'], $action, true); if (!$is_processor_script && $__order_status == STATUS_INCOMPLETED_ORDER) { $__order_status = 'O'; } // Set new order status fn_change_order_status($order_id, $__order_status, '', $is_processor_script || $__order_status == STATUS_PARENT_ORDER ? fn_get_notification_rules(array(), true) : fn_get_notification_rules(array()), true); $cart['processed_order_id'] = array(); $cart['processed_order_id'][] = $order_id; if (!$parent_order_id && count($cart['companies']) > 1 && PRODUCT_TYPE == 'MULTIVENDOR') { fn_companies_place_suborders($order_id, $cart, $auth, $action); $child_orders = db_get_fields("SELECT order_id FROM ?:orders WHERE parent_order_id = ?i", $order_id); array_unshift($child_orders, $order_id); $cart['processed_order_id'] = $child_orders; } return array($order_id, $action != 'save'); } } return array(false, false); }