Ejemplo n.º 1
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;
    }
}
Ejemplo n.º 2
0
/**
 * Update cart products from passed products data
 *
 * @param array $cart Array of cart content and user information necessary for purchase
 * @param array $product_data Array of new products data
 * @param array $auth Array of user authentication data (e.g. uid, usergroup_ids, etc.)
 * @return boolean Always true
 */
function fn_update_cart_products(&$cart, $product_data, $auth)
{
    /**
     * Actions before updating cart products
     *
     * @param array $cart         Array of cart content and user information necessary for purchase
     * @param array $product_data Array of new products data
     * @param array $auth         Array of user authentication data (e.g. uid, usergroup_ids, etc.)
     */
    fn_set_hook('update_cart_products_pre', $cart, $product_data, $auth);
    if (is_array($cart['products']) && !empty($product_data)) {
        list($product_data, $cart) = fn_add_product_options_files($product_data, $cart, $auth, true);
        unset($product_data['custom_files']);
        foreach ($product_data as $k => $v) {
            if (!isset($cart['products'][$k]['extra']['exclude_from_calculate'])) {
                if (empty($v['extra'])) {
                    $v['extra'] = array();
                }
                if ($v['price'] < 0) {
                    $v['price'] = 0;
                }
                unset($v['object_id']);
                $amount = fn_normalize_amount($v['amount']);
                $price = fn_get_product_price($v['product_id'], $amount, $auth);
                $v['extra'] = empty($cart['products'][$k]['extra']) ? array() : $cart['products'][$k]['extra'];
                $v['extra']['product_options'] = empty($v['product_options']) ? array() : $v['product_options'];
                $_id = fn_generate_cart_id($v['product_id'], $v['extra']);
                if (!isset($cart['products'][$_id])) {
                    //if combination doesn't exist in the cart
                    $cart['products'][$_id] = $v;
                    $cart['products'][$_id]['company_id'] = !empty($cart['products'][$k]['company_id']) ? $cart['products'][$k]['company_id'] : 0;
                    $_product = $cart['products'][$k];
                    fn_define_original_amount($v['product_id'], $_id, $cart['products'][$_id], $_product);
                    fn_delete_cart_product($cart, $k);
                } elseif ($k != $_id) {
                    // if the combination is exist but differs from the current
                    $amount += $cart['products'][$_id]['amount'];
                    fn_delete_cart_product($cart, $k);
                }
                if (empty($amount)) {
                    fn_delete_cart_product($cart, $_id);
                    continue;
                } else {
                    $_product_options = !empty($v['product_options']) ? $v['product_options'] : array();
                    $cart['products'][$_id]['amount'] = fn_check_amount_in_stock($v['product_id'], $amount, $_product_options, $_id, !empty($cart['products'][$_id]['is_edp']) && $cart['products'][$_id]['is_edp'] == 'Y' ? 'Y' : 'N', !empty($cart['products'][$_id]['original_amount']) ? $cart['products'][$_id]['original_amount'] : 0, $cart);
                    if ($cart['products'][$_id]['amount'] == false && !empty($_product)) {
                        $cart['products'][$_id] = $_product;
                        unset($_product);
                    }
                }
                if ($k != $_id) {
                    $cart['products'][$_id]['prev_cart_id'] = $k;
                    // save stored taxes for products
                    fn_update_stored_cart_taxes($cart, $k, $_id, true);
                } elseif (isset($cart['products'][$_id]['prev_cart_id'])) {
                    unset($cart['products'][$_id]['prev_cart_id']);
                }
                $cart['products'][$_id]['stored_price'] = !empty($v['stored_price']) ? $v['stored_price'] : 'N';
                if ($cart['products'][$_id]['stored_price'] == 'Y') {
                    $cart['products'][$_id]['price'] = $v['price'];
                }
                $cart['products'][$_id]['stored_discount'] = !empty($v['stored_discount']) ? $v['stored_discount'] : 'N';
                if ($cart['products'][$_id]['stored_discount'] == 'Y') {
                    $cart['products'][$_id]['discount'] = $v['discount'];
                }
            }
        }
    }
    /**
     * Additional cart products updates from passed params
     *
     * @param array $cart         Array of cart content and user information necessary for purchase
     * @param array $product_data Array of new products data
     * @param array $auth         Array of user authentication data (e.g. uid, usergroup_ids, etc.)
     */
    fn_set_hook('update_cart_products_post', $cart, $product_data, $auth);
    return true;
}
Ejemplo n.º 3
0
     $_suffix = ".customer_info";
 }
 if ($mode == 'update') {
     // Clean up saved shipping rates
     unset($_SESSION['shipping_rates']);
     if (is_array($cart['products'])) {
         $product_data = $_REQUEST['cart_products'];
         list($product_data, $cart) = fn_add_product_options_files($product_data, $cart, $customer_auth, true);
         unset($product_data['custom_files']);
         foreach ($product_data as $k => $v) {
             if (!isset($cart['products'][$k]['extra']['exclude_from_calculate'])) {
                 if (empty($v['extra'])) {
                     $v['extra'] = array();
                 }
                 unset($v['object_id']);
                 $amount = fn_normalize_amount($v['amount']);
                 $price = fn_get_product_price($v['product_id'], $amount, $customer_auth);
                 $v['extra'] = empty($cart['products'][$k]['extra']) ? array() : $cart['products'][$k]['extra'];
                 $v['extra']['product_options'] = empty($v['product_options']) ? array() : $v['product_options'];
                 $_id = fn_generate_cart_id($v['product_id'], $v['extra']);
                 if (!isset($cart['products'][$_id])) {
                     //if combination doesn't exist in the cart
                     $cart['products'][$_id] = $v;
                     $_product = $cart['products'][$k];
                     fn_define_original_amount($v['product_id'], $_id, $cart['products'][$_id], $_product);
                     unset($cart['products'][$k]);
                 } elseif ($k != $_id) {
                     // if the combination is exist but differs from the current
                     $amount += $cart['products'][$_id]['amount'];
                     unset($cart['products'][$k]);
                 }