Beispiel #1
0
function fn_buy_together_check_products_amount(&$product_data, &$cart)
{
    if (!isset($cart['products'])) {
        $cart['products'] = array();
    }
    if (!empty($product_data)) {
        foreach ($product_data as $key => $product) {
            if (!empty($product['buy_together'])) {
                $amount = fn_check_amount_in_stock($product['product_id'], $product['amount'], @$product['product_options'], $key, $is_edp = 'N', $original_amount = 0, $cart);
                if ($amount < $product['amount']) {
                    $extra_amount = intval($amount / $product['extra']['min_qty']);
                    $product_data[$key]['amount'] = $extra_amount * $product['extra']['min_qty'];
                    if (isset($cart['products'][$key])) {
                        $cart['products'][$key]['amount'] = $product_data[$key]['amount'];
                    }
                    foreach ($product['buy_together'] as $hash => $amount) {
                        if (isset($product_data[$hash])) {
                            $product_data[$hash]['amount'] = $extra_amount * $product_data[$hash]['extra']['min_qty'];
                            $product_data[$key]['buy_together'][$hash] = $product_data[$hash]['amount'];
                            if (isset($cart['products'][$hash])) {
                                $cart['products'][$hash]['amount'] = $product_data[$hash]['amount'];
                            }
                        }
                    }
                }
                foreach ($product['buy_together'] as $hash => $amount) {
                    if ($product_data[$hash]['amount'] > 0) {
                        $allowed_amount = fn_check_amount_in_stock($product_data[$hash]['product_id'], $product_data[$hash]['amount'], empty($product_data[$hash]['product_options']) ? array() : $product_data[$hash]['product_options'], $hash, $is_edp = 'N', $original_amount = 0, $cart);
                        foreach ($product_data as $_key => $_product) {
                            if ($_key == $hash) {
                                continue;
                            } elseif ($_product['product_id'] == $product_data[$hash]['product_id'] && $_product['product_options'] == $product_data[$hash]['product_options']) {
                                $allowed_amount -= $product_data[$hash]['amount'];
                            }
                        }
                        if ($allowed_amount < $product_data[$hash]['amount']) {
                            $extra_amount = intval($allowed_amount / $product_data[$hash]['extra']['min_qty']);
                            $product_data[$hash]['amount'] = $extra_amount * $product_data[$hash]['extra']['min_qty'];
                            $product_data[$key]['buy_together'][$hash] = $product_data[$hash]['amount'];
                            $product_data[$key]['amount'] = $extra_amount * $product['extra']['min_qty'];
                            if (isset($cart['products'][$hash])) {
                                $cart['products'][$hash]['amount'] = $product_data[$hash]['amount'];
                            }
                            if (isset($cart['products'][$key])) {
                                $cart['products'][$key]['amount'] = $product_data[$key]['amount'];
                            }
                            foreach ($product['buy_together'] as $_id => $_amount) {
                                if ($_id == $hash) {
                                    continue;
                                }
                                $product_data[$_id]['amount'] = $extra_amount * $product_data[$_id]['extra']['min_qty'];
                                if (isset($cart['products'][$_id])) {
                                    $cart['products'][$_id]['amount'] = $product_data[$_id]['amount'];
                                }
                            }
                        }
                    }
                }
                if ($product_data[$key]['amount'] <= 0) {
                    foreach ($product['buy_together'] as $hash => $amount) {
                        unset($product_data[$hash]);
                    }
                    unset($product_data[$key]);
                    fn_set_notification('E', fn_get_lang_var('error'), fn_get_lang_var('buy_together_combination_cannot_be_added'));
                }
            }
        }
    }
    return $product_data;
}
Beispiel #2
0
function fn_update_conf_amount(&$cart, &$prev_amount)
{
    $rollback = array();
    foreach ($cart['products'] as $cart_id => $cart_item) {
        if (!empty($cart['products'][$cart_id]['extra']['configuration'])) {
            $coef = $cart['products'][$cart_id]['amount'] / $prev_amount[$cart_id];
            foreach ($cart['products'] as $key => $item) {
                if (!empty($item['extra']['parent']['configuration']) && $item['extra']['parent']['configuration'] == $cart_id) {
                    $new_amount = round($cart['products'][$key]['amount'] * $coef);
                    $new_amount = empty($new_amount) ? 1 : $new_amount;
                    $checked_amount = fn_check_amount_in_stock($item['product_id'], $new_amount, @$item['product_options'], $key, !empty($item['is_edp']) && $item['is_edp'] == 'Y' ? 'Y' : 'N', 0, $cart);
                    if ($checked_amount < $new_amount) {
                        $rollback[] = $cart_id;
                        break;
                    }
                    $cart['products'][$key]['amount'] = $new_amount;
                }
            }
        }
    }
    // If amount of products is less than we try to update to, roll back to previous state
    if (!empty($rollback)) {
        foreach ($rollback as $cart_id) {
            if (!empty($cart['products'][$cart_id]['extra']['configuration'])) {
                foreach ($cart['products'] as $key => $item) {
                    if (!empty($item['extra']['parent']['configuration']) && $item['extra']['parent']['configuration'] == $cart_id) {
                        $cart['products'][$key]['amount'] = $prev_amount[$cart_id];
                    }
                }
                $cart['products'][$cart_id]['amount'] = $prev_amount[$cart_id];
            }
        }
    }
    return true;
}
                 }
             }
             if (count($promotionP['bonuses']) == 0) {
                 unset($promotionP['bonuses']);
             }
         }
         if (count($promotionP) == 0) {
             unset($cart['promotions'][$keyPromotion]);
         }
     }
 } elseif (count($cart['product_groups']) > 1) {
 }
 if (!empty($cart['products'])) {
     foreach ($cart['products'] as $k => $v) {
         $_is_edp = db_get_field("SELECT is_edp FROM ?:products WHERE product_id = ?i", $v['product_id']);
         if (fn_check_amount_in_stock($v['product_id'], $v['amount'], empty($v['product_options']) ? array() : $v['product_options'], $k, $_is_edp, 0, $cart) == false) {
             fn_delete_cart_product($cart, $k);
             return array(CONTROLLER_STATUS_REDIRECT, "checkout.cart");
         }
         if (!fn_allowed_for('ULTIMATE:FREE')) {
             $exceptions = fn_get_product_exceptions($v['product_id'], true);
             if (!isset($v['options_type']) || !isset($v['exceptions_type'])) {
                 $v = array_merge($v, db_get_row('SELECT options_type, exceptions_type FROM ?:products WHERE product_id = ?i', $v['product_id']));
             }
             if (!fn_is_allowed_options_exceptions($exceptions, $v['product_options'], $v['options_type'], $v['exceptions_type'])) {
                 fn_set_notification('E', __('notice'), __('product_options_forbidden_combination', array('[product]' => $v['product'])));
                 fn_delete_cart_product($cart, $k);
                 return array(CONTROLLER_STATUS_REDIRECT, "checkout.cart");
             }
         }
     }
 if (AREA == 'A') {
     $_auth = $_SESSION['customer_auth'];
     if (empty($_auth)) {
         $_auth = fn_fill_auth(array(), array(), false, 'C');
     }
 }
 foreach ($cart_products as $cart_id => $item) {
     if (isset($_cart['products'][$cart_id])) {
         $amount = isset($item['amount']) ? $item['amount'] : 1;
         $product_data = fn_get_product_data($item['product_id'], $auth, CART_LANGUAGE, '', false, false, false, false, false, false, false);
         if ($product_data['options_type'] == 'S' && isset($item['product_options']) && isset($_REQUEST['changed_option'][$cart_id])) {
             $item['product_options'] = fn_fill_sequential_options($item, $_REQUEST['changed_option'][$cart_id]);
             unset($_REQUEST['changed_option']);
         }
         $product_options = isset($item['product_options']) ? $item['product_options'] : array();
         $amount = fn_check_amount_in_stock($item['product_id'], $amount, $product_options, $cart_id, $_cart['products'][$cart_id]['is_edp'], 0, $_cart);
         if ($amount === false) {
             unset($_cart['products'][$cart_id]);
             continue;
         }
         $_cart['products'][$cart_id]['amount'] = $amount;
         $_cart['products'][$cart_id]['product_options'] = isset($item['product_options']) ? $item['product_options'] : array();
         if (!empty($_cart['products'][$cart_id]['extra']['saved_options_key'])) {
             $_cart['saved_product_options'][$_cart['products'][$cart_id]['extra']['saved_options_key']] = $_cart['products'][$cart_id]['product_options'];
         }
         if (!empty($item['object_id'])) {
             $_cart['products'][$cart_id]['object_id'] = $item['object_id'];
             if (!empty($_cart['products'][$cart_id]['extra']['saved_options_key'])) {
                 // Product from promotion. Save object_id for this product
                 $_cart['saved_object_ids'][$_cart['products'][$cart_id]['extra']['saved_options_key']] = $item['object_id'];
             }
Beispiel #5
0
/**
 * Place order handler
 *
 * @param  array $cart   Cart
 * @param  array $auth   Auth
 * @param  array $params Params
 * @return str
 */
function fn_checkout_place_order(&$cart, &$auth, $params)
{
    // Prevent unauthorized access
    if (empty($cart['user_data']['email'])) {
        return PLACE_ORDER_STATUS_DENIED;
    }
    // Prevent using disabled payment method by challenging HTTP data
    if (!empty($params['payment_id'])) {
        $cart['payment_id'] = $params['payment_id'];
    }
    if (isset($cart['payment_id'])) {
        $payment_method_data = fn_get_payment_method_data($cart['payment_id']);
        if (!empty($payment_method_data['status']) && $payment_method_data['status'] != 'A') {
            return PLACE_ORDER_STATUS_DENIED;
        }
    }
    // Remove previous failed order
    if (!empty($cart['failed_order_id']) || !empty($cart['processed_order_id'])) {
        $_order_ids = !empty($cart['failed_order_id']) ? $cart['failed_order_id'] : $cart['processed_order_id'];
        foreach ($_order_ids as $_order_id) {
            fn_delete_order($_order_id);
        }
        $cart['rewrite_order_id'] = $_order_ids;
        unset($cart['failed_order_id'], $cart['processed_order_id']);
    }
    if (!empty($params['payment_info'])) {
        $cart['payment_info'] = $params['payment_info'];
    } else {
        $cart['payment_info'] = array();
    }
    if (empty($params['payment_info']) && !empty($cart['extra_payment_info'])) {
        $cart['payment_info'] = empty($cart['payment_info']) ? array() : $cart['payment_info'];
        $cart['payment_info'] = array_merge($cart['extra_payment_info'], $cart['payment_info']);
    }
    unset($cart['payment_info']['secure_card_number']);
    if (!empty($cart['products'])) {
        foreach ($cart['products'] as $cart_id => $product) {
            $_is_edp = db_get_field("SELECT is_edp FROM ?:products WHERE product_id = ?i", $product['product_id']);
            if (fn_check_amount_in_stock($product['product_id'], $product['amount'], empty($product['product_options']) ? array() : $product['product_options'], $cart_id, $_is_edp, 0, $cart) == false) {
                fn_delete_cart_product($cart, $cart_id);
                return PLACE_ORDER_STATUS_TO_CART;
            }
            if (!fn_allowed_for('ULTIMATE:FREE')) {
                $exceptions = fn_get_product_exceptions($product['product_id'], true);
                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']));
                }
                if (!fn_is_allowed_options_exceptions($exceptions, $product['product_options'], $product['options_type'], $product['exceptions_type'])) {
                    fn_set_notification('E', __('notice'), __('product_options_forbidden_combination', array('[product]' => $product['product'])));
                    fn_delete_cart_product($cart, $cart_id);
                    return PLACE_ORDER_STATUS_TO_CART;
                }
                if (!fn_is_allowed_options($product)) {
                    fn_set_notification('E', __('notice'), __('product_disabled_options', array('[product]' => $product['product'])));
                    fn_delete_cart_product($cart, $cart_id);
                    return PLACE_ORDER_STATUS_TO_CART;
                }
            }
        }
    }
    list($order_id, $process_payment) = fn_place_order($cart, $auth);
    // Clean up saved shipping rates
    unset($_SESSION['product_groups']);
    if (!empty($order_id)) {
        if (empty($params['skip_payment']) && $process_payment == true || !empty($params['skip_payment']) && empty($auth['act_as_user'])) {
            // administrator, logged in as customer can skip payment
            $payment_info = !empty($cart['payment_info']) ? $cart['payment_info'] : array();
            fn_start_payment($order_id, array(), $payment_info);
        }
        fn_order_placement_routines('route', $order_id);
        return PLACE_ORDER_STATUS_OK;
    } else {
        return PLACE_ORDER_STATUS_TO_CART;
    }
}
Beispiel #6
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;
}
Beispiel #7
0
function fn_reorder($order_id, &$cart, &$auth)
{
    $order_info = fn_get_order_info($order_id, false, false, false, true);
    unset(Tygh::$app['session']['shipping_hash']);
    unset(Tygh::$app['session']['edit_step']);
    fn_set_hook('reorder', $order_info, $cart, $auth);
    foreach ($order_info['products'] as $k => $item) {
        // refresh company id
        $company_id = db_get_field("SELECT company_id FROM ?:products WHERE product_id = ?i", $item['product_id']);
        $order_info['products'][$k]['company_id'] = $company_id;
        unset($order_info['products'][$k]['extra']['ekey_info']);
        unset($order_info['products'][$k]['extra']['promotions']);
        unset($order_info['products'][$k]['promotions']);
        $order_info['products'][$k]['product_options'] = empty($order_info['products'][$k]['extra']['product_options']) ? array() : $order_info['products'][$k]['extra']['product_options'];
        $order_info['products'][$k]['main_pair'] = fn_get_cart_product_icon($item['product_id'], $order_info['products'][$k]);
    }
    if (!empty($cart) && !empty($cart['products'])) {
        $cart['products'] = fn_array_merge($cart['products'], $order_info['products']);
    } else {
        $cart['products'] = $order_info['products'];
    }
    foreach ($cart['products'] as $k => $v) {
        $_is_edp = db_get_field("SELECT is_edp FROM ?:products WHERE product_id = ?i", $v['product_id']);
        if ($amount = fn_check_amount_in_stock($v['product_id'], $v['amount'], $v['product_options'], $k, $_is_edp, 0, $cart)) {
            $cart['products'][$k]['amount'] = $amount;
            // Check if the product price with options modifiers equals to zero
            $price = fn_get_product_price($v['product_id'], $amount, $auth);
            $zero_price_action = db_get_field("SELECT zero_price_action FROM ?:products WHERE product_id = ?i", $v['product_id']);
            if (!floatval($price) && $zero_price_action == 'A') {
                if (isset($v['custom_user_price'])) {
                    $price = $v['custom_user_price'];
                }
            }
            $price = fn_apply_options_modifiers($v['product_options'], $price, 'P', array(), array('product_data' => $v));
            if (!floatval($price)) {
                $data['price'] = isset($data['price']) ? fn_parse_price($data['price']) : 0;
                if (AREA == 'C' && ($zero_price_action == 'R' || $zero_price_action == 'A' && floatval($data['price']) < 0)) {
                    if ($zero_price_action == 'A') {
                        fn_set_notification('E', __('error'), __('incorrect_price_warning'));
                    } else {
                        fn_set_notification('W', __('warning'), __('warning_zero_price_restricted_product', array('[product]' => $v['product'])));
                    }
                    unset($cart['products'][$k]);
                    continue;
                }
            }
            // Change the path of custom files
            if (!empty($v['extra']['custom_files'])) {
                foreach ($v['extra']['custom_files'] as $option_id => $_data) {
                    if (!empty($_data)) {
                        foreach ($_data as $file_id => $file) {
                            $cart['products'][$k]['extra']['custom_files'][$option_id][$file_id]['path'] = 'sess_data/' . fn_basename($file['path']);
                        }
                    }
                }
            }
        } else {
            unset($cart['products'][$k]);
        }
    }
    // Restore custom files for editing
    $dir_path = 'order_data/' . $order_id;
    if (Storage::instance('custom_files')->isExist($dir_path)) {
        Storage::instance('custom_files')->copy($dir_path, 'sess_data');
    }
    // Redirect customer to step three after reordering
    $cart['payment_updated'] = true;
    fn_save_cart_content($cart, $auth['user_id']);
    unset($cart['product_groups']);
}
Beispiel #8
0
function fn_reorder($order_id, &$cart, &$auth)
{
    $order_info = fn_get_order_info($order_id, false, false, false, true);
    unset($_SESSION['shipping_hash']);
    unset($_SESSION['edit_step']);
    fn_set_hook('reorder', $order_info, $cart, $auth);
    foreach ($order_info['products'] as $k => $item) {
        // refresh company id
        $company_id = db_get_field("SELECT company_id FROM ?:products WHERE product_id = ?i", $item['product_id']);
        $order_info['products'][$k]['company_id'] = $company_id;
        unset($order_info['products'][$k]['extra']['ekey_info']);
        $order_info['products'][$k]['product_options'] = empty($order_info['products'][$k]['extra']['product_options']) ? array() : $order_info['products'][$k]['extra']['product_options'];
        $order_info['products'][$k]['main_pair'] = fn_get_cart_product_icon($item['product_id'], $order_info['products'][$k]);
    }
    if (!empty($cart) && !empty($cart['products'])) {
        $cart['products'] = fn_array_merge($cart['products'], $order_info['products']);
    } else {
        $cart['products'] = $order_info['products'];
    }
    foreach ($cart['products'] as $k => $v) {
        $_is_edp = db_get_field("SELECT is_edp FROM ?:products WHERE product_id = ?i", $v['product_id']);
        if ($amount = fn_check_amount_in_stock($v['product_id'], $v['amount'], $v['product_options'], $k, $_is_edp, 0, $cart)) {
            $cart['products'][$k]['amount'] = $amount;
            // Change the path of custom files
            if (!empty($v['extra']['custom_files'])) {
                foreach ($v['extra']['custom_files'] as $option_id => $_data) {
                    if (!empty($_data)) {
                        foreach ($_data as $file_id => $file) {
                            $cart['products'][$k]['extra']['custom_files'][$option_id][$file_id]['path'] = 'sess_data/' . fn_basename($file['path']);
                        }
                    }
                }
            }
        } else {
            unset($cart['products'][$k]);
        }
    }
    // Restore custom files for editing
    $dir_path = 'order_data/' . $order_id;
    if (Storage::instance('custom_files')->isExist($dir_path)) {
        Storage::instance('custom_files')->copy($dir_path, 'sess_data');
    }
    // Redirect customer to step three after reordering
    $cart['payment_updated'] = true;
    fn_save_cart_content($cart, $auth['user_id']);
    unset($cart['product_groups']);
}
Beispiel #9
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 #10
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;
}
Beispiel #11
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;
    }
}
Beispiel #12
0
/**
 * Add buy together connections to 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.)
 * @return boolean Always true
 */
