function fn_reward_points_calculate_cart(&$cart, &$cart_products, &$auth) { if (isset($cart['points_info']['reward'])) { unset($cart['points_info']['reward']); } if (isset($cart['points_info']['additional'])) { $cart['points_info']['reward'] = $cart['points_info']['additional']; unset($cart['points_info']['additional']); } if (isset($cart['points_info']['total_price'])) { unset($cart['points_info']['total_price']); } $reward_coef = $price_coef = 1; if (!empty($cart['subtotal_discount']) && !empty($cart['subtotal'])) { $subtotal_discount_coef = 1 - $cart['subtotal_discount'] / $cart['subtotal']; $reward_points_order_discount = Registry::get('addons.reward_points.reward_points_order_discount'); if ($reward_points_order_discount == 'Y') { $reward_coef = $subtotal_discount_coef; } $price_points_order_discount = Registry::get('addons.reward_points.price_in_points_order_discount'); if ($price_points_order_discount == 'Y') { $price_coef = $subtotal_discount_coef; } } if (!empty($cart_products)) { foreach ($cart_products as $k => $v) { if (!isset($product['exclude_from_calculate'])) { if (isset($cart['products'][$k]['extra']['points_info'])) { unset($cart['products'][$k]['extra']['points_info']); } fn_reward_points_get_additional_product_data($cart_products[$k], $auth, true); if (isset($cart_products[$k]['points_info']['reward'])) { $cart['products'][$k]['extra']['points_info']['reward'] = round($reward_coef * $v['amount'] * (!empty($v['product_options']) ? fn_apply_options_modifiers($cart['products'][$k]['product_options'], $cart_products[$k]['points_info']['reward']['raw_amount'], POINTS_MODIFIER_TYPE) : $cart_products[$k]['points_info']['reward']['raw_amount'])); $cart['points_info']['reward'] = (isset($cart['points_info']['reward']) ? $cart['points_info']['reward'] : 0) + $cart['products'][$k]['extra']['points_info']['reward']; } if (isset($cart_products[$k]['points_info']['raw_price'])) { $cart['products'][$k]['extra']['points_info']['price'] = round($price_coef * $cart_products[$k]['points_info']['raw_price']); $cart['points_info']['total_price'] = (isset($cart['points_info']['total_price']) ? $cart['points_info']['total_price'] : 0) + $cart['products'][$k]['extra']['points_info']['price']; } } } } if (!empty($cart['points_info']['in_use']) && (CONTROLLER == 'checkout' || defined('ORDER_MANAGEMENT') && (MODE == 'totals' || MODE == 'summary'))) { fn_set_point_payment($cart, $cart_products, $auth); if (!empty($cart['points_info']['reward']) && !empty($cart['points_info']['in_use']['points'])) { $cart['points_info']['reward'] = round($cart['points_info']['reward'] * (1 - $cart['points_info']['in_use']['points'] / $cart['points_info']['total_price'])); } } }
function fn_gather_reward_points_data(&$product, &$auth, $get_point_info = true) { // Check, if the product has any option points modifiers if (empty($product['options_update']) && !empty($product['product_options'])) { foreach ($product['product_options'] as $_id => $option) { if (!empty($product['product_options'][$_id]['variants'])) { foreach ($product['product_options'][$_id]['variants'] as $variant) { if (!empty($variant['point_modifier']) && floatval($variant['point_modifier'])) { $product['options_update'] = true; break 2; } } } } } if (isset($product['exclude_from_calculate']) || isset($product['points_info']['reward']) && !(Registry::get('runtime.controller') == 'products' && Registry::get('runtime.mode') == 'options') || $get_point_info == false) { return false; } if (!isset($product['main_category'])) { $product['main_category'] = db_get_field("SELECT category_id FROM ?:products_categories WHERE product_id = ?i AND link_type = 'M'", $product['product_id']); } $candidates = array(PRODUCT_REWARD_POINTS => $product['product_id'], CATEGORY_REWARD_POINTS => $product['main_category'], GLOBAL_REWARD_POINTS => 0); $reward_points = array(); foreach ($candidates as $object_type => $object_id) { $_reward_points = fn_get_reward_points($object_id, $object_type, $auth['usergroup_ids']); if ($object_type == CATEGORY_REWARD_POINTS && !empty($_reward_points)) { // get the "override point" setting $category_is_op = db_get_field("SELECT is_op FROM ?:categories WHERE category_id = ?i", $_reward_points['object_id']); } if ($object_type == CATEGORY_REWARD_POINTS && (empty($_reward_points) || $category_is_op != 'Y')) { // if there is no points of main category of the "override point" setting is disabled // then get point of secondary categories $secondary_categories = db_get_fields("SELECT category_id FROM ?:products_categories WHERE product_id = ?i AND link_type = 'A'", $product['product_id']); if (!empty($secondary_categories)) { $secondary_categories_points = array(); foreach ($secondary_categories as $value) { $_rp = fn_get_reward_points($value, $object_type, $auth['usergroup_ids']); if (isset($_rp['amount'])) { $secondary_categories_points[] = $_rp; } unset($_rp); } if (!empty($secondary_categories_points)) { $sorted_points = fn_sort_array_by_key($secondary_categories_points, 'amount', Registry::get('addons.reward_points.several_points_action') == 'minimal_absolute' || Registry::get('addons.reward_points.several_points_action') == 'minimal_percentage' ? SORT_ASC : SORT_DESC); $_reward_points = array_shift($sorted_points); } } if (!isset($_reward_points['amount'])) { if (Registry::get('addons.reward_points.higher_level_extract') == 'Y' && !empty($candidates[$object_type])) { $id_path = db_get_field("SELECT REPLACE(id_path, '{$candidates[$object_type]}', '') FROM ?:categories WHERE category_id = ?i", $candidates[$object_type]); if (!empty($id_path)) { $c_ids = explode('/', trim($id_path, '/')); $c_ids = array_reverse($c_ids); foreach ($c_ids as $category_id) { $__reward_points = fn_get_reward_points($category_id, $object_type, $auth['usergroup_ids']); if (!empty($__reward_points)) { // get the "override point" setting $_category_is_op = db_get_field("SELECT is_op FROM ?:categories WHERE category_id = ?i", $__reward_points['object_id']); if ($_category_is_op == 'Y') { $category_is_op = $_category_is_op; $_reward_points = $__reward_points; break; } } } } } } } if (!empty($_reward_points) && ($object_type == GLOBAL_REWARD_POINTS || $object_type == PRODUCT_REWARD_POINTS && $product['is_op'] == 'Y' || $object_type == CATEGORY_REWARD_POINTS && (!empty($category_is_op) && $category_is_op == 'Y'))) { // if global points or category points (and override points is enabled) or product points (and override points is enabled) $reward_points = $_reward_points; break; } } if (isset($reward_points['amount'])) { if ((defined('ORDER_MANAGEMENT') || Registry::get('runtime.controller') == 'checkout') && isset($product['subtotal']) && isset($product['original_price'])) { if (Registry::get('addons.reward_points.points_with_discounts') == 'Y' && $reward_points['amount_type'] == 'P' && !empty($product['discounts'])) { $product['discount'] = empty($product['discount']) ? 0 : $product['discount']; $reward_points['coefficient'] = floatval($product['price']) ? ($product['price'] * $product['amount'] - $product['discount']) / $product['price'] * $product['amount'] / pow($product['amount'], 2) : 0; } else { $reward_points['coefficient'] = 1; } } else { $reward_points['coefficient'] = Registry::get('addons.reward_points.points_with_discounts') == 'Y' && $reward_points['amount_type'] == 'P' && isset($product['discounted_price']) ? $product['discounted_price'] / $product['price'] : 1; } if (isset($product['extra']['configuration'])) { if ($reward_points['amount_type'] == 'P') { // for configurable product calc reward points only for base price $price = $product['original_price']; if (!empty($product['discount'])) { $price -= $product['discount']; } $reward_points['amount'] = $price * $reward_points['amount'] / 100; } else { $points_info = Registry::get("runtime.product_configurator.points_info"); if (!empty($points_info[$product['product_id']])) { $reward_points['amount'] = $points_info[$product['product_id']]['reward']; $reward_points['coefficient'] = 1; } } } else { if ($reward_points['amount_type'] == 'P') { $reward_points['amount'] = $product['price'] * $reward_points['amount'] / 100; } } $reward_points['raw_amount'] = $reward_points['coefficient'] * $reward_points['amount']; $reward_points['raw_amount'] = !empty($product['selected_options']) ? fn_apply_options_modifiers($product['selected_options'], $reward_points['raw_amount'], POINTS_MODIFIER_TYPE) : $reward_points['raw_amount']; $reward_points['amount'] = round($reward_points['raw_amount']); $product['points_info']['reward'] = $reward_points; } fn_calculate_product_price_in_points($product, $auth, $get_point_info); }
protected function buildOfferCombination($product, $combination) { $this->offer['items']['param'] = $this->params; $skip = false; foreach ($combination['combination'] as $option_id => $variant_id) { if (!isset($product['product_options'][$option_id]['variants'][$variant_id])) { $skip = true; break; } } if ($this->options['export_stock'] == 'Y') { if ($product['tracking'] == 'O' && $combination['amount'] <= 0) { $this->log->write(Logs::SKIP_PRODUCT, $product, __('yml2_log_product_amount_combination_is_empty')); $skip = true; } } if ($skip) { return false; } if ($product['tracking'] == 'O' && $combination['amount'] <= 0) { $this->offer['attr']['available'] = 'false'; } $product['images'][] = $combination['image_pairs']; while ($image = array_shift($product['images'])) { $offer['items']['picture'][] = $this->getImageUrl($image); } $this->offer['items']['price'] = fn_apply_options_modifiers($combination['combination'], $this->offer['items']['price'], 'P'); $combination_hash = array(); foreach ($combination['combination'] as $option_id => $variant_id) { $combination_hash[] = $option_id; $combination_hash[] = $variant_id; } $this->offer['items']['url'] = $this->escapeUrl('products.view?product_id=' . $product['product_id'] . '&combination=' . implode('_', $combination_hash)); $options = fn_get_selected_product_options($product['product_id'], $combination['combination']); if (!empty($combination['product_code']) && isset($this->offer['items']['vendorCode']) && $this->offer['items']['vendorCode'] == $product['product_code']) { $this->offer['items']['vendorCode'] = $combination['product_code']; } foreach ($options as $option) { if (!empty($option['yml2_type_options'])) { $this->setOfferOptions($option); } } $this->offer['attr']['id'] = $this->generateNewId($options); sort($this->offer['items']['param']); return true; }
function fn_add_product_to_cart($product_data, &$cart, &$auth, $update = false) { $ids = array(); if (!empty($product_data) && is_array($product_data)) { if (!defined('GET_OPTIONS')) { list($product_data, $cart) = fn_add_product_options_files($product_data, $cart, $auth, $update); } fn_set_hook('pre_add_to_cart', $product_data, $cart, $auth, $update); foreach ($product_data as $key => $data) { if (empty($key)) { continue; } if (empty($data['amount'])) { continue; } $data['stored_price'] = !empty($data['stored_price']) && defined('ORDER_MANAGEMENT') ? $data['stored_price'] : 'N'; if (empty($data['extra'])) { $data['extra'] = array(); } $product_id = !empty($data['product_id']) ? intval($data['product_id']) : intval($key); if (!fn_check_add_product_to_cart($cart, $data, $product_id)) { continue; } // Check if product options exist if (!isset($data['product_options'])) { $data['product_options'] = fn_get_default_product_options($product_id); } // Generate cart id $data['extra']['product_options'] = $data['product_options']; $_id = fn_generate_cart_id($product_id, $data['extra'], false); if (isset($ids[$_id]) && $key == $_id) { continue; } if (isset($data['extra']['exclude_from_calculate'])) { if (!empty($cart['products'][$key]) && !empty($cart['products'][$key]['extra']['aoc'])) { $cart['saved_product_options'][$cart['products'][$key]['extra']['saved_options_key']] = $data['product_options']; } if (isset($cart['deleted_exclude_products'][$data['extra']['exclude_from_calculate']][$_id])) { continue; } } $amount = fn_normalize_amount(@$data['amount']); if (!isset($data['extra']['exclude_from_calculate'])) { if ($data['stored_price'] != 'Y') { $allow_add = true; // Check if the product price with options modifiers equals to zero $price = fn_get_product_price($product_id, $amount, $auth); $zero_price_action = db_get_field("SELECT zero_price_action FROM ?:products WHERE product_id = ?i", $product_id); if (!floatval($price) && $zero_price_action == 'A') { if (isset($cart['products'][$key]['custom_user_price'])) { $price = $cart['products'][$key]['custom_user_price']; } else { $custom_user_price = empty($data['price']) ? 0 : $data['price']; } } $price = fn_apply_options_modifiers($data['product_options'], $price, 'P', array(), array('product_data' => $data)); if (!floatval($price)) { $data['price'] = isset($data['price']) ? fn_parse_price($data['price']) : 0; if (($zero_price_action == 'R' || $zero_price_action == 'A' && floatval($data['price']) < 0) && AREA == 'C') { if ($zero_price_action == 'A') { fn_set_notification('E', __('error'), __('incorrect_price_warning')); } $allow_add = false; } $price = empty($data['price']) ? 0 : $data['price']; } /** * Recalculates price and checks if product can be added with the current price * * @param array $data Adding product data * @param float $price Calculated product price * @param boolean $allow_add Flag that determines if product can be added to cart */ fn_set_hook('add_product_to_cart_check_price', $data, $price, $allow_add); if (!$allow_add) { continue; } } else { $price = empty($data['price']) ? 0 : $data['price']; } } else { $price = 0; } $_data = db_get_row('SELECT is_edp, options_type, tracking, unlimited_download FROM ?:products WHERE product_id = ?i', $product_id); if (isset($_data['is_edp'])) { $data['is_edp'] = $_data['is_edp']; } elseif (!isset($data['is_edp'])) { $data['is_edp'] = 0; } if (isset($_data['options_type'])) { $data['options_type'] = $_data['options_type']; } if (isset($_data['tracking'])) { $data['tracking'] = $_data['tracking']; } if (isset($_data['unlimited_download'])) { $data['extra']['unlimited_download'] = $_data['unlimited_download']; } // Check the sequential options if (!empty($data['tracking']) && $data['tracking'] == ProductTracking::TRACK_WITH_OPTIONS && $data['options_type'] == 'S') { $inventory_options = db_get_fields("SELECT a.option_id FROM ?:product_options as a LEFT JOIN ?:product_global_option_links as c ON c.option_id = a.option_id WHERE (a.product_id = ?i OR c.product_id = ?i) AND a.status = 'A' AND a.inventory = 'Y'", $product_id, $product_id); $sequential_completed = true; if (!empty($inventory_options)) { foreach ($inventory_options as $option_id) { if (!isset($data['product_options'][$option_id]) || empty($data['product_options'][$option_id])) { $sequential_completed = false; break; } } } if (!$sequential_completed) { fn_set_notification('E', __('error'), __('select_all_product_options')); // Even if customer tried to add the product from the catalog page, we will redirect he/she to the detailed product page to give an ability to complete a purchase $redirect_url = fn_url('products.view?product_id=' . $product_id . '&combination=' . fn_get_options_combination($data['product_options'])); $_REQUEST['redirect_url'] = $redirect_url; //FIXME: Very very very BAD style to use the global variables in the functions!!! return false; } } if (!isset($cart['products'][$_id])) { // If product doesn't exists in the cart $amount = empty($data['original_amount']) ? fn_check_amount_in_stock($product_id, $amount, $data['product_options'], $_id, $data['is_edp'], 0, $cart, $update == true ? $key : 0) : $data['original_amount']; if ($amount === false) { continue; } $cart['products'][$_id]['product_id'] = $product_id; $cart['products'][$_id]['product_code'] = fn_get_product_code($product_id, $data['product_options']); $cart['products'][$_id]['product'] = fn_get_product_name($product_id); $cart['products'][$_id]['amount'] = $amount; $cart['products'][$_id]['product_options'] = $data['product_options']; $cart['products'][$_id]['price'] = $price; if (!empty($zero_price_action) && $zero_price_action == 'A') { if (isset($custom_user_price)) { $cart['products'][$_id]['custom_user_price'] = $custom_user_price; } elseif (isset($cart['products'][$key]['custom_user_price'])) { $cart['products'][$_id]['custom_user_price'] = $cart['products'][$key]['custom_user_price']; } } $cart['products'][$_id]['stored_price'] = $data['stored_price']; // add image for minicart $cart['products'][$_id]['main_pair'] = fn_get_cart_product_icon($product_id, $data); fn_define_original_amount($product_id, $_id, $cart['products'][$_id], $data); if ($update == true && $key != $_id) { fn_delete_cart_product($cart, $key, false); } } else { // If product is already exist in the cart $_initial_amount = empty($cart['products'][$_id]['original_amount']) ? $cart['products'][$_id]['amount'] : $cart['products'][$_id]['original_amount']; // If ID changed (options were changed), summ the total amount of old and new products if ($update == true && $key != $_id) { $amount += $_initial_amount; fn_delete_cart_product($cart, $key, false); } $cart['products'][$_id]['amount'] = fn_check_amount_in_stock($product_id, ($update == true ? 0 : $_initial_amount) + $amount, $data['product_options'], $_id, !empty($data['is_edp']) && $data['is_edp'] == 'Y' ? 'Y' : 'N', 0, $cart, $update == true ? $key : 0); } $cart['products'][$_id]['extra'] = empty($data['extra']) ? array() : $data['extra']; $cart['products'][$_id]['stored_discount'] = @$data['stored_discount']; if (defined('ORDER_MANAGEMENT')) { $cart['products'][$_id]['discount'] = @$data['discount']; } // Increase product popularity if (empty($_SESSION['products_popularity']['added'][$product_id])) { $_data = array('product_id' => $product_id, 'added' => 1, 'total' => POPULARITY_ADD_TO_CART); db_query("INSERT INTO ?:product_popularity ?e ON DUPLICATE KEY UPDATE added = added + 1, total = total + ?i", $_data, POPULARITY_ADD_TO_CART); $_SESSION['products_popularity']['added'][$product_id] = true; } $company_id = db_get_field("SELECT company_id FROM ?:products WHERE product_id = ?i", $product_id); $cart['products'][$_id]['company_id'] = $company_id; if (!empty($data['saved_object_id'])) { $cart['products'][$_id]['object_id'] = $data['saved_object_id']; } fn_set_hook('add_to_cart', $cart, $product_id, $_id); $ids[$_id] = $product_id; } /** * Change product data after adding product to cart * * @param array $product_data Product data * @param array $cart Cart data * @param array $auth Auth data * @param bool $update Flag the determains if cart data are updated */ fn_set_hook('post_add_to_cart', $product_data, $cart, $auth, $update, $ids); $cart['recalculate'] = true; if (!empty($cart['chosen_shipping'])) { $cart['calculate_shipping'] = true; unset($cart['product_groups']); } return $ids; } else { return false; } }
function fn_calculate_price_of_a_product($product, $selected_options) { if (empty($product['modifiers_price'])) { $addon_price_calculation_price = fn_get_price_by_selected_options($product['product_id'], $product, $selected_options); if (intval($addon_price_calculation_price) > 0) { $product['price'] = $addon_price_calculation_price; } else { $product['price'] = fn_apply_options_modifiers($selected_options, $product['price'], 'P', array(), array('product_data' => $product)); } } return $product['price']; }
function fn_reorder($order_id, &$cart, &$auth) { $order_info = fn_get_order_info($order_id, false, false, false, true); unset(Tygh::$app['session']['shipping_hash']); unset(Tygh::$app['session']['edit_step']); fn_set_hook('reorder', $order_info, $cart, $auth); foreach ($order_info['products'] as $k => $item) { // refresh company id $company_id = db_get_field("SELECT company_id FROM ?:products WHERE product_id = ?i", $item['product_id']); $order_info['products'][$k]['company_id'] = $company_id; unset($order_info['products'][$k]['extra']['ekey_info']); unset($order_info['products'][$k]['extra']['promotions']); unset($order_info['products'][$k]['promotions']); $order_info['products'][$k]['product_options'] = empty($order_info['products'][$k]['extra']['product_options']) ? array() : $order_info['products'][$k]['extra']['product_options']; $order_info['products'][$k]['main_pair'] = fn_get_cart_product_icon($item['product_id'], $order_info['products'][$k]); } if (!empty($cart) && !empty($cart['products'])) { $cart['products'] = fn_array_merge($cart['products'], $order_info['products']); } else { $cart['products'] = $order_info['products']; } foreach ($cart['products'] as $k => $v) { $_is_edp = db_get_field("SELECT is_edp FROM ?:products WHERE product_id = ?i", $v['product_id']); if ($amount = fn_check_amount_in_stock($v['product_id'], $v['amount'], $v['product_options'], $k, $_is_edp, 0, $cart)) { $cart['products'][$k]['amount'] = $amount; // Check if the product price with options modifiers equals to zero $price = fn_get_product_price($v['product_id'], $amount, $auth); $zero_price_action = db_get_field("SELECT zero_price_action FROM ?:products WHERE product_id = ?i", $v['product_id']); if (!floatval($price) && $zero_price_action == 'A') { if (isset($v['custom_user_price'])) { $price = $v['custom_user_price']; } } $price = fn_apply_options_modifiers($v['product_options'], $price, 'P', array(), array('product_data' => $v)); if (!floatval($price)) { $data['price'] = isset($data['price']) ? fn_parse_price($data['price']) : 0; if (AREA == 'C' && ($zero_price_action == 'R' || $zero_price_action == 'A' && floatval($data['price']) < 0)) { if ($zero_price_action == 'A') { fn_set_notification('E', __('error'), __('incorrect_price_warning')); } else { fn_set_notification('W', __('warning'), __('warning_zero_price_restricted_product', array('[product]' => $v['product']))); } unset($cart['products'][$k]); continue; } } // Change the path of custom files if (!empty($v['extra']['custom_files'])) { foreach ($v['extra']['custom_files'] as $option_id => $_data) { if (!empty($_data)) { foreach ($_data as $file_id => $file) { $cart['products'][$k]['extra']['custom_files'][$option_id][$file_id]['path'] = 'sess_data/' . fn_basename($file['path']); } } } } } else { unset($cart['products'][$k]); } } // Restore custom files for editing $dir_path = 'order_data/' . $order_id; if (Storage::instance('custom_files')->isExist($dir_path)) { Storage::instance('custom_files')->copy($dir_path, 'sess_data'); } // Redirect customer to step three after reordering $cart['payment_updated'] = true; fn_save_cart_content($cart, $auth['user_id']); unset($cart['product_groups']); }
function fn_add_product_to_cart($product_data, &$cart, &$auth, $update = false) { $ids = array(); if (!empty($product_data) && is_array($product_data)) { if (!defined('GET_OPTIONS')) { list($product_data, $cart) = fn_add_product_options_files($product_data, $cart, $auth, $update); } fn_set_hook('pre_add_to_cart', $product_data, $cart, $auth, $update); foreach ($product_data as $key => $data) { if (empty($key)) { continue; } if (empty($data['amount'])) { continue; } $data['stored_price'] = !empty($data['stored_price']) && AREA != 'C' ? $data['stored_price'] : 'N'; if (empty($data['extra'])) { $data['extra'] = array(); } $product_id = !empty($data['product_id']) ? $data['product_id'] : $key; // Check if product options exist if (!isset($data['product_options'])) { $data['product_options'] = fn_get_default_product_options($product_id); } // Generate cart id $data['extra']['product_options'] = $data['product_options']; $_id = fn_generate_cart_id($product_id, $data['extra'], false); if (isset($data['extra']['exclude_from_calculate'])) { if (!empty($cart['products'][$key]) && !empty($cart['products'][$key]['extra']['aoc'])) { $cart['saved_product_options'][$cart['products'][$key]['extra']['saved_options_key']] = $data['product_options']; } if (isset($cart['deleted_exclude_products'][$data['extra']['exclude_from_calculate']][$_id])) { continue; } } $amount = fn_normalize_amount(@$data['amount']); if (!isset($data['extra']['exclude_from_calculate'])) { if ($data['stored_price'] != 'Y') { // Check if the product price with options modifiers equals to zero $price = fn_get_product_price($product_id, $amount, $auth); $price = fn_apply_options_modifiers($data['product_options'], $price, 'P'); if (!floatval($price)) { $data['price'] = isset($data['price']) ? fn_parse_price($data['price']) : 0; $zero_price_action = db_get_field("SELECT zero_price_action FROM ?:products WHERE product_id = ?i", $product_id); if (($zero_price_action == 'R' || $zero_price_action == 'A' && floatval($data['price']) < 0) && AREA == 'C') { if ($zero_price_action == 'A') { fn_set_notification('E', fn_get_lang_var('error'), fn_get_lang_var('incorrect_price_warning')); } continue; } $price = empty($data['price']) ? 0 : $data['price']; } } else { $price = empty($data['price']) ? 0 : $data['price']; } } else { $price = 0; } $data['is_edp'] = db_get_field('SELECT is_edp FROM ?:products WHERE product_id = ?i', $product_id); if (!isset($cart['products'][$_id])) { // If product doesn't exists in the cart $amount = empty($data['original_amount']) ? fn_check_amount_in_stock($product_id, $amount, $data['product_options'], $_id, $data['is_edp'], 0, $cart, $update == true ? $key : 0) : $data['original_amount']; if ($amount === false) { continue; } $cart['products'][$_id]['product_id'] = $product_id; $cart['products'][$_id]['amount'] = $amount; $cart['products'][$_id]['product_options'] = $data['product_options']; $cart['products'][$_id]['price'] = $price; $cart['products'][$_id]['stored_price'] = $data['stored_price']; fn_define_original_amount($product_id, $_id, $cart['products'][$_id], $data); if ($update == true && $key != $_id) { unset($cart['products'][$key]); } } else { // If product is already exist in the cart $_initial_amount = empty($cart['products'][$_id]['original_amount']) ? $cart['products'][$_id]['amount'] : $cart['products'][$_id]['original_amount']; // If ID changed (options were changed), summ the total amount of old and new products if ($update == true && $key != $_id) { $amount += $_initial_amount; unset($cart['products'][$key]); } $cart['products'][$_id]['amount'] = fn_check_amount_in_stock($product_id, ($update == true ? 0 : $_initial_amount) + $amount, $data['product_options'], $_id, !empty($data['is_edp']) && $data['is_edp'] == 'Y' ? 'Y' : 'N', 0, $cart, $update == true ? $key : 0); } $cart['products'][$_id]['extra'] = empty($data['extra']) ? array() : $data['extra']; $cart['products'][$_id]['stored_discount'] = @$data['stored_discount']; if (defined('ORDER_MANAGEMENT')) { $cart['products'][$_id]['discount'] = @$data['discount']; } // Increase product popularity if (empty($_SESSION['products_popularity']['added'][$product_id])) { $_data = array('product_id' => $product_id, 'added' => 1, 'total' => POPULARITY_ADD_TO_CART); db_query("INSERT INTO ?:product_popularity ?e ON DUPLICATE KEY UPDATE added = added + 1, total = total + ?i", $_data, POPULARITY_ADD_TO_CART); $_SESSION['products_popularity']['added'][$product_id] = true; } $company_id = db_get_field("SELECT company_id FROM ?:products WHERE product_id = ?i", $product_id); $cart['products'][$_id]['company_id'] = $company_id; fn_set_hook('add_to_cart', $cart, $product_id, $_id); $ids[$_id] = $product_id; } $cart['recalculate'] = true; return $ids; } else { return false; } }
/** * Gets additional products data * * @param array $products Array with products * @param array $params Array of flags which determines which data should be gathered * @return array Array of products with additional information */ function fn_gather_additional_products_data(&$products, $params) { /** * Change parameters for gathering additional products data * * @param array $products List of products * @param array $params Parameters for gathering data */ fn_set_hook('gather_additional_products_data_pre', $products, $params); if (empty($products)) { return; } // Set default values to input params $default_params = array('get_icon' => false, 'get_detailed' => false, 'get_additional' => false, 'get_options' => true, 'get_discounts' => true, 'get_features' => false, 'get_extra' => false, 'get_taxed_prices' => true, 'get_for_one_product' => !is_array(reset($products)) ? true : false, 'detailed_params' => true, 'features_display_on' => 'C'); $params = array_merge($default_params, $params); $auth =& $_SESSION['auth']; $allow_negative_amount = Registry::get('settings.General.allow_negative_amount'); $inventory_tracking = Registry::get('settings.General.inventory_tracking'); if ($params['get_for_one_product']) { $products = array($products); } $product_ids = array(); foreach ($products as $v) { $product_ids[] = $v['product_id']; } if ($params['get_icon'] || $params['get_detailed']) { $products_images = fn_get_image_pairs($product_ids, 'product', 'M', $params['get_icon'], $params['get_detailed'], CART_LANGUAGE); } if ($params['get_additional']) { $additional_images = fn_get_image_pairs($product_ids, 'product', 'A', true, true, CART_LANGUAGE); } if ($params['get_options']) { $product_options = fn_get_product_options($product_ids, CART_LANGUAGE); } else { $has_product_options = db_get_hash_array("SELECT a.option_id, a.product_id FROM ?:product_options AS a WHERE a.product_id IN (?n) AND a.status = 'A'", 'product_id', $product_ids); $has_product_options_links = db_get_hash_array("SELECT c.option_id, c.product_id FROM ?:product_global_option_links AS c LEFT JOIN ?:product_options AS a ON a.option_id = c.option_id WHERE a.status = 'A' AND c.product_id IN (?n)", 'product_id', $product_ids); } /** * Changes before gathering additional products data * * @param array $product_ids Array of product identifiers * @param array $params Parameteres for gathering data * @param array $products Array of products * @param mixed $auth Array of user authentication data * @param array $products_images Array with product main images * @param array $additional_images Array with product additional images * @param array $product_options Array with product options * @param array $has_product_options Array of flags determines if product has options * @param array $has_product_options_links Array of flags determines if product has option links */ fn_set_hook('gather_additional_products_data_params', $product_ids, $params, $products, $auth, $products_images, $additional_images, $product_options, $has_product_options, $has_product_options_links); // foreach $products foreach ($products as &$_product) { $product = $_product; $product_id = $product['product_id']; // Get images if ($params['get_icon'] == true || $params['get_detailed'] == true) { if (empty($product['main_pair']) && !empty($products_images[$product_id])) { $product['main_pair'] = reset($products_images[$product_id]); } } if ($params['get_additional'] == true) { if (empty($product['image_pairs']) && !empty($additional_images[$product_id])) { $product['image_pairs'] = $additional_images[$product_id]; } } if (!isset($product['base_price'])) { $product['base_price'] = $product['price']; // save base price (without discounts, etc...) } /** * Changes before gathering product options * * @param array $product Product data * @param mixed $auth Array of user authentication data * @param array $params Parameteres for gathering data */ fn_set_hook('gather_additional_product_data_before_options', $product, $auth, $params); // Convert product categories if (!empty($product['category_ids']) && !is_array($product['category_ids'])) { list($product['category_ids'], $product['main_category']) = fn_convert_categories($product['category_ids']); } $product['selected_options'] = empty($product['selected_options']) ? array() : $product['selected_options']; // Get product options if ($params['get_options'] && !empty($product_options[$product['product_id']])) { if (!isset($product['options_type']) || !isset($product['exceptions_type'])) { $types = db_get_row('SELECT options_type, exceptions_type FROM ?:products WHERE product_id = ?i', $product['product_id']); $product['options_type'] = $types['options_type']; $product['exceptions_type'] = $types['exceptions_type']; } if (empty($product['product_options'])) { if (!empty($product['combination'])) { $selected_options = fn_get_product_options_by_combination($product['combination']); } $product['product_options'] = !empty($selected_options) ? fn_get_selected_product_options($product['product_id'], $selected_options, CART_LANGUAGE) : $product_options[$product_id]; } $product = fn_apply_options_rules($product); if (!empty($params['get_icon']) || !empty($params['get_detailed'])) { // Get product options images if (!empty($product['combination_hash']) && !empty($product['product_options'])) { $image = fn_get_image_pairs($product['combination_hash'], 'product_option', 'M', $params['get_icon'], $params['get_detailed'], CART_LANGUAGE); if (!empty($image)) { $product['main_pair'] = $image; } } } $product['has_options'] = !empty($product['product_options']); if (!fn_allowed_for('ULTIMATE:FREE')) { $product = fn_apply_exceptions_rules($product); } //var_dump($product['product_options']);die(); //var_dump($selected_options); // Change price $selected_options = isset($product['selected_options']) ? $product['selected_options'] : array(); foreach ($product['product_options'] as $option) { if (!empty($option['disabled'])) { unset($selected_options[$option['option_id']]); } } $product['selected_options'] = $selected_options; if (empty($product['modifiers_price'])) { $product['base_modifier'] = fn_apply_options_modifiers($selected_options, $product['base_price'], 'P', array(), array('product_data' => $product)); $old_price = $product['price']; $product['price'] = fn_apply_options_modifiers($selected_options, $product['price'], 'P', array(), array('product_data' => $product)); if (empty($product['original_price'])) { $product['original_price'] = $old_price; } $product['original_price'] = fn_apply_options_modifiers($selected_options, $product['original_price'], 'P', array(), array('product_data' => $product)); $product['modifiers_price'] = $product['price'] - $old_price; } if (!empty($product['list_price'])) { $product['list_price'] = fn_apply_options_modifiers($selected_options, $product['list_price'], 'P', array(), array('product_data' => $product)); } if (!empty($product['prices']) && is_array($product['prices'])) { foreach ($product['prices'] as $pr_k => $pr_v) { $product['prices'][$pr_k]['price'] = fn_apply_options_modifiers($selected_options, $pr_v['price'], 'P', array(), array('product_data' => $product)); } } } else { $product['has_options'] = !empty($has_product_options[$product_id]) || !empty($has_product_options_links[$product_id]) ? true : false; $product['product_options'] = empty($product['product_options']) ? array() : $product['product_options']; } unset($selected_options); /** * Changes before gathering product discounts * * @param array $product Product data * @param mixed $auth Array of user authentication data * @param array $params Parameteres for gathering data */ fn_set_hook('gather_additional_product_data_before_discounts', $product, $auth, $params); // Get product discounts if ($params['get_discounts'] && !isset($product['exclude_from_calculate'])) { fn_promotion_apply('catalog', $product, $auth); if (!empty($product['prices']) && is_array($product['prices'])) { $product_copy = $product; foreach ($product['prices'] as $pr_k => $pr_v) { $product_copy['base_price'] = $product_copy['price'] = $pr_v['price']; fn_promotion_apply('catalog', $product_copy, $auth); $product['prices'][$pr_k]['price'] = $product_copy['price']; } } if (empty($product['discount']) && !empty($product['list_price']) && !empty($product['price']) && floatval($product['price']) && $product['list_price'] > $product['price']) { $product['list_discount'] = fn_format_price($product['list_price'] - $product['price']); $product['list_discount_prc'] = sprintf('%d', round($product['list_discount'] * 100 / $product['list_price'])); } } // FIXME: old product options scheme $product['discounts'] = array('A' => 0, 'P' => 0); if (!empty($product['promotions'])) { foreach ($product['promotions'] as $v) { foreach ($v['bonuses'] as $a) { if ($a['discount_bonus'] == 'to_fixed') { $product['discounts']['A'] += $a['discount']; } elseif ($a['discount_bonus'] == 'by_fixed') { $product['discounts']['A'] += $a['discount_value']; } elseif ($a['discount_bonus'] == 'to_percentage') { $product['discounts']['P'] += 100 - $a['discount_value']; } elseif ($a['discount_bonus'] == 'by_percentage') { $product['discounts']['P'] += $a['discount_value']; } } } } // Add product prices with taxes and without taxes if ($params['get_taxed_prices'] && AREA != 'A' && Registry::get('settings.Appearance.show_prices_taxed_clean') == 'Y' && $auth['tax_exempt'] != 'Y') { fn_get_taxed_and_clean_prices($product, $auth); } if ($params['get_features'] && !isset($product['product_features'])) { $product['product_features'] = fn_get_product_features_list($product, $params['features_display_on']); } if ($params['get_extra'] && !empty($product['is_edp']) && $product['is_edp'] == 'Y') { $product['agreement'] = array(fn_get_edp_agreements($product['product_id'])); } $product['qty_content'] = fn_get_product_qty_content($product, $allow_negative_amount, $inventory_tracking); if ($params['detailed_params']) { $product['detailed_params'] = empty($product['detailed_params']) ? $params : array_merge($product['detailed_params'], $params); } /** * Add additional data to product * * @param array $product Product data * @param mixed $auth Array of user authentication data * @param array $params Parameteres for gathering data */ fn_set_hook('gather_additional_product_data_post', $product, $auth, $params); $_product = $product; } // \foreach $products /** * Add additional data to products after gathering additional products data * * @param array $product_ids Array of product identifiers * @param array $params Parameteres for gathering data * @param array $products Array of products * @param mixed $auth Array of user authentication data */ fn_set_hook('gather_additional_products_data_post', $product_ids, $params, $products, $auth); if ($params['get_for_one_product'] == true) { $products = array_shift($products); } }
function fn_recurring_billing_get_additional_product_data_before_discounts(&$product, $auth, $get_options) { if (AREA != 'A' && !empty($product['product_id']) && empty($product['extra']['recurring_price_calculated']) && empty($product['extra']['parent']) && empty($product['exclude_from_calculate'])) { $plan_ids = db_get_fields("SELECT plan_id FROM ?:recurring_plans WHERE status = 'A' AND FIND_IN_SET(?i, product_ids)", $product['product_id']); if (!empty($plan_ids)) { $plans = array(); $base_price = $product['base_price']; $_free_buy = false; if ($cut_plan_id = Registry::get('recurring_plan_id')) { $product['extra']['recurring_plan_id'] = $cut_plan_id; Registry::del('recurring_plan_id'); } foreach ($plan_ids as $ind => $id) { $plans[$id] = fn_get_recurring_plan_data($id); $price_cond = empty($plans[$id]['start_duration']) ? $plans[$id]['price'] : $plans[$id]['start_price']; $plans[$id]['base_price'] = fn_apply_options_modifiers($product['selected_options'], fn_calculate_recurring_price($base_price, $price_cond), 'P'); if ($plans[$id]['base_price'] < 0) { $plans[$id]['base_price'] = 0; } $plans[$id]['last_base_price'] = fn_apply_options_modifiers($product['selected_options'], fn_calculate_recurring_price($base_price, $plans[$id]['price']), 'P'); if ($plans[$id]['last_base_price'] < 0) { $plans[$id]['last_base_price'] = 0; } if ($plans[$id]['allow_free_buy'] == 'Y') { $_free_buy = true; } if ($ind == 0 && empty($product['extra']['recurring_plan_id']) || !empty($product['extra']['recurring_plan_id']) && $product['extra']['recurring_plan_id'] == $id) { $_base_price = $plans[$id]['base_price']; } } if ($_free_buy) { array_unshift($plans, array('plan_id' => 0, 'base_price' => $base_price, 'last_base_price' => $base_price)); } if (!$_free_buy || $_free_buy && !empty($product['extra']['recurring_plan_id'])) { $product['price'] = $product['base_price'] = $product['original_price'] = $product['display_price'] = $product['display_subtotal'] = $product['subtotal'] = $_base_price; } $product['recurring_plans'] = $plans; $product['extra']['recurring_price_calculated'] = true; } } if (!empty($product['recurring_plan_id']) && isset($product['recurring_plans'][$product['recurring_plan_id']])) { $product['price'] = $product['base_price'] = $product['original_price'] = $product['display_price'] = $product['display_subtotal'] = $product['subtotal'] = $product['recurring_plans'][$product['recurring_plan_id']]['base_price']; } }
function fn_apply_options_rules($product) { /* Options type: P - simultaneous/parallel S - sequential */ // Check for the options and exceptions types if (!isset($product['options_type']) || !isset($product['exceptions_type'])) { $product = array_merge($product, db_get_row('SELECT options_type, exceptions_type FROM ?:products WHERE product_id = ?i', $product['product_id'])); } // Get the selected options or get the default options $product['selected_options'] = empty($product['selected_options']) ? array() : $product['selected_options']; $product['options_update'] = $product['options_type'] == 'S' ? true : false; // Conver the selected options text to the utf8 format if (!empty($product['product_options'])) { foreach ($product['product_options'] as $id => $option) { if (!empty($option['value'])) { $product['product_options'][$id]['value'] = fn_unicode_to_utf8($option['value']); } if (!empty($product['selected_options'][$option['option_id']])) { $product['selected_options'][$option['option_id']] = fn_unicode_to_utf8($product['selected_options'][$option['option_id']]); } } } $selected_options =& $product['selected_options']; $changed_option = empty($product['changed_option']) ? true : false; $simultaneous = array(); $next = 0; foreach ($product['product_options'] as $_id => $option) { $simultaneous[$next] = $option['option_id']; $next = $option['option_id']; if (!empty($option['value'])) { $selected_options[$option['option_id']] = $option['value']; } if (!$changed_option && $product['changed_option'] == $option['option_id']) { $changed_option = true; } if (!empty($selected_options[$option['option_id']]) && ($selected_options[$option['option_id']] == 'checked' || $selected_options[$option['option_id']] == 'unchecked') && $option['option_type'] == 'C') { foreach ($option['variants'] as $variant) { if ($variant['position'] == 0 && $selected_options[$option['option_id']] == 'unchecked' || $variant['position'] == 1 && $selected_options[$option['option_id']] == 'checked') { $selected_options[$option['option_id']] = $variant['variant_id']; if ($changed_option) { $product['changed_option'] = $option['option_id']; } } } } // Check, if the product has any options modifiers if (!empty($product['product_options'][$_id]['variants'])) { foreach ($product['product_options'][$_id]['variants'] as $variant) { if (!empty($variant['modifier']) && floatval($variant['modifier'])) { $product['options_update'] = true; } } } } if (!empty($product['changed_option']) && empty($selected_options[$product['changed_option']]) && $product['options_type'] == 'S') { $product['changed_option'] = array_search($product['changed_option'], $simultaneous); if ($product['changed_option'] == 0) { unset($product['changed_option']); if (!empty($selected_options)) { foreach ($selected_options as $option_id => $variant_id) { if (!in_array($option['option_type'], array('I', 'T', 'F'))) { unset($selected_options[$option_id]); } } } } } if (empty($selected_options) && $product['options_type'] == 'P') { $selected_options = fn_get_default_product_options($product['product_id'], true); } if (empty($product['changed_option'])) { end($selected_options); $product['changed_option'] = key($selected_options); } if ($product['options_type'] == 'S') { empty($product['changed_option']) ? $allow = 1 : ($allow = 0); foreach ($product['product_options'] as $_id => $option) { $product['product_options'][$_id]['disabled'] = false; if (in_array($option['option_type'], array('I', 'T', 'F'))) { continue; } $option_id = $option['option_id']; if ($allow >= 1) { unset($selected_options[$option_id]); $product['product_options'][$_id]['value'] = ''; } if ($allow >= 2) { $product['product_options'][$_id]['disabled'] = true; continue; } if (empty($product['changed_option']) || !empty($product['changed_option']) && $product['changed_option'] == $option_id || $allow > 0) { $allow++; } } $product['simultaneous'] = $simultaneous; } // Restore selected values if (!empty($selected_options)) { foreach ($product['product_options'] as $_id => $option) { if (isset($selected_options[$option['option_id']])) { $product['product_options'][$_id]['value'] = $selected_options[$option['option_id']]; } } } // Change price if (empty($product['modifiers_price'])) { $product['base_modifier'] = fn_apply_options_modifiers($selected_options, $product['base_price'], 'P'); $old_price = $product['price']; $product['price'] = $product['original_price'] = fn_apply_options_modifiers($selected_options, $product['price'], 'P'); $product['modifiers_price'] = $product['price'] - $old_price; } if (!empty($product['list_price'])) { $product['list_price'] = fn_apply_options_modifiers($selected_options, $product['list_price'], 'P'); } // Generate combination hash to get images. (Also, if the tracking with options, get amount and product code) $combination_hash = fn_generate_cart_id($product['product_id'], array('product_options' => $selected_options), true); $product['combination_hash'] = $combination_hash; // Change product code and amount if (!empty($product['tracking']) && $product['tracking'] == 'O') { $combination = db_get_row("SELECT product_code, amount FROM ?:product_options_inventory WHERE combination_hash = ?i", $combination_hash); if (!empty($combination['product_code'])) { $product['product_code'] = $combination['product_code']; } if (isset($combination['amount'])) { $product['inventory_amount'] = $combination['amount']; } else { $product['inventory_amount'] = $product['amount'] = 0; } } if (!$product['options_update']) { $product['options_update'] = db_get_field('SELECT COUNT(*) FROM ?:product_options_inventory WHERE product_id = ?i', $product['product_id']); } fn_set_hook('apply_options_rules', $product); return $product; }