function fn_buy_together_get_chains($params = array(), $auth = array(), $lang_code = CART_LANGUAGE) { $fields = array('items.chain_id', 'items.product_id', 'items.products', 'items.modifier', 'items.modifier_type', 'items.date_from', 'items.date_to', 'items.display_in_promotions', 'items.status', 'descr.name', 'descr.description'); $conditions = array(); if (!empty($params['product_id'])) { $conditions[] = db_quote('items.product_id = ?i', $params['product_id']); } if (!empty($params['chain_id'])) { $conditions[] = db_quote('items.chain_id = ?i', $params['chain_id']); } if (!empty($params['status'])) { $conditions[] = db_quote('items.status = ?s', $params['status']); } if (!empty($params['date']) && $params['date']) { $date = mktime(0, 0, 0); $conditions[] = db_quote('(items.date_from <= ?i AND items.date_to >= ?i)', $date, $date); } if (!empty($params['promotions']) && $params['promotions']) { $conditions[] = db_quote('items.display_in_promotions = ?s', 'Y'); } if (!empty($conditions)) { $condition = 'WHERE ' . implode(' AND ', $conditions); } else { $condition = ''; } $fields = implode(', ', $fields); $chains = db_get_array("SELECT {$fields} FROM ?:buy_together AS items LEFT JOIN ?:buy_together_descriptions AS descr ON items.chain_id = descr.chain_id AND descr.lang_code = ?s {$condition}", $lang_code); if (!empty($chains)) { foreach ($chains as $key => $chain) { $chains[$key]['products'] = unserialize($chain['products']); if (!empty($params['full_info'])) { if ($params['full_info']) { if (isset($auth['area']) && $auth['area'] == 'C' && empty($chains[$key]['products'])) { unset($chains[$key]); continue; } $chains[$key]['products_info'] = $chains[$key]['products']; $is_valid = true; $_product = fn_get_product_data($chain['product_id'], $auth, CART_LANGUAGE, '', true, true, true, true); if (empty($_product)) { $is_valid = false; break; } fn_gather_additional_product_data($_product, true, true); $chains[$key]['product_name'] = $_product['product']; $chains[$key]['chain_amount'] = $_product['min_qty'] > 0 ? $_product['min_qty'] : 1; $chains[$key]['min_qty'] = $_product['min_qty'] = 1; $chains[$key]['price'] = $_product['price']; $chains[$key]['list_price'] = $_product['list_price']; $chains[$key]['main_pair'] = $_product['main_pair']; $chains[$key]['default_options'] = fn_get_default_product_options($_product['product_id']); $chains[$key]['product_options'] = $_product['product_options']; list($chains[$key]['discount'], $chains[$key]['discounted_price']) = fn_buy_together_calculate_discount($_product['price'], $chain['modifier'], $chain['modifier_type']); $chains[$key]['options_type'] = $_product['options_type']; $chains[$key]['exceptions_type'] = $_product['exceptions_type']; isset($_product['options_update']) ? $chains[$key]['options_update'] = $_product['options_update'] : ($chains[$key]['options_update'] = false); $total_price = $_product['price']; $chain_price = $chains[$key]['discounted_price']; foreach ($chains[$key]['products'] as $hash => $product) { if (empty($product['product_id'])) { unset($chains[$key]['products'][$hash]); unset($chains[$key]['products_info'][$hash]); continue; } $_product = fn_get_product_data($product['product_id'], $auth, CART_LANGUAGE, '', true, true, true, true); if (empty($_product)) { $is_valid = false; break; } fn_gather_additional_product_data($_product, true, true); $_product['min_qty'] = $_product['min_qty'] > 0 ? $_product['min_qty'] : 1; $chains[$key]['products'][$hash]['product_name'] = $_product['product']; $chains[$key]['products'][$hash]['min_qty'] = $_product['min_qty']; $chains[$key]['products'][$hash]['price'] = empty($chains[$key]['products'][$hash]['price']) ? $_product['price'] : $chains[$key]['products'][$hash]['price']; $chains[$key]['products'][$hash]['list_price'] = $_product['list_price']; $chains[$key]['products'][$hash]['main_pair'] = $_product['main_pair']; $chains[$key]['products'][$hash]['options_type'] = $_product['options_type']; $chains[$key]['products'][$hash]['exceptions_type'] = $_product['exceptions_type']; isset($_product['options_update']) ? $chains[$key]['products'][$hash]['options_update'] = $_product['options_update'] : ($chains[$key]['products'][$hash]['options_update'] = false); list($chains[$key]['products'][$hash]['discount'], $chains[$key]['products'][$hash]['discounted_price']) = fn_buy_together_calculate_discount($_product['price'], empty($product['modifier']) ? 0 : $product['modifier'], empty($product['modifier_type']) ? 'to_fixed' : $product['modifier_type']); $total_price += $_product['price'] * $product['amount']; $chain_price += $chains[$key]['products'][$hash]['discounted_price'] * $product['amount']; if (!empty($product['product_options'])) { $chains[$key]['products'][$hash]['product_options_short'] = $chains[$key]['products'][$hash]['product_options']; $options = fn_get_selected_product_options_info($product['product_options'], DESCR_SL); $chains[$key]['products'][$hash]['product_options'] = $options; } elseif (!empty($_product['product_options'])) { $chains[$key]['products'][$hash]['aoc'] = true; // Allow any option combinations $chains[$key]['products'][$hash]['options'] = $_product['product_options']; } $chains[$key]['products_info'][$hash]['price'] = $chains[$key]['products'][$hash]['price']; $chains[$key]['products_info'][$hash]['discount'] = $chains[$key]['products'][$hash]['discount']; $chains[$key]['products_info'][$hash]['discounted_price'] = $chains[$key]['products'][$hash]['discounted_price']; } } if (!$is_valid) { unset($chains[$key]); continue; } $chains[$key]['total_price'] = $total_price; $chains[$key]['chain_price'] = $chain_price; } if (!empty($params['simple'])) { if ($params['simple']) { return $chains[$key]; } } } } return $chains; }
/** * Get products chains * * @param array $params Parameters for the function * @param array $auth Array of user authentication data * @param string $lang_code 2-letter language code (e.g. 'en', 'ru', etc.) * * @return array Chains of products */ function fn_buy_together_get_chains($params = array(), $auth = array(), $lang_code = CART_LANGUAGE) { /** * Modify product chains get parameters * * @param array $params Parameters for the function * @param array $auth Array of user authentication data * @param string $lang_code 2-letter language code (e.g. 'en', 'ru', etc.) */ fn_set_hook('buy_together_get_chains_pre', $parms, $auth, $lang_code); $fields = array('items.chain_id', 'items.product_id', 'items.products', 'items.modifier', 'items.modifier_type', 'items.date_from', 'items.date_to', 'items.display_in_promotions', 'items.status', 'descr.name', 'descr.description'); if (fn_allowed_for('ULTIMATE')) { $fields[] = 'p.company_id'; } $conditions = array(); $joins = array(); $joins[] = db_quote("LEFT JOIN ?:products AS p ON p.product_id = items.product_id"); $joins[] = db_quote("LEFT JOIN ?:buy_together_descriptions AS descr ON items.chain_id = descr.chain_id AND descr.lang_code = ?s", $lang_code); if (!empty($params['product_id'])) { $conditions['product_id'] = db_quote('items.product_id = ?i', $params['product_id']); } if (!empty($params['chain_id'])) { $conditions['chain_id'] = db_quote('items.chain_id = ?i', $params['chain_id']); } if (!empty($params['status'])) { $conditions['status'] = db_quote('items.status = ?s', $params['status']); } if (!empty($params['date']) && $params['date']) { $date = mktime(0, 0, 0); $conditions['date'] = db_quote('(items.date_from <= ?i AND items.date_to >= ?i)', $date, $date); } if (!empty($params['promotions']) && $params['promotions']) { $conditions['promotions'] = db_quote('items.display_in_promotions = ?s', 'Y'); } /** * Change select condition (fields, conditions, joins) before selecting payment method data * * @param array $params Parameters for the function * @param array $auth Array of user authentication data * @param string $lang_code 2-letter language code (e.g. 'en', 'ru', etc.) * @param array $fields Array of fields to be selected * @param array $conditions Array of complete condition expressions to be applied to the end of an SQL-query * @param array $joins List of strings with the complete JOIN information (JOIN type, tables and fields) for an SQL-query * */ fn_set_hook('buy_together_get_chains', $params, $auth, $lang_code, $fields, $conditions, $joins); if (!empty($conditions)) { $condition = 'WHERE ' . implode(' AND ', $conditions); } else { $condition = ''; } $fields = implode(', ', $fields); $joins = implode(' ', $joins); $chains = db_get_array("SELECT {$fields} FROM ?:buy_together AS items {$joins} {$condition}"); if (!empty($chains)) { $selected_options = isset($params['selected_options']) ? $params['selected_options'] : array(); $product_ids = array(); $products = array(); if (!empty($params['full_info'])) { foreach ($chains as $chain) { $product_ids[$chain['product_id']] = $chain['product_id']; $chain_products = unserialize($chain['products']); foreach ($chain_products as $product) { $product_ids[$product['product_id']] = $product['product_id']; } } list($products) = fn_get_products(array('pid' => $product_ids)); } foreach ($chains as $key => &$chain) { $chain['products'] = unserialize($chain['products']); if (!empty($params['full_info'])) { $is_valid = true; if (isset($auth['area']) && $auth['area'] == 'C' && empty($chain['products'])) { unset($chains[$key]); continue; } if (!isset($products[$chain['product_id']])) { unset($chains[$key]); continue; } $chain['products_info'] = $chain['products']; $main_product = $products[$chain['product_id']]; $option_key = $main_product['product_id'] . '_' . $chain['chain_id']; $main_product['selected_options'] = isset($selected_options[$option_key]['selected_options']) ? $selected_options[$option_key]['selected_options'] : ''; $main_product['changed_option'] = isset($selected_options[$option_key]['changed_option']) ? $params['selected_options'][$option_key]['changed_option'] : ''; fn_gather_additional_products_data($main_product, array('get_icon' => true, 'get_detailed' => true, 'get_additional' => false, 'get_options' => true, 'get_discounts' => true)); $chain['product_name'] = $main_product['product']; $chain['chain_amount'] = $main_product['min_qty'] > 0 ? $main_product['min_qty'] : 1; $chain['min_qty'] = $main_product['min_qty'] = 1; $chain['price'] = $main_product['price']; $chain['list_price'] = $main_product['list_price']; $chain['default_options'] = fn_get_default_product_options($main_product['product_id']); $chain['product_options'] = $main_product['product_options']; $chain['options_type'] = $main_product['options_type']; $chain['exceptions_type'] = $main_product['exceptions_type']; $chain['options_update'] = isset($main_product['options_update']) ? $main_product['options_update'] : false; list($chain['discount'], $chain['discounted_price']) = fn_buy_together_calculate_discount($main_product['price'], $chain['modifier'], $chain['modifier_type']); if (isset($main_product['main_pair'])) { $chain['main_pair'] = $main_product['main_pair']; } $total_price = $main_product['price']; $chain_price = $chain['discounted_price']; foreach ($chain['products'] as $hash => &$chain_product) { if (empty($product['product_id'])) { unset($chains[$key]['products'][$hash]); unset($chains[$key]['products_info'][$hash]); continue; } if (!isset($products[$chain_product['product_id']])) { if (isset($auth['area']) && $auth['area'] == 'C') { $is_valid = false; break; } unset($chains[$key]['products'][$hash]); unset($chains[$key]['products_info'][$hash]); continue; } $product = $products[$chain_product['product_id']]; if (AREA == 'C' && ($product['status'] == 'H' || isset($product['tracking']) && $product['tracking'] != ProductTracking::DO_NOT_TRACK && Registry::get('settings.General.show_out_of_stock_products') == 'N' && empty($product['amount']))) { $is_valid = false; break; } if (!empty($chain_product['product_options'])) { $product['selected_options'] = $chain_product['product_options']; } else { $product['selected_options'] = isset($selected_options[$product['product_id']]['selected_options']) ? $selected_options[$product['product_id']]['selected_options'] : ''; $product['changed_option'] = isset($selected_options[$product['product_id']]['changed_option']) ? $selected_options[$product['product_id']]['changed_option'] : ''; } fn_gather_additional_products_data($product, array('get_icon' => true, 'get_detailed' => true, 'get_additional' => false, 'get_options' => true, 'get_discounts' => true)); $product['min_qty'] = $product['min_qty'] > 0 ? $product['min_qty'] : 1; $chain_product['product_name'] = $product['product']; $chain_product['min_qty'] = $product['min_qty']; $chain_product['price'] = empty($chain_product['price']) ? $product['price'] : $chain_product['price']; $chain_product['list_price'] = $product['list_price']; if (isset($product['main_pair'])) { $chain_product['main_pair'] = $product['main_pair']; } list($chain_product['discount'], $chain_product['discounted_price']) = fn_buy_together_calculate_discount($product['price'], empty($chain_product['modifier']) ? 0 : $chain_product['modifier'], empty($chain_product['modifier_type']) ? 'to_fixed' : $chain_product['modifier_type']); $chain_product['options_type'] = $product['options_type']; $chain_product['exceptions_type'] = $product['exceptions_type']; $chain_product['options_update'] = isset($product['options_update']) ? $product['options_update'] : false; $total_price += $product['price'] * $chain_product['amount']; $chain_price += $chain_product['discounted_price'] * $chain_product['amount']; if (!empty($chain_product['product_options'])) { $chain_product['product_options_short'] = $chain_product['product_options']; $options = fn_get_selected_product_options_info($chain_product['product_options'], DESCR_SL); $chain_product['product_options'] = $options; } elseif (!empty($product['product_options'])) { $chain_product['aoc'] = true; // Allow any option combinations $chain_product['options'] = $product['product_options']; } $chains[$key]['products_info'][$hash]['price'] = $chain_product['price']; $chains[$key]['products_info'][$hash]['discount'] = $chain_product['discount']; $chains[$key]['products_info'][$hash]['discounted_price'] = $chain_product['discounted_price']; } if (!$is_valid) { unset($chains[$key]); continue; } $chain['total_price'] = $total_price; $chain['chain_price'] = $chain_price; unset($chain_product); } if (!empty($params['simple'])) { $simple_chain = $chain; break; } } unset($chain); } if (isset($simple_chain)) { $chains = $simple_chain; } /** * Gets function result along with parameters and query information. * * @param array $params Parameters for the function * @param array $auth Array of user authentication data * @param string $lang_code 2-letter language code (e.g. 'en', 'ru', etc.) * @param mixed $chains One chain or array of chains, depending on 'simple' parameter. * @param array $fields Array of fields to be selected * @param array $conditions Array of complete condition expressions to be applied to the end of an SQL-query * @param array $joins List of strings with the complete JOIN information (JOIN type, tables and fields) for an SQL-query */ fn_set_hook('buy_together_get_chains_post', $params, $auth, $lang_code, $chains, $fields, $conditions, $joins); return $chains; }