Example #1
0
function fn_exim_1c_import_product_features($product, $product_id, $features_1c, $categories, $lang_code, $cml)
{
    $is_id = false;
    $shipping_params = fn_exim_1c_get_shipping_params();
    $category_id = !empty($categories[strval($product->{$cml}['groups']->{$cml}['id'])]) ? $categories[strval($product->{$cml}['groups']->{$cml}['id'])] : '0';
    $variants_data['product_id'] = $product_id;
    $variants_data['lang_code'] = $lang_code;
    foreach ($product->{$cml}['properties_values']->{$cml}['property_values'] as $_feature) {
        if (!isset($features_1c[strval($_feature->{$cml}['id'])])) {
            continue;
        }
        $variants_data['feature_id'] = $features_1c[strval($_feature->{$cml}['id'])]['id'];
        $_variants = '';
        if (!empty($features_1c[strval($_feature->{$cml}['id'])]['variants'])) {
            $_variants = $features_1c[strval($_feature->{$cml}['id'])]['variants'];
        }
        $value_data['feature_id'] = $features_1c[strval($_feature->{$cml}['id'])]['id'];
        $value_data['product_id'] = $product_id;
        $value_data['lang_code'] = $lang_code;
        $variant = array('variant' => '');
        if (empty($_variants)) {
            $variant['variant'] = strval($_feature->{$cml}['value']);
            if (strlen($variant['variant']) > 255) {
                $variant['variant'] = substr($variant['variant'], 0, 255);
            }
        } else {
            foreach ($_variants as $_variant) {
                if (strval($_feature->{$cml}['value']) == $_variant['id']) {
                    $variant['variant'] = $_variant['value'];
                    $is_id = true;
                    break;
                }
            }
            if (!$is_id) {
                $variant['variant'] = strval($_feature->{$cml}['value']);
            }
        }
        fn_exim_1c_add_shipping_param($product_id, $shipping_params, $_feature, $features_1c[strval($_feature->{$cml}['id'])], $cml);
        if ($variants_data['feature_id'] == 0) {
            continue;
        }
        if (!empty($category_id)) {
            $feature_categories = fn_explode(',', db_get_field("SELECT categories_path FROM ?:product_features WHERE feature_id = ?i", $variants_data['feature_id']));
            if (!in_array($category_id, $feature_categories)) {
                $feature_categories[] = $category_id;
                $feature_categories = array_diff($feature_categories, array(''));
                db_query("UPDATE ?:product_features SET categories_path = ?s WHERE feature_id = ?i", implode(',', $feature_categories), $variants_data['feature_id']);
            }
        }
        // Check if current variant already exist
        list($check, $variant_id) = fn_exim_1c_check_feature_variant($variants_data['feature_id'], $variant['variant'], $lang_code);
        if ($check) {
            $variants_data['variant_id'] = fn_add_feature_variant($variants_data['feature_id'], $variant);
        } else {
            $variants_data['variant_id'] = $variant_id;
        }
        fn_exim_1c_add_features_values($variants_data);
    }
}
Example #2
0
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_product_features_value($product_id, $product_features, $add_new_variant, $lang_code)
{
    if (empty($product_features)) {
        return false;
    }
    $i_data = array('product_id' => $product_id, 'lang_code' => $lang_code);
    foreach ($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, $lang_code);
            $_product = db_get_field("SELECT product FROM ?:product_descriptions WHERE product_id = ?i AND lang_code = ?s", $product_id, $lang_code);
            fn_set_notification('E', __('error'), __('product_feature_cannot_assigned', array('[feature_name]' => $_feature, '[product_name]' => $_product)));
            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'] = !empty($value) ? fn_parse_date($value) : '';
        } elseif ($feature_type == 'M') {
            $variant_ids = array();
            if (!empty($add_new_variant[$feature_id]['variant']) || isset($add_new_variant[$feature_id]['variant']) && $add_new_variant[$feature_id]['variant'] === '0') {
                $value = empty($value) ? array() : $value;
                $value[] = fn_add_feature_variant($feature_id, $add_new_variant[$feature_id]);
            }
            if (!empty($value)) {
                foreach ($value as $variant_id) {
                    foreach (fn_get_translation_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($add_new_variant[$feature_id]['variant']) || isset($add_new_variant[$feature_id]['variant']) && $add_new_variant[$feature_id]['variant'] === '0') {
                $i_data['variant_id'] = fn_add_feature_variant($feature_id, $add_new_variant[$feature_id]);
                $i_data['value_int'] = $add_new_variant[$feature_id]['variant'];
            } 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, $lang_code);
                }
                $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 (fn_get_translation_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);
        }
    }
    return true;
}
function fn_exim_product_feature_variants($feature, $feature_id, $variants, $lang_code)
{
    $feature_type = $feature['type'];
    if (strpos('MSNE', $feature_type) !== false) {
        // variant IDs
        $vars = array();
        foreach ($feature['variants'] as $variant) {
            $vars[] = $variant;
        }
        $existent_variants = db_get_hash_single_array('SELECT pfvd.variant_id, variant FROM ?:product_feature_variant_descriptions AS pfvd ' . 'LEFT JOIN ?:product_feature_variants AS pfv ON pfv.variant_id = pfvd.variant_id ' . 'WHERE feature_id = ?i AND variant IN (?a) AND lang_code = ?s', array('variant_id', 'variant'), $feature_id, $vars, $lang_code);
        foreach ($feature['variants'] as $variant_data) {
            if (!in_array($variant_data, $existent_variants)) {
                $variant_id = fn_add_feature_variant($feature_id, array('variant' => $variant_data));
                $existent_variants[$variant_id] = $variant_data;
            }
        }
        if ($feature_type == 'M') {
            foreach ($feature['variants'] as $variant_data) {
                if (in_array($variant_data, $existent_variants)) {
                    $variant_id = array_search($variant_data, $existent_variants);
                    $variants[$feature_id][$variant_id] = $variant_id;
                }
            }
        } else {
            $variant_data = reset($feature['variants']);
            if (in_array($variant_data, $existent_variants)) {
                $variant_id = array_search($variant_data, $existent_variants);
                $variants[$feature_id] = $variant_id;
            }
        }
    } else {
        $variant_data = reset($feature['variants']);
        $variants[$feature_id] = $variant_data;
    }
    return $variants;
}
 public static function dataProductFeatures($data_product, &$product, $import_params)
 {
     $property_for_promo_text = trim(self::$s_commerceml['exim_1c_property_product']);
     $cml = self::$cml;
     $features_commerceml = self::$features_commerceml;
     if (!empty($data_product->{$cml}['properties_values']->{$cml}['property_values'])) {
         foreach ($data_product->{$cml}['properties_values']->{$cml}['property_values'] as $_feature) {
             $variant_data = array();
             $feature_id = strval($_feature->{$cml}['id']);
             if (!isset($features_commerceml[$feature_id]) || empty($_feature->{$cml}['value'])) {
                 continue;
             }
             $p_feature_name = strval($_feature->{$cml}['value']);
             $feature_name = trim($features_commerceml[$feature_id]['name'], " ");
             if (!empty($features_commerceml[$feature_id])) {
                 $product_params = self::dataShippingParams($p_feature_name, $feature_name, $import_params);
                 if (!empty($product_params)) {
                     $product = array_merge($product, $product_params);
                 }
                 if (!empty($property_for_promo_text) && $property_for_promo_text == $feature_name) {
                     if (!empty($features_commerceml[$feature_id]['variants'])) {
                         $product['promo_text'] = $features_commerceml[$feature_id]['variants'][$p_feature_name]['value'];
                     } else {
                         $product['promo_text'] = $p_feature_name;
                     }
                 }
             }
             if (!empty($features_commerceml[$feature_id]['id'])) {
                 $variant_data['feature_id'] = $features_commerceml[$feature_id]['id'];
                 $variant_data['feature_types'] = $features_commerceml[$feature_id]['type'];
                 $variant_data['lang_code'] = $import_params['lang_code'];
                 list($d_variants, $variant_params, $has_ungroupped) = fn_get_product_features($variant_data, 0, $import_params['lang_code']);
                 $variant_data = $d_variants[$variant_data['feature_id']];
                 if ($variant_data['feature_type'] == ProductFeatures::NUMBER_SELECTBOX) {
                     $p_feature_name = str_replace(',', '.', $p_feature_name);
                     $variant_data['value_int'] = $p_feature_name;
                 }
                 $is_id = false;
                 $variant = '';
                 if (!empty($features_commerceml[$feature_id]['variants'])) {
                     foreach ($features_commerceml[$feature_id]['variants'] as $_variant) {
                         if ($p_feature_name == $_variant['id']) {
                             $variant = $_variant['value'];
                             $is_id = true;
                             break;
                         }
                     }
                     if (!$is_id) {
                         $variant = $p_feature_name;
                     }
                 } else {
                     $variant = $p_feature_name;
                 }
                 $variant_data['variant'] = $variant;
                 list($d_variants, $params_variant) = fn_get_product_feature_variants(array('feature_id' => array($variant_data['feature_id']), 'search_query' => $variant), 0, $import_params['lang_code']);
                 if (!empty($d_variants)) {
                     $d_variant = reset($d_variants);
                     $variant_data['variant_id'] = $d_variant['variant_id'];
                 } else {
                     $variant_data['variant_id'] = fn_add_feature_variant($variant_data['feature_id'], array('variant' => $variant));
                 }
                 $product['features'][$feature_id] = $variant_data;
             }
         }
     }
     $variant_data = array();
     if (self::$s_commerceml['exim_1c_used_brand'] == 'field_brand') {
         if (isset($data_product->{$cml}['manufacturer'])) {
             $variant_data['feature_id'] = $features_commerceml['brand1c']['id'];
             $variant_data['lang_code'] = $import_params['lang_code'];
             $variant_id = db_get_field("SELECT variant_id FROM ?:product_feature_variants WHERE feature_id = ?i AND external_id = ?s", $variant_data['feature_id'], strval($data_product->{$cml}['manufacturer']->{$cml}['id']));
             $variant = strval($data_product->{$cml}['manufacturer']->{$cml}['name']);
             if (empty($variant_id)) {
                 $variant_data['variant_id'] = fn_add_feature_variant($variant_data['feature_id'], array('variant' => $variant));
                 db_query("UPDATE ?:product_feature_variants SET external_id = ?s WHERE variant_id = ?i", strval($data_product->{$cml}['manufacturer']->{$cml}['id']), $variant_data['variant_id']);
             } else {
                 $variant_data['variant_id'] = $variant_id;
             }
             $product['features'][$variant_data['feature_id']] = $variant_data;
         }
     }
 }