function fn_buy_together_update_cart_products_pre(&$cart, &$product_data, &$auth)
{
    if (!empty($cart['products'])) {
        foreach ($cart['products'] as $id => $product) {
            if (!empty($product['extra']['buy_together'])) {
                $is_valid = true;
                $_ids = array();
                if (isset($product_data[$id])) {
                    $allowed_amount = fn_check_amount_in_stock($product['product_id'], $product_data[$id]['amount'], empty($product_data[$id]['product_options']) ? array() : $product_data[$id]['product_options'], $id, $is_edp = 'N', $product['amount'], $cart);
                    if ($allowed_amount != $product_data[$id]['amount']) {
                        $is_valid = false;
                    }
                    $_ids[] = $id;
                }
                foreach ($cart['products'] as $aux_id => $aux_product) {
                    if (isset($product_data[$aux_id]) && isset($aux_product['extra']['parent']['buy_together']) && $aux_product['extra']['parent']['buy_together'] == $id) {
                        if ($is_valid) {
                            $amount = $aux_product['extra']['min_qty'] * $product_data[$id]['amount'];
                            $allowed_amount = fn_check_amount_in_stock($aux_product['product_id'], $amount, empty($product_data[$aux_id]['product_options']) ? array() : $product_data[$aux_id]['product_options'], $aux_id, $is_edp = 'N', $aux_product['amount'], $cart);
                            if ($allowed_amount != $amount) {
                                $is_valid = false;
                            } else {
                                $product_data[$aux_id]['amount'] = $amount;
                            }
                        }
                        $_ids[] = $id;
                    }
                }
                if (!$is_valid) {
                    foreach ($_ids as $id) {
                        unset($product_data[$id]);
                    }
                }
            }
        }
    }
    return true;
}
 foreach ($cart['products'] as $id => $product) {
     if (!empty($product['extra']['buy_together'])) {
         $is_valid = true;
         $_ids = array();
         if (isset($_REQUEST['cart_products'][$id])) {
             $allowed_amount = fn_check_amount_in_stock($product['product_id'], $_REQUEST['cart_products'][$id]['amount'], empty($_REQUEST['cart_products'][$id]['product_options']) ? array() : $_REQUEST['cart_products'][$id]['product_options'], $id, $is_edp = 'N', $product['amount'], $cart);
             if ($allowed_amount != $_REQUEST['cart_products'][$id]['amount']) {
                 $is_valid = false;
             }
             $_ids[] = $id;
         }
         foreach ($cart['products'] as $aux_id => $aux_product) {
             if (isset($_REQUEST['cart_products'][$aux_id]) && isset($aux_product['extra']['parent']['buy_together']) && $aux_product['extra']['parent']['buy_together'] == $id) {
                 if ($is_valid) {
                     $amount = $aux_product['extra']['min_qty'] * $_REQUEST['cart_products'][$id]['amount'];
                     $allowed_amount = fn_check_amount_in_stock($aux_product['product_id'], $amount, empty($_REQUEST['cart_products'][$aux_id]['product_options']) ? array() : $_REQUEST['cart_products'][$aux_id]['product_options'], $aux_id, $is_edp = 'N', $aux_product['amount'], $cart);
                     if ($allowed_amount != $amount) {
                         $is_valid = false;
                     } else {
                         $_REQUEST['cart_products'][$aux_id]['amount'] = $amount;
                     }
                 }
                 $_ids[] = $id;
             }
         }
         if (!$is_valid) {
             foreach ($_ids as $id) {
                 unset($_REQUEST['cart_products'][$id]);
             }
         }
     }
Beispiel #14
0
function fn_reorder($order_id, &$cart, &$auth)
{
    $order_info = fn_get_order_info($order_id, false, false, false, true);
    fn_set_hook('reorder', $order_info, $cart, $auth);
    foreach ($order_info['items'] as $k => $item) {
        // refresh company id
        $company_id = db_get_field("SELECT company_id FROM ?:products WHERE product_id = ?i", $item['product_id']);
        $order_info['items'][$k]['company_id'] = $company_id;
        unset($order_info['items'][$k]['extra']['ekey_info']);
        $order_info['items'][$k]['product_options'] = empty($order_info['items'][$k]['extra']['product_options']) ? array() : $order_info['items'][$k]['extra']['product_options'];
    }
    if (!empty($cart) && !empty($cart['products'])) {
        $cart['products'] = fn_array_merge($cart['products'], $order_info['items']);
    } else {
        $cart['products'] = $order_info['items'];
    }
    foreach ($cart['products'] as $k => $v) {
        $_is_edp = db_get_field("SELECT is_edp FROM ?:products WHERE product_id = ?i", $v['product_id']);
        if ($amount = fn_check_amount_in_stock($v['product_id'], $v['amount'], $v['product_options'], $k, $_is_edp, 0, $cart)) {
            $cart['products'][$k]['amount'] = $amount;
        } else {
            unset($cart['products'][$k]);
        }
    }
    // Restore custom files for editing
    $dir_path = DIR_CUSTOM_FILES . 'order_data/' . $order_id;
    if (is_dir($dir_path)) {
        fn_mkdir(DIR_CUSTOM_FILES . 'sess_data');
        fn_copy($dir_path, DIR_CUSTOM_FILES . 'sess_data');
    }
    // Redirect customer to step three after reordering
    $cart['payment_updated'] = true;
    fn_save_cart_content($cart, $auth['user_id']);
}
 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]);
 }
 if (empty($amount)) {
     fn_delete_cart_product($cart, $_id);
     continue;
 } else {
     $cart['products'][$_id]['amount'] = fn_check_amount_in_stock($v['product_id'], $amount, @$v['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']);
 }
 if (@$v['stored_price'] == 'Y') {
     $cart['products'][$_id]['price'] = $v['price'];
 }