/**
 * 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;
    }
}
Beispiel #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;
}
Beispiel #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'];
            $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;
    }
}
Beispiel #4
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;
    }
}
 // Select customer
 if ($mode == 'select_customer') {
     if (!empty($_REQUEST['selected_user_id'])) {
         $cart['user_id'] = $_REQUEST['selected_user_id'];
         $u_data = db_get_row("SELECT user_id, tax_exempt, user_type FROM ?:users WHERE user_id = ?i", $cart['user_id']);
         $customer_auth = fn_fill_auth($u_data, array(), false, 'C');
         $cart['user_data'] = array();
     }
     $_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;