function fn_exim_product_feature_variants($feature, $feature_id, $variants, $lang_code)
{
    $feature_type = $feature['type'];
    if (strpos(ProductFeatures::getSelectable(), $feature_type) !== false) {
        // variant IDs
        $vars = array();
        foreach ($feature['variants'] as $variant) {
            $vars[] = $variant['name'];
        }
        $existent_variants = db_get_hash_single_array('SELECT pfvd.variant_id, variant FROM ?:product_feature_variant_descriptions AS pfvd ' . 'LEFT JOIN ?:product_feature_variants AS pfv ON pfv.variant_id = pfvd.variant_id ' . 'WHERE feature_id = ?i AND variant IN (?a) AND lang_code = ?s', array('variant_id', 'variant'), $feature_id, $vars, $lang_code);
        foreach ($feature['variants'] as $variant_data) {
            if (!in_array($variant_data['name'], $existent_variants)) {
                if (fn_allowed_for('MULTIVENDOR') && Registry::get('runtime.company_id')) {
                    fn_set_notification('W', __('warning'), __('exim_vendor_cant_create_feature'));
                    continue;
                }
                $variant_id = fn_add_feature_variant($feature_id, array('variant' => $variant_data['name']));
                $existent_variants[$variant_id] = $variant_data['name'];
            }
        }
        if ($feature_type == ProductFeatures::MULTIPLE_CHECKBOX) {
            foreach ($feature['variants'] as $variant_data) {
                if (in_array($variant_data['name'], $existent_variants)) {
                    $variant_id = array_search($variant_data['name'], $existent_variants);
                    $variants[$feature_id][$variant_id] = $variant_id;
                }
            }
        } else {
            $variant_data = reset($feature['variants']);
            if (in_array($variant_data['name'], $existent_variants)) {
                $variant_id = array_search($variant_data['name'], $existent_variants);
                $variants[$feature_id] = $variant_id;
            }
        }
    } else {
        $variant_data = reset($feature['variants']);
        $variants[$feature_id] = $variant_data['name'];
    }
    return $variants;
}