Example #1
0
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;
}
Example #2
0
/**
 * Add gift certificate to wishlist
 *
 * @param array $wishlist wishlist data storage
 * @param array $gift_cert_data array with data for the certificate to add
 * @return array array with gift certificate ID and data if addition is successful and empty array otherwise
 */
function fn_add_gift_certificate_to_wishlist(&$wishlist, $gift_cert_data)
{
    if (!empty($gift_cert_data) && is_array($gift_cert_data)) {
        fn_correct_gift_certificate($gift_cert_data);
        // Generate wishlist id
        $gift_cert_wishlist_id = fn_generate_gift_certificate_cart_id($gift_cert_data);
        $wishlist['gift_certificates'][$gift_cert_wishlist_id] = $gift_cert_data;
        $gift_cert_data['display_subtotal'] = $gift_cert_data['amount'];
        if (!empty($gift_cert_data['products'])) {
            $product_data = array();
            foreach ($gift_cert_data['products'] as $w_id => $_data) {
                if (empty($_data['amount'])) {
                    unset($gift_cert_data['products'][$w_id]);
                    continue;
                }
                if (empty($_data['product_options'])) {
                    $_data['product_options'] = fn_get_default_product_options($_data['product_id']);
                }
                $wishlist_id = fn_generate_cart_id($_data['product_id'], array('product_options' => $_data['product_options'], 'parent' => array('certificate' => $gift_cert_wishlist_id)), true);
                $product_data[$wishlist_id] = $_data;
                $wishlist['products'][$wishlist_id] = array('product_id' => $_data['product_id'], 'product_options' => $_data['product_options'], 'amount' => $_data['amount'], 'extra' => array('parent' => array('certificate' => $gift_cert_wishlist_id)));
                $product = fn_get_product_data($_data['product_id'], $_SESSION['auth']);
                $gift_cert_data['display_subtotal'] += $_data['amount'] * $product['price'];
            }
            $gift_cert_data['products'] = $wishlist['gift_certificates'][$gift_cert_wishlist_id]['products'] = $product_data;
        }
        return array($gift_cert_wishlist_id, $gift_cert_data);
    } else {
        return array();
    }
}
Example #3
0
/**
 * Add product to wishlist
 *
 * @param array $product_data array with data for the product to add)(product_id, price, amount, product_options, is_edp)
 * @param array $wishlist wishlist data storage
 * @param array $auth user session data
 * @return mixed array with wishlist IDs for the added products, false otherwise
 */
function fn_add_product_to_wishlist($product_data, &$wishlist, &$auth)
{
    // Check if products have cusom images
    list($product_data, $wishlist) = fn_add_product_options_files($product_data, $wishlist, $auth, false, 'wishlist');
    fn_set_hook('pre_add_to_wishlist', $product_data, $wishlist, $auth);
    if (!empty($product_data) && is_array($product_data)) {
        $wishlist_ids = array();
        foreach ($product_data as $product_id => $data) {
            if (empty($data['amount'])) {
                $data['amount'] = 1;
            }
            if (!empty($data['product_id'])) {
                $product_id = $data['product_id'];
            }
            if (empty($data['extra'])) {
                $data['extra'] = array();
            }
            // Add one product
            if (!isset($data['product_options'])) {
                $data['product_options'] = fn_get_default_product_options($product_id);
            }
            // Generate wishlist id
            $data['extra']['product_options'] = $data['product_options'];
            $_id = fn_generate_cart_id($product_id, $data['extra']);
            //$_SESSION['tessssssssssssssssssssssssssssssssssssssstttttttttttt'] = $data['extra'];
            $_data = db_get_row('SELECT is_edp, options_type, tracking FROM ?:products WHERE product_id = ?i', $product_id);
            $data['is_edp'] = $_data['is_edp'];
            $data['options_type'] = $_data['options_type'];
            $data['tracking'] = $_data['tracking'];
            // Check the sequential options
            if (!empty($data['tracking']) && $data['tracking'] == 'O' && $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;
                }
            }
            $wishlist_ids[] = $_id;
            $wishlist['products'][$_id]['product_id'] = $product_id;
            $wishlist['products'][$_id]['product_options'] = $data['product_options'];
            $wishlist['products'][$_id]['extra'] = $data['extra'];
            $wishlist['products'][$_id]['amount'] = $data['amount'];
        }
        return $wishlist_ids;
    } else {
        return false;
    }
}
Example #4
0
/**
 * Checks product list and adds required products
 *
 * @param array $product_data Products data
 * @param mixed $auth Array with authorization data
 * @param array $cart
 * @param array $added_products Products that are checked for further products that need to be added
 * @return bool False if some products were removed, otherwise - true
 */
