function cw_group_edit_copy_system_info($product_id, $data)
{
    global $ge_id;
    if (!$ge_id) {
        return;
    }
    while ($pid = cw_group_edit_each($ge_id, 1, $product_id)) {
        cw_product_update_system_info($pid, $data);
    }
}
function cw_product_clone($product_id)
{
    global $addons, $customer_id, $tables;
    $tables_array = array(array('table' => 'products_images_thumb', 'key_field' => 'id'), array('table' => 'products_images_det', 'key_field' => 'id'), array('table' => 'delivery', 'key_field' => 'product_id'), array('table' => 'attributes_values', 'key_field' => 'item_id'), array('table' => 'products_lng', 'key_field' => 'product_id'), array('table' => 'products_categories', 'key_field' => 'product_id'), array('table' => 'products_memberships', 'key_field' => 'product_id'));
    $product_data = cw_query_first("SELECT * FROM {$tables['products']} WHERE product_id='{$product_id}'");
    if (!$product_data) {
        return;
    }
    $product_data['productcode'] = cw_product_generate_sku();
    $new_product_id = cw_array2insert('products', array('productcode' => $product_data['productcode'], 'product_type' => $product_data['product_type']));
    $to_update = array();
    foreach ($product_data as $field => $value) {
        if (!in_array($field, array('product_id', 'productcode', 'product_type', 'views_stats'))) {
            $to_update[] = $field;
        }
    }
    $product_data['product'] = $product_data['product'] . ' (CLONE)';
    cw_array2update('products', cw_addslashes($product_data), "product_id='{$new_product_id}'", $to_update);
    foreach ($tables_array as $k => $v) {
        $error_string .= cw_core_copy_tables($v['table'], $v['key_field'], $product_id, $new_product_id);
    }
    db_query("update {$tables['products_lng']} set product = concat(product, ' (CLONE)') where product_id='{$new_product_id}'");
    // Clone prices
    $prices = cw_query("SELECT * FROM {$tables['products_prices']} WHERE product_id = '{$product_id}' AND variant_id = '0'");
    if (!empty($prices)) {
        foreach ($prices as $v) {
            unset($v['price_id']);
            $v['product_id'] = $new_product_id;
            cw_array2insert('products_prices', $v);
        }
    }
    $prices = cw_query("select pp.* from {$tables['products_prices']} as pp where pp.product_id='{$product_id}'");
    cw_func_call('cw_product_build_flat', array('product_id' => $new_product_id));
    cw_product_update_system_info($new_product_id, $customer_id);
    //    cw_warehouse_recalculate($product_id);
    // Update products counter for categories in which product is placed
    $product_categorie = cw_query_first("SELECT category_id FROM {$tables['products_categories']} WHERE product_id = '{$product_id}'");
    cw_recalc_subcat_count($product_categorie['category_id']);
    return $new_product_id;
}
     if ($fields['price'] && !$is_variant) {
         if ($ge_id) {
             while ($pid = cw_group_edit_each($ge_id, 1, $product_id)) {
                 if ($pid != $product_id) {
                     cw_price_lists_replace_price($pid, $product_data['price'], 0);
                 }
             }
         }
     }
     cw_func_call('cw_items_attribute_classes_save', array('item_id' => $product_id, 'attribute_class_ids' => $product_data['attribute_class_ids'], 'item_type' => 'P'));
     # kornev, it have to be product_data here - because we change the attributes in the error_check function
     cw_call('cw_attributes_save', array('item_id' => $product_id, 'item_type' => 'P', 'attributes' => $product_data['attributes'], 'language' => $edited_language, array('update_posted_only' => true, 'is_default' => false)));
     cw_attributes_group_update($ge_id, $product_id, 'P', $fields);
     cw_func_call('cw_product_build_flat', array('product_id' => $product_id));
     cw_group_edit_end($product_id);
     cw_product_update_system_info($product_id, array('supplier_customer_id' => $product_data['supplier']));
     cw_group_edit_copy_system_info($product_id, array('supplier_customer_id' => $product_data['supplier']));
     cw_warehouse_recalculate($product_id);
     cw_product_filter_recalculate_price_ranges();
     // tags
     if (!empty($product_data['tags'])) {
         $tags = explode(',', $product_data['tags']);
         cw_tags_set_product_tags($tags, $product_id);
     } else {
         cw_tags_clear_product_tags($product_id);
     }
 } else {
     cw_add_top_message($fillerror, 'E');
     $product_modified_data = $product_data;
     $product_modified_data['product_id'] = $product_id;
     cw_core_process_date_fields($product_modified_data, null, array('' => array('membership_ids', 'status')));