Пример #1
0
 protected function offer($product)
 {
     $yml_data = array();
     $offer_attrs = '';
     $market_categories = $this->getMarketCategories();
     if (!empty($product['yml_bid'])) {
         $offer_attrs .= '@bid=' . $product['yml_bid'];
     }
     if (!empty($product['yml_cbid'])) {
         $offer_attrs .= '@cbid=' . $product['yml_cbid'];
     }
     $price_fields = array('price', 'yml_cost', 'list_price', 'base_price');
     $currency_data = Registry::get('currencies.' . CART_PRIMARY_CURRENCY);
     foreach ($price_fields as $field) {
         $product[$field] = fn_format_price($product[$field], $currency_data['currency_code'], $currency_data['decimals'], false);
     }
     if (CART_PRIMARY_CURRENCY != "RUB" && CART_PRIMARY_CURRENCY != "UAH" && CART_PRIMARY_CURRENCY != "BYR" && CART_PRIMARY_CURRENCY != "KZT") {
         $currencies = Registry::get('currencies');
         if (isset($currencies['RUB'])) {
             $currency = $currencies['RUB'];
         } elseif (isset($currencies['UAH'])) {
             $currency = $currencies['UAH'];
         } elseif (isset($currencies['BYR'])) {
             $currency = $currencies['BYR'];
         } elseif (isset($currencies['KZT'])) {
             $currency = $currencies['KZT'];
         }
         if (!empty($currency)) {
             foreach ($price_fields as $field) {
                 $product[$field] = fn_format_rate_value($product[$field], 'F', $currency['decimals'], '.', '', $currency['coefficient']);
             }
         }
     }
     foreach ($price_fields as $field) {
         if (empty($product[$field])) {
             $product[$field] = floatval($product[$field]) ? $product[$field] : fn_parse_price($product[$field]);
         }
     }
     $yml_data['url'] = $product['product_url'];
     $yml_data['price'] = !empty($product['price']) ? $product['price'] : "0.00";
     if (!empty($product['base_price']) && $product['price'] < $product['base_price'] * 0.95) {
         $yml_data['oldprice'] = $product['base_price'];
     } elseif (!empty($product['list_price']) && $product['price'] < $product['list_price'] * 0.95) {
         $yml_data['oldprice'] = $product['list_price'];
     }
     $yml_data['currencyId'] = !empty($currency) ? $currency['currency_code'] : CART_PRIMARY_CURRENCY;
     $yml_data['categoryId'] = $product['category_id'];
     if ($this->options['market_category'] == "Y") {
         if ($this->options['market_category_object'] == "category" && isset($market_categories[$product['category_id']])) {
             $yml_data['market_category'] = $market_categories[$product['category_id']];
         } elseif ($this->options['market_category_object'] == "product" && !empty($product['yml_market_category'])) {
             $yml_data['market_category'] = $product['yml_market_category'];
         }
     }
     // Images
     $picture_index = 0;
     while ($image = array_shift($product['images'])) {
         $key = 'picture';
         if ($picture_index) {
             $key .= '+' . $picture_index;
         }
         $yml_data[$key] = $this->getImageUrl($image);
         $picture_index++;
     }
     $yml_data['store'] = $product['yml_store'] == 'Y' ? 'true' : 'false';
     $yml_data['pickup'] = $product['yml_pickup'] == 'Y' ? 'true' : 'false';
     $yml_data['delivery'] = $product['yml_delivery'] == 'Y' ? 'true' : 'false';
     if ($product['yml_adult'] == 'Y') {
         $yml_data['adult'] = 'true';
     }
     if ($this->options['local_delivery_cost'] == "Y") {
         $yml_data['local_delivery_cost'] = $product['yml_cost'] == 0 ? '0' : $product['yml_cost'];
     }
     $type = '';
     if ($this->options['export_type'] == 'vendor_model') {
         $type = '@type=vendor.model';
         if ($this->options['type_prefix'] == "Y") {
             if (!empty($product['yml_type_prefix'])) {
                 $yml_data['typePrefix'] = $product['yml_type_prefix'];
             } else {
                 $yml_data['typePrefix'] = $product['category'];
             }
         }
         $yml_data['vendor'] = $product['brand'];
         if ($this->options['export_vendor_code'] == 'Y') {
             $vendor_code = $this->getVendorCode($product);
             if (!empty($vendor_code)) {
                 $yml_data['vendorCode'] = $vendor_code;
             }
         }
         $yml_data['model'] = !empty($product['yml_model']) ? $product['yml_model'] : '';
     } elseif ($this->options['export_type'] == 'simple') {
         $yml_data['name'] = $product['product'];
         if (!empty($product['brand'])) {
             $yml_data['vendor'] = $product['brand'];
         }
         if ($this->options['export_vendor_code'] == 'Y') {
             $vendor_code = $this->getVendorCode($product);
             if (!empty($vendor_code)) {
                 $yml_data['vendorCode'] = $vendor_code;
             }
         }
     }
     if (!empty($product['full_description'])) {
         //Stripping the invalid chars
         $product['full_description'] = preg_replace('/[^\\x{0009}\\x{000A}\\x{000D}\\x{0020}-\\x{D7FF}\\x{E000}-\\x{FFFD}\\x{10000}-\\x{10FFFF}]/u', '', $product['full_description']);
         $yml_data['description'] = $product['full_description'];
     }
     if (!empty($product['yml_sales_notes'])) {
         $yml_data['sales_notes'] = $product['yml_sales_notes'];
     }
     if (!empty($product['yml_manufacturer_warranty'])) {
         $yml_data['manufacturer_warranty'] = $product['yml_manufacturer_warranty'];
     }
     if (!empty($product['yml_seller_warranty'])) {
         $yml_data['seller_warranty'] = $product['yml_seller_warranty'];
     }
     if (!empty($product['yml_origin_country']) && fn_yandex_market_check_country($product['yml_origin_country'])) {
         $yml_data['country_of_origin'] = $product['yml_origin_country'];
     }
     if (!empty($product['product_features'])) {
         foreach ($product['product_features'] as $feature) {
             $yml_data['param@name=' . $this->escape($feature['description'])] = $feature['value'];
         }
     }
     if ($product['amount'] > 0) {
         $avail = 'true';
     } else {
         $avail = 'false';
     }
     return array('offer@id=' . $product['product_id'] . $type . '@available=' . $avail . $offer_attrs, $yml_data);
 }