function fn_check_added_required_products(&$product_data, $auth, &$cart, $added_products = array())
{
    $result = true;
    foreach ($product_data as $key => $entry) {
        if (!empty($entry['amount']) && !empty($key)) {
            $product_id = !empty($entry['product_id']) ? $entry['product_id'] : $key;
            $added_products[$product_id] = $entry;
            $ids = fn_get_required_products_ids($product_id);
            if (!empty($ids)) {
                $have = fn_required_products_get_existent($auth, $ids, $cart);
                if (empty($have) || count($have) != count($ids)) {
                    $products_to_cart = array_diff($ids, $have);
                    $out_of_stock = array();
                    $check_products = array();
                    foreach ($products_to_cart as $id) {
                        if (!empty($added_products[$id])) {
                            continue;
                        }
                        $amount = fn_check_amount_in_stock($id, 1, fn_get_default_product_options($id), 0, 'N', 0, $cart);
                        if (!$amount) {
                            $out_of_stock[] = $id;
                        } else {
                            $check_products[$id] = array('product_id' => $id, 'amount' => $amount);
                        }
                    }
                    if (empty($out_of_stock) && fn_check_added_required_products($check_products, $auth, $cart, $added_products)) {
                        $cart['change_cart_products'] = true;
                        $msg = __('required_products_added');
                        foreach ($check_products as $id => $v) {
                            if (empty($added_products[$id])) {
                                $added_products[$id] = $v;
                                $product_data[$id] = $v;
                                $msg .= "<br />" . fn_get_product_name($id);
                                $cart['amount'] = !isset($cart['amount']) ? $v['amount'] : $cart['amount'] + $v['amount'];
                            }
                        }
                    } else {
                        unset($product_data[$key]);
                        unset($added_products[$product_id]);
                        $result = false;
                        $msg = __('required_products_out_of_stock');
                        foreach ($out_of_stock as $id) {
                            $msg .= "<br />" . fn_get_product_name($id);
                        }
                    }
                    fn_set_notification('N', __('notice'), $msg);
                }
            }
        }
    }
    return $result;
}
Example #5
0
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_apply_options_rules($product)
{
    /**
     * Changes product data before applying product options rules
     *
     * @param array $product Product data
     */
    fn_set_hook('apply_options_rules_pre', $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) {
        if (!in_array($option['option_type'], array('I', 'T', 'F'))) {
            $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']);
            $reset = true;
            if (!empty($selected_options)) {
                foreach ($selected_options as $option_id => $variant_id) {
                    if (!isset($product['product_options'][$option_id]) || !in_array($product['product_options'][$option_id]['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, $product);
    }
    if (empty($product['changed_option']) && isset($reset)) {
        $product['changed_option'] = '';
    } elseif (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;
    }
    //        echo "selected_options :";var_dump($selected_options);
    // 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']];
            }
        }
    }
    // Generate combination hash to get images. (Also, if the tracking with options, get amount and product code)
    $inventoryOptions = db_get_fields("SELECT a.option_id FROM ?:product_options as a LEFT JOIN ?:product_global_option_links as b ON a.option_id = b.option_id WHERE (a.product_id = ?i OR b.product_id = ?i) AND a.option_type IN ('S','R','C','Y') AND a.inventory = 'Y' ORDER BY position DESC", $product_id, $product_id);
    $defaultSelectedProductOptions = fn_get_default_product_options($product_id, true, $product);
    $selectedOptions = array();
    foreach ($inventoryOptions as $inventoryOption) {
        if ($defaultSelectedProductOptions[$inventoryOption]) {
            $selectedOptions[$inventoryOption] = $defaultSelectedProductOptions[$inventoryOption];
        }
    }
    $combination_hash = fn_generate_cart_id($product['product_id'], array('product_options' => $selected_options), true);
    //  echo "<br>fn_generate_cart_id product page  combination hash={$combination_hash} product options ";
    //  var_dump($selected_options);
    //   echo '<hr>';
    //   echo 'selected_options '.$selected_options;
    $product['combination_hash'] = $combination_hash;
    // Change product code and amount
    if (!empty($product['tracking']) && $product['tracking'] == 'O') {
        $product['hide_stock_info'] = false;
        if ($product['options_type'] == 'S') {
            foreach ($product['product_options'] as $option) {
                $option_id = $option['option_id'];
                if ($option['inventory'] == 'Y' && empty($product['selected_options'][$option_id])) {
                    $product['hide_stock_info'] = true;
                    break;
                }
            }
        }
        if (!$product['hide_stock_info']) {
            $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 (Registry::get('settings.General.inventory_tracking') == 'Y') {
                if (isset($combination['amount'])) {
                    $product['inventory_amount'] = $combination['amount'];
                    //   echo 'combination amount set: ';
                } else {
                    $product['inventory_amount'] = $product['amount'] = 0;
                    //echo 'combination amount not set :'.$product['inventory_amount'];
                }
            }
        }
    }
    if (!$product['options_update']) {
        $product['options_update'] = db_get_field('SELECT COUNT(*) FROM ?:product_options_inventory WHERE product_id = ?i', $product['product_id']);
    }
    /**
     * Changes product data after applying product options rules
     *
     * @param array $product Product data
     */
    fn_set_hook('apply_options_rules_post', $product);
    return $product;
}
Example #7
0
function fn_required_products_pre_add_to_cart(&$product_data, &$cart, $auth, $update)
{
    foreach ($product_data as $key => $entry) {
        if (!empty($entry['amount']) && !empty($entry['product_id'])) {
            $ids = db_get_fields('SELECT req_prod.required_id FROM ?:product_required_products as req_prod LEFT JOIN ?:products ON req_prod.required_id = ?:products.product_id WHERE req_prod.product_id = ?i AND ?:products.status != ?s', $entry['product_id'], 'D');
            if (!empty($ids)) {
                $have = fn_required_products_get_existent($auth, $ids);
                if (empty($have) || count($have) != count($ids)) {
                    $products_to_cart = array_diff($ids, $have);
                    $out_of_stock = array();
                    $amounts = array();
                    foreach ($products_to_cart as $id) {
                        $amounts[$id] = fn_check_amount_in_stock($id, 1, fn_get_default_product_options($id), 0, 'N', 0, $cart);
                        if (!$amounts[$id]) {
                            $out_of_stock[] = $id;
                        }
                    }
                    if (empty($out_of_stock)) {
                        $msg = fn_get_lang_var('required_products_added');
                        foreach ($amounts as $id => $v) {
                            $product_data[$id] = array('product_id' => $id, 'amount' => $v);
                            $msg .= "<br />" . fn_get_product_name($id);
                        }
                    } else {
                        unset($product_data[$key]);
                        $msg = fn_get_lang_var('required_products_out_of_stock');
                        foreach ($out_of_stock as $id) {
                            $msg .= "<br />" . fn_get_product_name($id);
                        }
                    }
                    fn_set_notification('N', fn_get_lang_var('notice'), $msg);
                }
            }
        }
    }
    return true;
}
Example #8
0
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;
    }
}
Example #9
0
/**
 * Add product to wishlist
 *
 * @param array $product_data array with data for the product to add)(product_id, price, amount, product_options, is_edp)
 * @param array $wishlist wishlist data storage
 * @param array $auth user session data
 * @return mixed array with wishlist IDs for the added products, false otherwise
 */
function fn_add_product_to_wishlist($product_data, &$wishlist, &$auth)
{
    // Check if products have cusom images
    list($product_data, $wishlist) = fn_add_product_options_files($product_data, $wishlist, $auth, false, 'wishlist');
    fn_set_hook('pre_add_to_wishlist', $product_data, $wishlist, $auth);
    if (!empty($product_data) && is_array($product_data)) {
        $wishlist_ids = array();
        foreach ($product_data as $product_id => $data) {
            if (empty($data['amount'])) {
                $data['amount'] = 1;
            }
            if (!empty($data['product_id'])) {
                $product_id = $data['product_id'];
            }
            if (empty($data['extra'])) {
                $data['extra'] = array();
            }
            // Add one product
            if (!isset($data['product_options'])) {
                $data['product_options'] = fn_get_default_product_options($product_id);
            }
            // Generate wishlist id
            $data['extra']['product_options'] = $data['product_options'];
            $wishlist_ids[] = $_id = fn_generate_cart_id($product_id, $data['extra']);
            $wishlist['products'][$_id]['product_id'] = $product_id;
            $wishlist['products'][$_id]['product_options'] = $data['product_options'];
            $wishlist['products'][$_id]['extra'] = $data['extra'];
        }
        return $wishlist_ids;
    } else {
        return false;
    }
}
Example #10
0
/**
 * 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;
}
Example #11
0
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;
}