function cw_ps_products_in_cart_pre($cart, $user_info)
{
    $special_offers_apply =& cw_session_register("special_offers_apply");
    # Delete all free added products
    if (is_array($cart['products'])) {
        foreach ($cart['products'] as $kk => $vv) {
            if ($vv["promotion_suite"]["free_product"] == 'Y') {
                cw_call('cw_delete_from_cart', array(&$cart, $vv['cartid']));
            }
        }
    }
    # / Delete all free added products
    if (!empty($special_offers_apply['free_products'])) {
        foreach ($special_offers_apply['free_products'] as $pid => $qty) {
            #
            # Add product to the cart
            #
            $product = cw_func_call('cw_product_get', array('id' => $pid, 'info_type' => 8192));
            $avail_amount = $product['avail'];
            $qty = min($qty, $avail_amount);
            $min_amount = $product['min_amount'];
            if ($qty >= $min_amount) {
                cw_load('warehouse');
                $possible_warehouses = cw_warehouse_get_avails_customer($pid);
                $warehouse = key($possible_warehouses);
                $add_product = array();
                $add_product["product_id"] = $pid;
                $add_product["amount"] = $qty;
                $add_product["product_options"] = "";
                $add_product["price"] = 0.0;
                // warehouse is required, otherwise free product will be placed as separate order
                $add_product['warehouse_customer_id'] = $warehouse;
                // do to use cw_warehouse_add_to_cart_simple. It operates with global $cart while we use here local $cart copy
                //  $result = cw_call('cw_warehouse_add_to_cart_simple', array($pid, $qty, '', 0.00));
                $result = cw_call('cw_add_to_cart', array(&$cart, $add_product));
                # Adjust just added product
                foreach ($cart['products'] as $ck => $cv) {
                    if ($cv["cartid"] == $result['cartid']) {
                        $cart['products'][$ck]["promotion_suite"]["free_product"] = true;
                        // mark just added product as offered
                    }
                }
            } else {
                unset($special_offers_apply['free_products'][$pid]);
            }
        }
        // foreach
    }
    // if
    return new EventReturn($cart['products'], array($cart, $user_info));
    // replace cart in input params for main function
}
function cw_warehouse_add_to_cart_simple($product_id, $amount, $product_options, $price, $order_id = 0)
{
    global $cart;
    # kornev, if warehouse is not specified, use the first N warehouses, while required amouint is not added
    $possible_warehouses = cw_warehouse_get_avails_customer($product_id);
    $amount = abs(intval($amount));
    foreach ($possible_warehouses as $warehouse => $tmp) {
        $add_product = array();
        $add_product['product_id'] = abs(intval($product_id));
        $add_product['amount'] = abs(intval($amount));
        $add_product['product_options'] = $product_options;
        $add_product['price'] = abs(doubleval($price));
        $add_product['warehouse_customer_id'] = $warehouse;
        $add_product['salesman_doc_id'] = abs(doubleval($order_id));
        $result = cw_call('cw_add_to_cart', array(&$cart, $add_product));
        $amount = $amount - $result['added_amount'];
        if ($amount <= 0) {
            break;
        }
    }
    return $result;
}
function cw_ps_update_cart_products(&$cart, &$products, $user_info, $offers_ids = array())
{
    global $config, $smarty, $tables, $addons;
    static $iter;
    if (APP_AREA != 'customer') {
        return false;
    }
    if (empty($products) || !is_array($products)) {
        cw_session_unregister('ps_offers_info');
        return false;
    }
    // we should track actions here as well...
    global $action;
    $action = (string) $action;
    $tracking_actions = array('add' => 1, 'update' => 1, 'delete' => 1, 'ajax_update' => 1, 'clear_cart' => 1);
    if (empty($action) || !isset($tracking_actions[$action])) {
        return -1;
        // view cart
    }
    if ($action == 'clear_cart') {
        cw_session_unregister('ps_offers_info');
        return false;
    }
    if (!isset($iter)) {
        $iter = 0;
    }
    $ps_offers_info =& cw_session_register('ps_offers_info');
    $current_product_hash = md5(cw_ps_prods_str($products));
    echo '<pre>$current_product_hash: ', $current_product_hash, '</pre>';
    //die;
    if (!empty($ps_offers_info) && is_array($ps_offers_info)) {
        if (isset($ps_offers_info['hash_offer_free']) && !empty($ps_offers_info['hash_offer_free'])) {
            if (isset($ps_offers_info['hash_offer_free'][$current_product_hash])) {
                return $ps_offers_info['hash_offer_free'][$current_product_hash];
                //3 - already processed, we should replace the cart[products] with the existing products array
                //2 - already processed
            } else {
                $iter++;
                $ps_offers_info['hash_offer_free'][$current_product_hash] = array();
                if ($iter > 1) {
                    echo '<pre>', "\t\t!!!", "{$iter} iteration: ", "ps_offers_info['processed_prods_hash']: {$ps_offers_info['processed_prods_hash']}", '</pre>';
                    echo '<pre>', "\t\tps_offers_info['product_hash']: {$ps_offers_info['product_hash']}", '</pre>';
                    //echo '<pre>', print_r($products), '</pre>';
                }
            }
        }
        /*if (isset($ps_offers_info['processed_prods_hash']) || isset($ps_offers_info['product_hash'])) {
                    $current_product_hash = md5(cw_ps_prods_str($products));
                }
        
                if (isset($ps_offers_info['processed_prods_hash'])) {
        
                    if ($ps_offers_info['processed_prods_hash'] == $current_product_hash) {
                        return 3; //already processed, we should replace the cart[products] with the existing products array
                    }
                }
        
                if (isset($ps_offers_info['product_hash'])) {
        
                    if ($ps_offers_info['product_hash'] == $current_product_hash) {
                        return 2; //already processed
                    }
                }
        
        
                if (isset($ps_offers_info['product_hash']) || isset($ps_offers_info['processed_prods_hash'])) {
                    if ($ps_offers_info['product_hash'] != $current_product_hash && $ps_offers_info['processed_prods_hash'] != $current_product_hash && $iter > 1) {
                        echo '<pre>', "\t\t!!!", "$iter iteration: ", "ps_offers_info['processed_prods_hash']: $ps_offers_info[processed_prods_hash]", '</pre>';
                        echo '<pre>', "ps_offers_info['product_hash']: $ps_offers_info[product_hash]", '</pre>';
                        echo '<pre>', print_r($products), '</pre>';
                    }
                }*/
        /*if (isset($ps_offers_info['hash_offer_free']) && !empty($ps_offers_info['hash_offer_free'])) {
              if (!isset($ps_offers_info['hash_offer_free'][$current_product_hash]) && $iter > 1) {
                  echo '<pre>', "\t\t!!!", "$iter iteration: ", "ps_offers_info['processed_prods_hash']: $ps_offers_info[processed_prods_hash]", '</pre>';
                  echo '<pre>', "\t\tps_offers_info['product_hash']: $ps_offers_info[product_hash]", '</pre>';
                  echo '<pre>', print_r($products), '</pre>';
              }
          }*/
    }
    if (empty($offers_ids) || !isset($offers_ids['new']) || !isset($offers_ids['to_delete']) || !isset($offers_ids['suitable'])) {
        return false;
    }
    //echo '<pre>', "ps_offers_info['processed_prods_hash']: $ps_offers_info[processed_prods_hash]", '</pre>';
    //echo '<pre>', "ps_offers_info['product_hash']: $ps_offers_info[product_hash]", '</pre>';
    //die('-');
    $offers = array();
    //if (!empty($ps_offers_info) && is_array($ps_offers_info) && isset($ps_offers_info['product_hash'])) {
    if (!empty($ps_offers_info) && isset($ps_offers_info['hash_offer_free'])) {
        if (isset($ps_offers_info['added_free_prods']) && is_array($ps_offers_info['added_free_prods']) && isset($ps_offers_info['applied_offers_free']) && is_array($ps_offers_info['applied_offers_free'])) {
            // we will not restore products for free which were deleted by a customer
            $new_offers = $offers_ids['new'];
            $offers_to_delete = $offers_ids['to_delete'];
            $suitable_offers = $offers_ids['suitable'];
            if (!empty($new_offers) && is_array($new_offers)) {
                $offers = $new_offers;
            }
            $deleted_prods_exist = false;
            if (!empty($offers_to_delete)) {
                $cart_records_to_delete = array();
                foreach ($offers_to_delete as $key => $trash) {
                    $cart_records_to_delete += $ps_offers_info['applied_offers_free'][$key];
                    //unset($ps_offers_info['applied_offers_free'][$key]); //?
                }
                if (!empty($cart_records_to_delete)) {
                    foreach ($products as $key => $cart_record) {
                        if (isset($cart_records_to_delete[$cart_record['cartid']])) {
                            unset($products[$key]);
                            $deleted_prods_exist = true;
                            if (isset($ps_offers_info['added_free_prods'][$cart_record['cartid']])) {
                                //unset($ps_offers_info['added_free_prods'][$cart_record['cartid']]); //?
                            }
                        }
                    }
                }
            }
            // offers to delelete
            // let's update the products for free in the cart; their quantities and prices should be untouched
            $free_products_exist = false;
            if (!empty($suitable_offers) && is_array($suitable_offers)) {
                $cart_records_to_update = array();
                foreach ($suitable_offers as $key => $trash) {
                    $cart_records_to_update += $ps_offers_info['applied_offers_free'][$key];
                }
                //echo '<pre>$cart_records_to_update: ', print_r($cart_records_to_update), '</pre>';
                //echo '<pre>$ps_offers_info: ', print_r($ps_offers_info), '</pre>';
                //echo '<pre>before update: ', print_r($products), '</pre>';
                $free_products_exist = false;
                if (!empty($cart_records_to_update)) {
                    foreach ($products as $key => $cart_record) {
                        if (isset($cart_records_to_update[$cart_record['cartid']])) {
                            $free_products_exist = true;
                            $products[$key] = $ps_offers_info['added_free_prods'][$cart_record['cartid']];
                        }
                    }
                }
            }
            if (!empty($new_offers) && is_array($new_offers)) {
            } else {
                if ($free_products_exist == true) {
                    $products = cw_products_from_scratch($products, $user_info, false, false);
                }
                if ($deleted_prods_exist == true || $free_products_exist == true) {
                    $ps_offers_info['product_hash'] = md5(cw_ps_prods_str($products));
                    $ps_offers_info['processed_prods_hash'] = md5(cw_ps_prods_str($cart['products']));
                    $ps_offers_info['hash_offer_free'][md5(cw_ps_prods_str($products))] = 2;
                    $ps_offers_info['hash_offer_free'][md5(cw_ps_prods_str($cart['products']))] = 3;
                    //$ps_offers_info['data_hash'] = md5(cw_ps_prods_str($products) . cw_ps_address_str($user_info));
                    $ps_offers_info['hash'][md5(cw_ps_prods_str($products) . cw_ps_address_str($user_info))] = 2;
                    //die('yes');
                    return true;
                } else {
                    return 4;
                    // no new offers and cart prods were not updated
                }
            }
        }
    }
    // empty($ps_offers_info)
    if (empty($offers) && !empty($offers_ids['new'])) {
        $offers = $offers_ids['new'];
    }
    if (empty($offers) || !is_array($offers)) {
        return false;
    }
    // check if the suitable products exist...
    $fields = $from_tbls = $query_joins = $where = $groupbys = $having = $orderbys = array();
    $fields = array("{$tables['ps_bonuses']}.offer_id", 'object_id', 'quantity', "{$tables['ps_bonuses']}.offer_id as offerid", "{$tables['ps_bonuses']}.bonus_id");
    $from_tbls[] = 'ps_bonuses';
    $query_joins['ps_bonus_details'] = array('on' => "{$tables['ps_bonuses']}.bonus_id = {$tables['ps_bonus_details']}.bonus_id", 'is_inner' => 1);
    $where[] = "{$tables['ps_bonuses']}.offer_id IN ('" . implode("', '", array_keys($offers)) . "')";
    $where[] = "{$tables['ps_bonuses']}.type = '" . PS_FREE_PRODS . "'";
    $where[] = "{$tables['ps_bonus_details']}.object_type = '" . PS_OBJ_TYPE_PRODS . "'";
    $search_query = cw_db_generate_query($fields, $from_tbls, $query_joins, $where, $groupbys, $having, $orderbys);
    $free_products = cw_query_hash($search_query, 'object_id', false);
    if (empty($free_products) || !is_array($free_products)) {
        return false;
    }
    //global $ps_offers_info;
    //$ps_offers_info = array();
    $ps_offers_info['already_applied'] = true;
    //$ps_offers_info = &cw_session_register('ps_offers_info');
    //var_dump('suitable products:', $free_products);
    //return $products;
    //die(var_dump('suitable products:', $free_products));
    global $user_account;
    cw_load('warehouse');
    foreach ($free_products as $product_id => $product_data) {
        /*
         * we will add the suitable products to the cart directly to avoid the groupping of the default products
         * and special ones
         */
        $product_status = cw_query_first_cell("SELECT status FROM {$tables['products_enabled']} as pe WHERE pe.product_id = '{$product_id}' AND pe.status = '1'");
        if ($product_status != 1) {
            continue;
        }
        $product_data['quantity'] = abs(intval($product_data['quantity']));
        if (empty($product_data['quantity'])) {
            continue;
        }
        $new_product = cw_func_call('cw_product_get', array('id' => $product_id, 'user_account' => $user_account, 'info_type' => 3));
        if ($new_product['product_type'] != constant('PRODUCT_TYPE_GENERAL')) {
            if (!empty($new_product['product_type'])) {
                continue;
            }
        }
        $amount = $product_data['quantity'];
        if (!empty($addons['egoods']) && !empty($new_product['distribution'])) {
            $amount = 1;
        }
        if ($amount < $new_product['min_amount']) {
            continue;
        }
        $possible_warehouses = cw_warehouse_get_avails_customer($product_id);
        if (empty($possible_warehouses) || !is_array($possible_warehouses)) {
            $possible_warehouse = cw_warehouse_get_max_amount_warehouse($product_id);
            $possible_warehouses = array($possible_warehouse => 1);
        }
        $warehouse = array_shift(array_keys($possible_warehouses));
        //foreach($possible_warehouses as $warehouse => $tmp) {
        if (!$warehouse) {
            $possible_warehouse = cw_warehouse_get_max_amount_warehouse($product_id);
        }
        if ($addons['product_options']) {
            # Get default options
            $product_options = cw_get_default_options($product_id, $amount, @$user_account['membership_id']);
            if ($product_options === false) {
                continue;
                //continue 2;
            } elseif ($product_options === true) {
                $product_options = null;
            }
            # Get the variant_id of options
            $variant_id = cw_get_variant_id($product_options, $product_id);
            if (!empty($variant_id)) {
                $possible_warehouse = cw_warehouse_get_max_amount_warehouse($product_id, $variant_id);
                if (empty($warehouse)) {
                    $warehouse = $possible_warehouse;
                }
            } else {
                if (empty($warehouse)) {
                    $warehouse = $possible_warehouse;
                }
            }
        }
        if (empty($warehouse)) {
            $warehouse = $possible_warehouse;
        }
        // let's add a product the cart
        $free_price = 0.0;
        $cartid = cw_generate_cartid($products);
        $products[] = array("cartid" => $cartid, "product_id" => $product_id, "amount" => $amount, "options" => $product_options, "free_price" => @price_format(@$free_price), "salesman_doc_id" => 0, "distribution" => $new_product['distribution'], "variant_id" => $variant_id, "warehouse_customer_id" => $warehouse);
        //$amount = $amount - $result['added_amount'];
        //if ($amount <= 0) break;
        //}
        if (!isset($ps_offers_info['added_free_prods'])) {
            $ps_offers_info['added_free_prods'] = array();
        }
        $ps_offers_info['added_free_prods'][$cartid] = $products[count($products) - 1];
        $ps_offers_info['added_free_prods'][$cartid]['offer_id'] = $product_data['offerid'];
        $ps_offers_info['added_free_prods'][$cartid]['bonus_id'] = $product_data['bonus_id'];
        if (!isset($ps_offers_info['applied_offers_free'])) {
            $ps_offers_info['applied_offers_free'] = array();
        }
        if (!isset($ps_offers_info['applied_offers_free'][$product_data['offerid']])) {
            $ps_offers_info['applied_offers_free'][$product_data['offerid']] = array();
        }
        $ps_offers_info['applied_offers_free'][$product_data['offerid']][$cartid] = 1;
    }
    $products = cw_products_from_scratch($products, $user_info, false, false);
    //$ps_offers_info['product_hash'] = md5(serialize($products));
    $ps_offers_info['product_hash'] = md5(cw_ps_prods_str($products));
    $ps_offers_info['processed_prods_hash'] = md5(cw_ps_prods_str($cart['products']));
    $ps_offers_info['hash_offer_free'][md5(cw_ps_prods_str($products))] = 2;
    $ps_offers_info['hash_offer_free'][md5(cw_ps_prods_str($cart['products']))] = 3;
    //$ps_offers_info['data_hash'] = md5(cw_ps_prods_str($products) . cw_ps_address_str($user_info));
    $ps_offers_info['hash'][md5(cw_ps_prods_str($products) . cw_ps_address_str($user_info))] = 2;
    cw_ps_save('products', $products);
    //$ps_offers_info['processed_prods_hash'] = md5(serialize($cart['products']));
    //echo '<pre>'; print_r($ps_offers_info); echo '</pre>';
    echo '<pre>', "ps_offers_info['processed_prods_hash']: {$ps_offers_info['processed_prods_hash']}", '</pre>';
    echo '<pre>', "ps_offers_info['product_hash']: {$ps_offers_info['product_hash']}", '</pre>';
    return true;
}
    $product_info['amount'] = 1;
    $config['General']['apply_default_country'] = 'Y';
    $config['Shipping']['enable_all_shippings'] = 'N';
    $cart_tmp['products'] = array($product_info);
    $shippings = cw_func_call('cw_shipping_get_list', array('cart' => $cart_tmp, 'products' => array($product_info), 'userinfo' => $userinfo, 'warehouse_customer_id' => $warehouse));
    // Supplier delivery time
    if ($product_info['system']['supplier_customer_id']) {
        $supplier_fields = cw_user_get_custom_fields($product_info['system']['supplier_customer_id'], 0, '', 'field');
        if ($supplier_fields['min_delivery_time'] == $supplier_fields['max_delivery_time']) {
            $product_info['supplier']['delivery_time'] = $supplier_fields['min_delivery_time'];
        } else {
            $product_info['supplier']['delivery_time'] = $supplier_fields['min_delivery_time'] . '-' . $supplier_fields['max_delivery_time'];
        }
    }
    $smarty->assign('shippings', $shippings);
}
$smarty->assign('zipcode', $zipcode);
$smarty->assign('country', $country ? $country : $user_account['country']);
$smarty->assign('product', $product_info);
if (defined('IS_AJAX') && constant('IS_AJAX')) {
    cw_add_ajax_block(array('id' => 'estimate_shipping_container', 'action' => 'replace', 'template' => 'customer/products/estimate-fields.tpl'));
} else {
    $avails = cw_warehouse_get_avails_customer($product_id);
    $smarty->assign('avails', $avails);
    $smarty->assign('count_avails', count($avails));
    $location[] = array(cw_get_langvar_by_name('lbl_estimate_ship_note'), '');
    $smarty->assign('home_style', 'popup');
    $smarty->assign('current_main_dir', 'addons/shipping_system');
    $smarty->assign('current_section_dir', 'customer');
    $smarty->assign('main', 'popup-shipping');
}
function cw_product_get($params, $return = null)
{
    extract($params);
    global $customer_id, $customer_id_type, $current_area, $cart, $current_location;
    global $current_language, $tables, $config, $addons;
    cw_load('files', 'taxes', 'tags');
    $lang = $lang ? $lang : $current_language;
    $variant_id = $variant_id ? $variant_id : 0;
    $amount = intval($amount > 0 ? $amount : 1);
    $fields = $from_tbls = $query_joins = $where = array();
    # kornev, merge standart and additional variables
    if ($return) {
        foreach ($return as $saname => $sadata) {
            if (isset(${$saname}) && is_array(${$saname}) && empty(${$saname})) {
                ${$saname} = $sadata;
            }
        }
    }
    $from_tbls[] = 'products';
    $where[] = "{$tables['products']}.product_id='{$id}'";
    # kornev
    # customer area - all of the checkings
    # pos area - not check for membership, just available for sale property
    if (in_array($current_area, array('C'))) {
        $memberships = array(0);
        if ($user_account['membership_id'] > 0) {
            $memberships[] = intval($user_account['membership_id']);
        }
        $where[] = "{$tables['products_memberships']}.membership_id IN (" . join(',', $memberships) . ")";
        $where[] = "{$tables['products_prices']}.quantity <= {$amount} and {$tables['products_prices']}.membership_id in (" . join(',', $memberships) . ")";
        if ($config['Appearance']['categories_in_products'] == '1') {
            $where[] = "{$tables['categories_memberships']}.membership_id IN (" . join(',', $memberships) . ")";
            $query_joins['products_categories'] = array('on' => "{$tables['products_categories']}.product_id = {$tables['products']}.product_id", 'pos' => '0', 'is_straight' => 1);
            $query_joins['categories'] = array('on' => "{$tables['products_categories']}.category_id = {$tables['categories']}.category_id", 'parent' => 'products_categories');
            $query_joins['categories_memberships'] = array('on' => "{$tables['categories_memberships']}.category_id = {$tables['categories']}.category_id", 'parent' => 'categories', 'is_straight' => 1);
        }
        $where[] = "{$tables['products']}.status in ('" . implode("', '", cw_core_get_required_status($current_area)) . "')";
    } elseif (in_array($current_area, array('G'))) {
        $where[] = "{$tables['products']}.product_id = {$tables['products_prices']}.product_id AND {$tables['products_prices']}.quantity <= {$amount} and {$tables['products_prices']}.membership_id in (" . join(',', $memberships) . ")";
    }
    $fields[] = "{$tables['products']}.*";
    $query_joins['products_warehouses_amount'] = array('on' => "{$tables['products']}.product_id = {$tables['products_warehouses_amount']}.product_id and {$tables['products_warehouses_amount']}.warehouse_customer_id=0 and {$tables['products_warehouses_amount']}.variant_id='{$variant_id}'");
    $in_cart = 0;
    if ($current_area == 'C' && !empty($cart) && !empty($cart['products'])) {
        foreach ($cart['products'] as $cart_item) {
            if ($cart_item['product_id'] == $id) {
                $in_cart += $cart_item['amount'];
            }
        }
    }
    $fields[] = "{$tables['products_warehouses_amount']}.avail-{$in_cart} AS avail";
    # kornev, TOFIX
    if ($addons['product_options'] && in_array($current_area, array('A', 'P'))) {
        $query_joins['product_variants'] = array('on' => "{$tables['products']}.product_id = {$tables['product_variants']}.product_id");
        $fields[] = "IF({$tables['product_variants']}.product_id IS NULL, '', 'Y') as is_variants";
    }
    /*
        if ($addons['manufacturers']) {
            $query_joins['manufacturers'] = array(
                'on' => "$tables[manufacturers].manufacturer_id = $tables[products].manufacturer_id",
            );
            $fields[] = "$tables[manufacturers].manufacturer";
        }
    */
    // statistic
    $fields[] = "{$tables['products_stats']}.views_stats";
    $fields[] = "{$tables['products_stats']}.sales_stats";
    $fields[] = "{$tables['products_stats']}.del_stats";
    $fields[] = "{$tables['products_stats']}.add_to_cart";
    $query_joins['products_stats'] = array('on' => "{$tables['products_stats']}.product_id = {$tables['products']}.product_id");
    if ($current_area == 'A' || $current_area == 'P') {
        $fields[] = "{$tables['products_prices']}.price";
        $fields[] = "{$tables['products_prices']}.list_price";
        $query_joins['products_prices'] = array('on' => "{$tables['products_prices']}.product_id={$tables['products']}.product_id AND {$tables['products_prices']}.variant_id = '{$variant_id}' and {$tables['products_prices']}.quantity <= {$amount}");
    } else {
        $query_joins['products_prices'] = array('on' => "{$tables['products_prices']}.product_id={$tables['products']}.product_id", 'is_inner' => 1);
        # kornev, find the min price and select only this record.
        $fields[] = "min({$tables['products_prices']}.price) as price";
        $fields[] = "{$tables['products_prices']}.variant_id";
        $fields[] = "min({$tables['products_prices']}.list_price) as list_price";
    }
    $fields[] = "IF({$tables['products_lng']}.product_id != '', {$tables['products_lng']}.product, {$tables['products']}.product) as product";
    $fields[] = "IF({$tables['products_lng']}.product_id != '', {$tables['products_lng']}.descr, {$tables['products']}.descr) as descr";
    $fields[] = "IF({$tables['products_lng']}.product_id != '', {$tables['products_lng']}.fulldescr, {$tables['products']}.fulldescr) as fulldescr";
    $fields[] = "IF({$tables['products_lng']}.product_id != '', {$tables['products_lng']}.features_text, {$tables['products']}.features_text) as features_text";
    $fields[] = "IF({$tables['products_lng']}.product_id != '', {$tables['products_lng']}.specifications, {$tables['products']}.specifications) as specifications";
    $query_joins['products_lng'] = array('on' => "{$tables['products_lng']}.code='{$lang}' AND {$tables['products_lng']}.product_id = {$tables['products']}.product_id");
    if (in_array($current_area, array('C', 'G', 'S'))) {
        $fields[] = "{$tables['products_flat']}.*";
        $query_joins['products_flat'] = array('on' => "{$tables['products']}.product_id = {$tables['products_flat']}.product_id");
        if ($current_area == 'C') {
            $query_joins['products_memberships'] = array('on' => "{$tables['products_memberships']}.product_id = {$tables['products']}.product_id", 'is_inner' => 1);
        }
    }
    if ($config['Appearance']['categories_in_products'] == '1') {
        $fields[] = "{$tables['products_categories']}.category_id";
        $query_joins['categories'] = array('parent' => 'products_categories', 'on' => "{$tables['categories']}.category_id = {$tables['products_categories']}.category_id");
        $query_joins['products_categories'] = array('on' => "{$tables['products_categories']}.product_id = {$tables['products']}.product_id and {$tables['products_categories']}.main=1", 'pos' => '0', 'is_straight' => 1);
    }
    $fields[] = "{$tables['products']}.product_id";
    $query = cw_db_generate_query($fields, $from_tbls, $query_joins, $where, array("{$tables['products']}.product_id"), array(), array());
    $product = cw_query_first($query);
    # Error handling
    if (!$product || $current_area == 'C' && !$product['category_id'] && $config['Appearance']['categories_in_products'] == '1') {
        return false;
    }
    $product['system'] = cw_call('cw_product_get_system_info', array($product['product_id']));
    $product['attribute_class_ids'] = cw_func_call('cw_items_attribute_classes_get', array('item_id' => $product['product_id'], 'item_type' => 'P', 'for_product_modify' => $for_product_modify));
    if ($info_type & 1) {
        $product['membership_ids'] = cw_query_key("select membership_id from {$tables['products_memberships']} where product_id = '{$product['product_id']}'");
    }
    # kornev, TOFIX
    if ($info_type & 64 && $product['variant_id'] && $addons['product_options']) {
        $tmp = cw_query_first("SELECT * FROM {$tables['product_variants']} WHERE variant_id = '{$product['variant_id']}'");
        if (!empty($tmp)) {
            cw_unset($tmp, "def");
            $product = cw_array_merge($product, $tmp);
        } else {
            cw_unset($product, 'variant_id');
        }
    }
    if ($info_type & 128) {
        cw_load('image');
        $product['image_thumb'] = cw_image_get('products_images_thumb', $id);
    }
    if ($info_type & 512) {
        $product['image_det'] = cw_image_get('products_images_det', $id);
        # kornev, TOFIX
        if (in_array($current_area, array('C', 'B')) && $product['variant_id'] && $addons['product_options']) {
            $var_image = cw_image_get('products_images_var', $id);
            if (!$var_image['is_default']) {
                $product['image_det'] = $var_image;
            }
        }
    }
    # TOFIX
    if (in_array($current_area, array('C', 'B', 'G'))) {
        if (!$addons['egoods']) {
            $product['distribution'] = '';
        }
        $product['display_price'] = $product['price'];
        if ($current_area == 'C' && $info_type & 8) {
            $_tmp_price = $product['price'];
            $product['price'] = $product['list_price'];
            cw_get_products_taxes($product, $user_account);
            $product['list_price_net'] = $product['list_price'];
            $product['list_price'] = $product['taxed_price'];
            $product['price'] = $_tmp_price;
        }
        $product['taxes'] = cw_get_products_taxes($product, $user_account, false, '', $current_area == 'G' && $user_account['usertype'] != 'R');
    }
    if (in_array($current_area, array('C', 'B'))) {
        $product['descr'] = cw_eol2br($product['descr']);
        $product['fulldescr'] = cw_eol2br($product['fulldescr']);
    }
    $product['uns_shippings'] = unserialize($product['shippings']);
    $product['tags'] = cw_tags_get_product_tags($id);
    if ($info_type & 4096 && $product['warranty_id']) {
        $wr = cw_get_warranty($product['warranty_id'], $lang);
        $product['warranties'] = $wr['body'];
    }
    // TODO: move to addon as on_product_get handler
    if ($info_type & 8192) {
        cw_load('warehouse');
        if ($addons['warehouse']) {
            if (AREA_TYPE == 'A') {
                $product['avail_ordered'] = cw_warehouse_get_avail($id, 0, 'avail_ordered');
                $product['avail_sold'] = cw_warehouse_get_avail($id, 0, 'avail_sold');
                $product['avail_reserved'] = cw_warehouse_get_avail($id, 0, 'avail_reserved');
            } elseif (AREA_TYPE == 'P') {
                $product['avail'] = cw_warehouse_get_warehouse_avail($customer_id, $id);
                $product['avail_ordered'] = cw_warehouse_get_warehouse_avail($customer_id, $id, 'avail_ordered');
            } else {
                // TOFIX:  $product['avail'] becomes different meanings when info_type & 8192 flag is raised or not
                // without flag - avail of all variants without products already in cart
                // with flag - total avail as set in admin per variant
                //                $product['avail'] = cw_warehouse_get_avail_for_customer($id, $product['variant_id']);
                //                $product['avails']= cw_warehouse_get_avails_customer($id);
                $product['avail'] = cw_warehouse_get_avail_for_customer($id, $product['variant_id']);
            }
        } else {
            $product['avails'] = cw_warehouse_get_avails_customer($id, $product['avail'] + $product['avail_ordered']);
        }
    }
    return $product;
}