Пример #2
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;
    }
}
Пример #3
0
function fn_mailru_filter_products($products, $addon_settings, $error_products)
{
    $product_ids = array();
    $products_filter = array();
    foreach ($products as $product) {
        $price = !floatval($product['price']) ? fn_parse_price($product['price']) : $product['price'];
        if (empty($price) || $price == '0') {
            $error_products['products_zero_price'] .= $product['product_name'] . ', ';
            continue;
        }
        $tracking = db_get_field("SELECT tracking FROM ?:products WHERE product_id = ?i", $product['product_id']);
        if (empty($tracking) || $tracking == 'O') {
            $product['amount'] = db_get_field("SELECT SUM(amount) FROM ?:product_options_inventory WHERE product_id = ?i", $product['product_id']);
        }
        if ($addon_settings['export_stock'] == "Y" && $product['amount'] <= 0) {
            $error_products['out_of_stock'] .= $product['product_name'] . ', ';
            continue;
        }
        $product_ids[] = $product['product_id'];
        $products_filter[] = $product;
    }
    return array($products_filter, $product_ids);
}
Пример #4
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']);
}
Пример #5
0
 public function preBuild(&$product, $products_images_main, $products_images_additional)
 {
     $is_broken = false;
     if ($this->options['export_null_price'] == 'N') {
         $price = !floatval($product['price']) ? fn_parse_price($product['price']) : intval($product['price']);
         if (empty($price)) {
             $this->log->write(Logs::SKIP_PRODUCT, $product, __('yml2_log_product_price_is_empty'));
             $is_broken = true;
         }
     }
     $bean_bag_150_id = Registry::get('addons.add_bean_bag_filling.bean_bag_filling_150');
     $bean_bag_300_id = Registry::get('addons.add_bean_bag_filling.bean_bag_filling_300');
     $bean_bag_150 = Registry::get('addons.add_bean_bag_filling.bean_bag_filling_cost_150');
     $bean_bag_300 = Registry::get('addons.add_bean_bag_filling.bean_bag_filling_cost_300');
     $fillings = db_get_array('SELECT p.product_id, p.shipping_params, p.weight' . ' FROM ?:products as p' . ' WHERE p.product_id IN(?n)', array($bean_bag_150_id, $bean_bag_300_id));
     foreach ($fillings as $fi => $fv) {
         $val = unserialize($fv['shipping_params']);
         $fil[$fv['product_id']]['width'] = $val['box_width'];
         $fil[$fv['product_id']]['height'] = $val['box_height'];
         $fil[$fv['product_id']]['length'] = $val['box_length'];
         $fil[$fv['product_id']]['weight'] = $fv['weight'];
     }
     $product['price'] = $product['price'] + $product['bean_bag_quantity_300'] * $bean_bag_300 + $product['bean_bag_quantity_150'] * $bean_bag_150;
     $product['weight'] = $product['weight'] + $product['bean_bag_quantity_300'] * $fil[$bean_bag_300_id]['weight'] + $product['bean_bag_quantity_150'] * $fil[$bean_bag_150_id]['weight'];
     $product['shipping_params'] = unserialize($product['shipping_params']);
     //var_dump($product['shipping_params'],$fil); die();
     $product_vol = $product['shipping_params']['box_length'] * $product['shipping_params']['box_height'] * $product['shipping_params']['box_width'];
     $fil_vol = $product['bean_bag_quantity_300'] * ($fil[$bean_bag_300_id]['width'] * $fil[$bean_bag_300_id]['height'] * $fil[$bean_bag_300_id]['length']) + $product['bean_bag_quantity_150'] * ($fil[$bean_bag_150_id]['width'] * $fil[$bean_bag_150_id]['height'] * $fil[$bean_bag_150_id]['length']);
     $sum_vol = $product_vol + $fil_vol;
     if ($product['shipping_params']['box_length'] < $fil[$bean_bag_300_id]['length']) {
         $product['shipping_params']['box_length'] = $fil[$bean_bag_300_id]['length'];
     }
     if ($product['shipping_params']['box_width'] < $fil[$bean_bag_300_id]['width']) {
         $product['shipping_params']['box_width'] = $fil[$bean_bag_300_id]['width'];
     }
     $product['shipping_params']['box_height'] = round($sum_vol / ($product['shipping_params']['box_width'] * $product['shipping_params']['box_length']), 1);
     $product['shipping_params'] = serialize($product['shipping_params']);
     $exclude_categories = array_intersect($product['category_ids'], $this->exclude_category_ids);
     if (!empty($exclude_categories)) {
         $this->log->write(Logs::SKIP_PRODUCT, $product, __('yml2_log_category_excluded'));
         $is_broken = true;
     } else {
         $export_categories = array_intersect($product['category_ids'], $this->export_category_ids);
         if (empty($export_categories)) {
             $this->log->write(Logs::SKIP_PRODUCT, $product, __('yml2_log_category_not_visible'));
             $is_broken = true;
         }
     }
     $product['product'] = $this->escape($product['product']);
     $product['full_description'] = $this->escape($product['full_description']);
     $product['short_description'] = $this->escape($product['short_description']);
     if ($this->options['export_stock'] == 'Y') {
         if ($product['tracking'] == 'B' && $product['amount'] <= 0) {
             $this->log->write(Logs::SKIP_PRODUCT, $product, __('yml2_log_product_amount_is_empty'));
             $is_broken = true;
         }
     }
     if (!$this->offer->preBuild($product)) {
         $is_broken = true;
     }
     if ($is_broken) {
         return false;
     }
     if (!empty($this->options['utm_link'])) {
         $product['product_url'] = $this->getUTMLink($product, $this->options['utm_link'], 'products.view?product_id=' . $product['product_id']);
     } else {
         $product['product_url'] = 'products.view?product_id=' . $product['product_id'];
     }
     // Images
     $images = array_merge($products_images_main[$product['product_id']], $products_images_additional[$product['product_id']]);
     $product['images'] = array_slice($images, 0, self::IMAGES_LIMIT);
     return true;
 }
Пример #6
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;
    }
}