购物流程_ -

我的购物车

清空购物车
商品 单价 数量 小计 操作
您的购物车中没有任何商品!

商品总计:¥0.00

GET['ajax'])) { header('Content-Type: application/json'); echo json_encode(array('success' => true)); exit; } ecs_header("Location:./ "); } elseif ($_REQUEST['step'] == 'drop_to_collect') { if ($_SESSION['user_id'] > 0) { $rec_id = intval($_GET['id']); $goods_id = $db->getOne("SELECT goods_id FROM " .$ecs->table('cart'). " WHERE rec_id = '$rec_id' AND session_id = '" . SESS_ID . "' "); $count = $db->getOne("SELECT goods_id FROM " . $ecs->table('collect_goods') . " WHERE user_id = '$_SESSION[user_id]' AND goods_id = '$goods_id'"); if (empty($count)) { $time = gmtime(); $sql = "INSERT INTO " .$GLOBALS['ecs']->table('collect_goods'). " (user_id, goods_id, add_time)" . "VALUES ('$_SESSION[user_id]', '$goods_id', '$time')"; $db->query($sql); } flow_drop_cart_goods($rec_id); } ecs_header("Location: flow.php\n"); exit; } /* 验证红包序列号 */ elseif ($_REQUEST['step'] == 'validate_bonus') { $bonus_sn = trim($_REQUEST['bonus_sn']); if (is_numeric($bonus_sn)) { $bonus = bonus_info(0, $bonus_sn); } else { $bonus = array(); } // if (empty($bonus) || $bonus['user_id'] > 0 || $bonus['order_id'] > 0) // { // die($_LANG['bonus_sn_error']); // } // if ($bonus['min_goods_amount'] > cart_amount()) // { // die(sprintf($_LANG['bonus_min_amount_error'], price_format($bonus['min_goods_amount'], false))); // } // die(sprintf($_LANG['bonus_is_ok'], price_format($bonus['type_money'], false))); $bonus_kill = price_format($bonus['type_money'], false); include_once('includes/cls_json.php'); $result = array('error' => '', 'content' => ''); /* 取得购物类型 */ $flow_type = isset($_SESSION['flow_type']) ? intval($_SESSION['flow_type']) : CART_GENERAL_GOODS; /* 获得收货人信息 */ $consignee = get_consignee($_SESSION['user_id']); $consignee['country']= $_REQUEST['country'] ? $_REQUEST['country'] : 1; $consignee['province']= $_REQUEST['province'] ? $_REQUEST['province'] :0; $consignee['city']= $_REQUEST['city'] ? $_REQUEST['city'] :0; $consignee['district']= $_REQUEST['district'] ? $_REQUEST['district'] :0; /* 对商品信息赋值 */ $cart_goods = cart_goods($flow_type); // 取得商品列表,计算合计 if (empty($cart_goods) ) { $result['error'] = $_LANG['no_goods_in_cart']; } else { /* 取得购物流程设置 */ $smarty->assign('config', $_CFG); /* 取得订单信息 */ $order = flow_order_info(); if (((!empty($bonus) && $bonus['user_id'] == $_SESSION['user_id']) || ($bonus['type_money'] > 0 && empty($bonus['user_id']))) && $bonus['order_id'] <= 0) { //$order['bonus_kill'] = $bonus['type_money']; $now = gmtime(); if ($now > $bonus['use_end_date']) { $order['bonus_id'] = ''; $result['error']=$_LANG['bonus_use_expire']; } else { $order['bonus_id'] = $bonus['bonus_id']; $order['bonus_sn'] = $bonus_sn; } } else { //$order['bonus_kill'] = 0; $order['bonus_id'] = ''; $result['error'] = $_LANG['invalid_bonus']; } /* 计算订单的费用 */ $total = order_fee($order, $cart_goods, $consignee); $smarty->assign('total', $total); /* 团购标志 */ if ($flow_type == CART_GROUP_BUY_GOODS) { $smarty->assign('is_group_buy', 1); } $result['content'] = $smarty->fetch('library/flow_order_total.lbi'); } $json = new JSON(); die($json->encode($result)); } /*------------------------------------------------------ */ //-- 添加礼包到购物车 /*------------------------------------------------------ */ elseif ($_REQUEST['step'] == 'add_package_to_cart') { include_once('includes/cls_json.php'); $_POST['package_info'] = json_str_iconv($_POST['package_info']); $result = array('error' => 0, 'message' => '', 'content' => '', 'package_id' => ''); $json = new JSON; if (empty($_POST['package_info'])) { $result['error'] = 1; die($json->encode($result)); } $package = $json->decode($_POST['package_info']); /* 如果是一步购物,先清空购物车 */ if ($_CFG['one_step_buy'] == '1') { clear_cart(); } /* 商品数量是否合法 */ if (!is_numeric($package->number) || intval($package->number) <= 0) { $result['error'] = 1; $result['message'] = $_LANG['invalid_number']; } else { /* 添加到购物车 */ if (add_package_to_cart($package->package_id, $package->number)) { if ($_CFG['cart_confirm'] > 2) { $result['message'] = ''; } else { $result['message'] = $_CFG['cart_confirm'] == 1 ? $_LANG['addto_cart_success_1'] : $_LANG['addto_cart_success_2']; } $result['content'] = insert_cart_info(); $result['one_step_buy'] = $_CFG['one_step_buy']; } else { $result['message'] = $err->last_message(); $result['error'] = $err->error_no; $result['package_id'] = stripslashes($package->package_id); } } //888 /* 取得商品列表,计算合计 */ $sql = 'SELECT SUM(goods_number) AS number, SUM(goods_price * goods_number) AS amount' . ' FROM ' . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' AND rec_type = '" . CART_GENERAL_GOODS . "'"; $row = $GLOBALS['db']->GetRow($sql); if ($row) { $number = intval($row['number']); $amount = floatval($row['amount']); } else { $number = 0; $amount = 0; } $result['goods_price'] = $amount; $result['goods_number'] = $number; $result['confirm_type'] = !empty($_CFG['cart_confirm']) ? $_CFG['cart_confirm'] : 2; die($json->encode($result)); } else { /* 标记购物流程为普通商品 */ $_SESSION['flow_type'] = CART_GENERAL_GOODS; /* 如果是一步购物,跳到结算中心 */ if ($_CFG['one_step_buy'] == '1') { ecs_header("Location: flow.php?step=checkout\n"); exit; } /* 取得商品列表,计算合计 */ $cart_goods = get_cart_goods(); $smarty->assign('goods_list', $cart_goods['goods_list']); $smarty->assign('total', $cart_goods['total']); //购物车的描述的格式化 $smarty->assign('shopping_money', sprintf($_LANG['shopping_money'], $cart_goods['total']['goods_price'])); $smarty->assign('market_price_desc', sprintf($_LANG['than_market_price'], $cart_goods['total']['market_price'], $cart_goods['total']['saving'], $cart_goods['total']['save_rate'])); // 显示收藏夹内的商品 if ($_SESSION['user_id'] > 0) { require_once(ROOT_PATH . 'includes/lib_clips.php'); $collection_goods = get_collection_goods($_SESSION['user_id']); $smarty->assign('collection_goods', $collection_goods); } /* 取得优惠活动 */ $favourable_list = favourable_list($_SESSION['user_rank']); usort($favourable_list, 'cmp_favourable'); $smarty->assign('favourable_list', $favourable_list); /* 计算折扣 */ $discount = compute_discount(); $smarty->assign('discount', $discount['discount']); $favour_name = empty($discount['name']) ? '' : join(',', $discount['name']); $smarty->assign('your_discount', sprintf($_LANG['your_discount'], $favour_name, price_format($discount['discount']))); /* 增加是否在购物车里显示商品图 */ $smarty->assign('show_goods_thumb', $GLOBALS['_CFG']['show_goods_in_cart']); /* 增加是否在购物车里显示商品属性 */ $smarty->assign('show_goods_attribute', $GLOBALS['_CFG']['show_attr_in_cart']); /* 购物车中商品配件列表 */ //取得购物车中基本件ID $sql = "SELECT goods_id " . "FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' " . "AND rec_type = '" . CART_GENERAL_GOODS . "' " . "AND is_gift = 0 " . "AND extension_code <> 'package_buy' " . "AND parent_id = 0 "; $parent_list = $GLOBALS['db']->getCol($sql); $fittings_list = get_goods_fittings($parent_list); $smarty->assign('fittings_list', $fittings_list); } $smarty->assign('currency_format', $_CFG['currency_format']); $smarty->assign('integral_scale', $_CFG['integral_scale']); $smarty->assign('step', $_REQUEST['step']); assign_dynamic('shopping_flow'); $smarty->display('flow.dwt'); /*------------------------------------------------------ */ //-- PRIVATE FUNCTION /*------------------------------------------------------ */ /** * 获得用户的可用积分 * * @access private * @return integral */ function flow_available_points() { $sql = "SELECT SUM(g.integral * c.goods_number) ". "FROM " . $GLOBALS['ecs']->table('cart') . " AS c, " . $GLOBALS['ecs']->table('goods') . " AS g " . "WHERE c.session_id = '" . SESS_ID . "' AND c.goods_id = g.goods_id AND c.is_gift = 0 AND g.integral > 0 " . "AND c.rec_type = '" . CART_GENERAL_GOODS . "'"; $val = intval($GLOBALS['db']->getOne($sql)); return integral_of_value($val); } /** * 更新购物车中的商品数量 * * @access public * @param array $arr * @return void */ function flow_update_cart($arr) { /* 处理 */ foreach ($arr AS $key => $val) { $val = intval(make_semiangle($val)); if (!is_numeric($key)) { continue; } if ($val <= 0 && !is_numeric($key)) { continue; } //查询: $sql = "SELECT `goods_id`, `goods_attr_id`, `product_id`, `extension_code` FROM" .$GLOBALS['ecs']->table('cart'). " WHERE rec_id='$key' AND session_id='" . SESS_ID . "'"; $goods = $GLOBALS['db']->getRow($sql); $sql = "SELECT g.goods_name, g.goods_number ". "FROM " .$GLOBALS['ecs']->table('goods'). " AS g, ". $GLOBALS['ecs']->table('cart'). " AS c ". "WHERE g.goods_id = c.goods_id AND c.rec_id = '$key'"; $row = $GLOBALS['db']->getRow($sql); //查询:系统启用了库存,检查输入的商品数量是否有效 if (intval($GLOBALS['_CFG']['use_storage']) > 0 && $goods['extension_code'] != 'package_buy') { if ($row['goods_number'] < $val) { show_message(sprintf($GLOBALS['_LANG']['stock_insufficiency'], $row['goods_name'], $row['goods_number'], $row['goods_number']),'返回上一页','flows.php?step=checkout'); exit; } /* 是货品 */ $goods['product_id'] = trim($goods['product_id']); if (!empty($goods['product_id'])) { $sql = "SELECT product_number FROM " .$GLOBALS['ecs']->table('products'). " WHERE goods_id = '" . $goods['goods_id'] . "' AND product_id = '" . $goods['product_id'] . "'"; $product_number = $GLOBALS['db']->getOne($sql); if ($product_number < $val) { show_message(sprintf($GLOBALS['_LANG']['stock_insufficiency'], $row['goods_name'], $product_number['product_number'], $product_number['product_number']), '返回上一页', 'flows.php?step=checkout'); exit; } } } elseif (intval($GLOBALS['_CFG']['use_storage']) > 0 && $goods['extension_code'] == 'package_buy') { if (judge_package_stock($goods['goods_id'], $val)) { show_message($GLOBALS['_LANG']['package_stock_insufficiency']); exit; } } /* 查询:检查该项是否为基本件 以及是否存在配件 */ /* 此处配件是指添加商品时附加的并且是设置了优惠价格的配件 此类配件都有parent_id goods_number为1 */ $sql = "SELECT b.goods_number, b.rec_id FROM " .$GLOBALS['ecs']->table('cart') . " a, " .$GLOBALS['ecs']->table('cart') . " b WHERE a.rec_id = '$key' AND a.session_id = '" . SESS_ID . "' AND a.extension_code <> 'package_buy' AND b.parent_id = a.goods_id AND b.session_id = '" . SESS_ID . "'"; $offers_accessories_res = $GLOBALS['db']->query($sql); //订货数量大于0 if ($val > 0) { /* 判断是否为超出数量的优惠价格的配件 删除*/ $row_num = 1; while ($offers_accessories_row = $GLOBALS['db']->fetchRow($offers_accessories_res)) { if ($row_num > $val) { $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' " . "AND rec_id = '" . $offers_accessories_row['rec_id'] ."' LIMIT 1"; $GLOBALS['db']->query($sql); } $row_num ++; } /* 处理超值礼包 */ if ($goods['extension_code'] == 'package_buy') { //更新购物车中的商品数量 $sql = "UPDATE " .$GLOBALS['ecs']->table('cart'). " SET goods_number = '$val' WHERE rec_id='$key' AND session_id='" . SESS_ID . "'"; } /* 处理普通商品或非优惠的配件 */ else { $attr_id = empty($goods['goods_attr_id']) ? array() : explode(',', $goods['goods_attr_id']); $goods_price = get_final_price($goods['goods_id'], $val, true, $attr_id); //更新购物车中的商品数量 $sql = "UPDATE " .$GLOBALS['ecs']->table('cart'). " SET goods_number = '$val', goods_price = '$goods_price' WHERE rec_id='$key' AND session_id='" . SESS_ID . "'"; } } //订货数量等于0 else { /* 如果是基本件并且有优惠价格的配件则删除优惠价格的配件 */ while ($offers_accessories_row = $GLOBALS['db']->fetchRow($offers_accessories_res)) { $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' " . "AND rec_id = '" . $offers_accessories_row['rec_id'] ."' LIMIT 1"; $GLOBALS['db']->query($sql); } $sql = "DELETE FROM " .$GLOBALS['ecs']->table('cart'). " WHERE rec_id='$key' AND session_id='" .SESS_ID. "'"; } $GLOBALS['db']->query($sql); } /* 删除所有赠品 */ $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" .SESS_ID. "' AND is_gift <> 0"; $GLOBALS['db']->query($sql); } /** * 检查订单中商品库存 * * @access public * @param array $arr * * @return void */ function flow_cart_stock($arr) { foreach ($arr AS $key => $val) { $val = intval(make_semiangle($val)); if (!is_numeric($key)) { continue; } if ($val <= 0 || !is_numeric($key)) { continue; } $sql = "SELECT `goods_id`, `goods_attr_id`, `extension_code` FROM" .$GLOBALS['ecs']->table('cart'). " WHERE rec_id='$key' AND session_id='" . SESS_ID . "'"; $goods = $GLOBALS['db']->getRow($sql); $sql = "SELECT g.goods_name, g.goods_number, c.product_id ". "FROM " .$GLOBALS['ecs']->table('goods'). " AS g, ". $GLOBALS['ecs']->table('cart'). " AS c ". "WHERE g.goods_id = c.goods_id AND c.rec_id = '$key'"; $row = $GLOBALS['db']->getRow($sql); //系统启用了库存,检查输入的商品数量是否有效 if (intval($GLOBALS['_CFG']['use_storage']) > 0 && $goods['extension_code'] != 'package_buy') { if ($row['goods_number'] < $val) { show_message(sprintf($GLOBALS['_LANG']['stock_insufficiency'], $row['goods_name'], $row['goods_number'], $row['goods_number'])); exit; } /* 是货品 */ $row['product_id'] = trim($row['product_id']); if (!empty($row['product_id'])) { $sql = "SELECT product_number FROM " .$GLOBALS['ecs']->table('products'). " WHERE goods_id = '" . $goods['goods_id'] . "' AND product_id = '" . $row['product_id'] . "'"; $product_number = $GLOBALS['db']->getOne($sql); if ($product_number < $val) { show_message(sprintf($GLOBALS['_LANG']['stock_insufficiency'], $row['goods_name'], $row['goods_number'], $row['goods_number'])); exit; } } } elseif (intval($GLOBALS['_CFG']['use_storage']) > 0 && $goods['extension_code'] == 'package_buy') { if (judge_package_stock($goods['goods_id'], $val)) { show_message($GLOBALS['_LANG']['package_stock_insufficiency']); exit; } } } } /** * 删除购物车中的商品 * * @access public * @param integer $id * @return void */ function flow_drop_cart_goods($id) { /* 取得商品id */ $sql = "SELECT * FROM " .$GLOBALS['ecs']->table('cart'). " WHERE rec_id = '$id'"; $row = $GLOBALS['db']->getRow($sql); if ($row) { //如果是超值礼包 if ($row['extension_code'] == 'package_buy') { $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' " . "AND rec_id = '$id' LIMIT 1"; } //如果是普通商品,同时删除所有赠品及其配件 elseif ($row['parent_id'] == 0 && $row['is_gift'] == 0) { /* 检查购物车中该普通商品的不可单独销售的配件并删除 */ $sql = "SELECT c.rec_id FROM " . $GLOBALS['ecs']->table('cart') . " AS c, " . $GLOBALS['ecs']->table('group_goods') . " AS gg, " . $GLOBALS['ecs']->table('goods'). " AS g WHERE gg.parent_id = '" . $row['goods_id'] . "' AND c.goods_id = gg.goods_id AND c.parent_id = '" . $row['goods_id'] . "' AND c.extension_code <> 'package_buy' AND gg.goods_id = g.goods_id AND g.is_alone_sale = 0"; $res = $GLOBALS['db']->query($sql); $_del_str = $id . ','; while ($id_alone_sale_goods = $GLOBALS['db']->fetchRow($res)) { $_del_str .= $id_alone_sale_goods['rec_id'] . ','; } $_del_str = trim($_del_str, ','); $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' " . "AND (rec_id IN ($_del_str) OR parent_id = '$row[goods_id]' OR is_gift <> 0)"; } //如果不是普通商品,只删除该商品即可 else { $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' " . "AND rec_id = '$id' LIMIT 1"; } $GLOBALS['db']->query($sql); } flow_clear_cart_alone(); } function flow_drop_cart_goods_ordernew($id) { /* 取得商品id */ $sql = "SELECT * FROM " .$GLOBALS['ecs']->table('cart_ordernew'). " WHERE rec_id = '$id'"; $row = $GLOBALS['db']->getRow($sql); if ($row) { //如果是超值礼包 if ($row['extension_code'] == 'package_buy') { $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart_ordernew') . " WHERE session_id = '" . SESS_ID . "' " . "AND rec_id = '$id' LIMIT 1"; } //如果是普通商品,同时删除所有赠品及其配件 elseif ($row['parent_id'] == 0 && $row['is_gift'] == 0) { /* 检查购物车中该普通商品的不可单独销售的配件并删除 */ $sql = "SELECT c.rec_id FROM " . $GLOBALS['ecs']->table('cart_ordernew') . " AS c, " . $GLOBALS['ecs']->table('group_goods') . " AS gg, " . $GLOBALS['ecs']->table('goods'). " AS g WHERE gg.parent_id = '" . $row['goods_id'] . "' AND c.goods_id = gg.goods_id AND c.parent_id = '" . $row['goods_id'] . "' AND c.extension_code <> 'package_buy' AND gg.goods_id = g.goods_id AND g.is_alone_sale = 0"; $res = $GLOBALS['db']->query($sql); $_del_str = $id . ','; while ($id_alone_sale_goods = $GLOBALS['db']->fetchRow($res)) { $_del_str .= $id_alone_sale_goods['rec_id'] . ','; } $_del_str = trim($_del_str, ','); $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart_ordernew') . " WHERE session_id = '" . SESS_ID . "' " . "AND (rec_id IN ($_del_str) OR parent_id = '$row[goods_id]' OR is_gift <> 0)"; } //如果不是普通商品,只删除该商品即可 else { $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart_ordernew') . " WHERE session_id = '" . SESS_ID . "' " . "AND rec_id = '$id' LIMIT 1"; } $GLOBALS['db']->query($sql); } } function flow_drop_cart_diaopai($id) { /* 取得商品id */ $sql = "SELECT * FROM " .$GLOBALS['ecs']->table('cart_diaopai'). " WHERE rec_id = '$id'"; $row = $GLOBALS['db']->getRow($sql); if ($row) { //如果是超值礼包 if ($row['extension_code'] == 'package_buy') { $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart_diaopai') . " WHERE session_id = '" . SESS_ID . "' " . "AND rec_id = '$id' LIMIT 1"; } //如果是普通商品,同时删除所有赠品及其配件 elseif ($row['parent_id'] == 0 && $row['is_gift'] == 0) { /* 检查购物车中该普通商品的不可单独销售的配件并删除 */ $sql = "SELECT c.rec_id FROM " . $GLOBALS['ecs']->table('cart_diaopai') . " AS c, " . $GLOBALS['ecs']->table('group_goods') . " AS gg, " . $GLOBALS['ecs']->table('goods'). " AS g WHERE gg.parent_id = '" . $row['goods_id'] . "' AND c.goods_id = gg.goods_id AND c.parent_id = '" . $row['goods_id'] . "' AND c.extension_code <> 'package_buy' AND gg.goods_id = g.goods_id AND g.is_alone_sale = 0"; $res = $GLOBALS['db']->query($sql); $_del_str = $id . ','; while ($id_alone_sale_goods = $GLOBALS['db']->fetchRow($res)) { $_del_str .= $id_alone_sale_goods['rec_id'] . ','; } $_del_str = trim($_del_str, ','); $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart_diaopai') . " WHERE session_id = '" . SESS_ID . "' " . "AND (rec_id IN ($_del_str) OR parent_id = '$row[goods_id]' OR is_gift <> 0)"; } //如果不是普通商品,只删除该商品即可 else { $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart_diaopai') . " WHERE session_id = '" . SESS_ID . "' " . "AND rec_id = '$id' LIMIT 1"; } $GLOBALS['db']->query($sql); } } /** * 删除购物车中不能单独销售的商品 * * @access public * @return void */ function flow_clear_cart_alone() { /* 查询:购物车中所有不可以单独销售的配件 */ $sql = "SELECT c.rec_id, gg.parent_id FROM " . $GLOBALS['ecs']->table('cart') . " AS c LEFT JOIN " . $GLOBALS['ecs']->table('group_goods') . " AS gg ON c.goods_id = gg.goods_id LEFT JOIN" . $GLOBALS['ecs']->table('goods') . " AS g ON c.goods_id = g.goods_id WHERE c.session_id = '" . SESS_ID . "' AND c.extension_code <> 'package_buy' AND gg.parent_id > 0 AND g.is_alone_sale = 0"; $res = $GLOBALS['db']->query($sql); $rec_id = array(); while ($row = $GLOBALS['db']->fetchRow($res)) { $rec_id[$row['rec_id']][] = $row['parent_id']; } if (empty($rec_id)) { return; } /* 查询:购物车中所有商品 */ $sql = "SELECT DISTINCT goods_id FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' AND extension_code <> 'package_buy'"; $res = $GLOBALS['db']->query($sql); $cart_good = array(); while ($row = $GLOBALS['db']->fetchRow($res)) { $cart_good[] = $row['goods_id']; } if (empty($cart_good)) { return; } /* 如果购物车中不可以单独销售配件的基本件不存在则删除该配件 */ $del_rec_id = ''; foreach ($rec_id as $key => $value) { foreach ($value as $v) { if (in_array($v, $cart_good)) { continue 2; } } $del_rec_id = $key . ','; } $del_rec_id = trim($del_rec_id, ','); if ($del_rec_id == '') { return; } /* 删除 */ $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') ." WHERE session_id = '" . SESS_ID . "' AND rec_id IN ($del_rec_id)"; $GLOBALS['db']->query($sql); } /** * 比较优惠活动的函数,用于排序(把可用的排在前面) * @param array $a 优惠活动a * @param array $b 优惠活动b * @return int 相等返回0,小于返回-1,大于返回1 */ function cmp_favourable($a, $b) { if ($a['available'] == $b['available']) { if ($a['sort_order'] == $b['sort_order']) { return 0; } else { return $a['sort_order'] < $b['sort_order'] ? -1 : 1; } } else { return $a['available'] ? -1 : 1; } } /** * 取得某用户等级当前时间可以享受的优惠活动 * @param int $user_rank 用户等级id,0表示非会员 * @return array */ function favourable_list($user_rank) { /* 购物车中已有的优惠活动及数量 */ $used_list = cart_favourable(); /* 当前用户可享受的优惠活动 */ $favourable_list = array(); $user_rank = ',' . $user_rank . ','; $now = gmtime(); $sql = "SELECT * " . "FROM " . $GLOBALS['ecs']->table('favourable_activity') . " WHERE CONCAT(',', user_rank, ',') LIKE '%" . $user_rank . "%'" . " AND start_time <= '$now' AND end_time >= '$now'" . " AND act_type = '" . FAT_GOODS . "'" . " ORDER BY sort_order"; $res = $GLOBALS['db']->query($sql); while ($favourable = $GLOBALS['db']->fetchRow($res)) { $favourable['start_time'] = local_date($GLOBALS['_CFG']['time_format'], $favourable['start_time']); $favourable['end_time'] = local_date($GLOBALS['_CFG']['time_format'], $favourable['end_time']); $favourable['formated_min_amount'] = price_format($favourable['min_amount'], false); $favourable['formated_max_amount'] = price_format($favourable['max_amount'], false); $favourable['gift'] = unserialize($favourable['gift']); foreach ($favourable['gift'] as $key => $value) { $favourable['gift'][$key]['formated_price'] = price_format($value['price'], false); $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('goods') . " WHERE is_on_sale = 1 AND goods_id = ".$value['id']; $is_sale = $GLOBALS['db']->getOne($sql); if(!$is_sale) { unset($favourable['gift'][$key]); } } $favourable['act_range_desc'] = act_range_desc($favourable); $favourable['act_type_desc'] = sprintf($GLOBALS['_LANG']['fat_ext'][$favourable['act_type']], $favourable['act_type_ext']); /* 是否能享受 */ $favourable['available'] = favourable_available($favourable); if ($favourable['available']) { /* 是否尚未享受 */ $favourable['available'] = !favourable_used($favourable, $used_list); } $favourable_list[] = $favourable; } return $favourable_list; } /** * 根据购物车判断是否可以享受某优惠活动 * @param array $favourable 优惠活动信息 * @return bool */ function favourable_available($favourable) { /* 会员等级是否符合 */ $user_rank = $_SESSION['user_rank']; if (strpos(',' . $favourable['user_rank'] . ',', ',' . $user_rank . ',') === false) { return false; } /* 优惠范围内的商品总额 */ $amount = cart_favourable_amount($favourable); /* 金额上限为0表示没有上限 */ return $amount >= $favourable['min_amount'] && ($amount <= $favourable['max_amount'] || $favourable['max_amount'] == 0); } /** * 取得优惠范围描述 * @param array $favourable 优惠活动 * @return string */ function act_range_desc($favourable) { if ($favourable['act_range'] == FAR_BRAND) { $sql = "SELECT brand_name FROM " . $GLOBALS['ecs']->table('brand') . " WHERE brand_id " . db_create_in($favourable['act_range_ext']); return join(',', $GLOBALS['db']->getCol($sql)); } elseif ($favourable['act_range'] == FAR_CATEGORY) { $sql = "SELECT cat_name FROM " . $GLOBALS['ecs']->table('category') . " WHERE cat_id " . db_create_in($favourable['act_range_ext']); return join(',', $GLOBALS['db']->getCol($sql)); } elseif ($favourable['act_range'] == FAR_GOODS) { $sql = "SELECT goods_name FROM " . $GLOBALS['ecs']->table('goods') . " WHERE goods_id " . db_create_in($favourable['act_range_ext']); return join(',', $GLOBALS['db']->getCol($sql)); } else { return ''; } } /** * 取得购物车中已有的优惠活动及数量 * @return array */ function cart_favourable() { $list = array(); $sql = "SELECT is_gift, COUNT(*) AS num " . "FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "'" . " AND rec_type = '" . CART_GENERAL_GOODS . "'" . " AND is_gift > 0" . " GROUP BY is_gift"; $res = $GLOBALS['db']->query($sql); while ($row = $GLOBALS['db']->fetchRow($res)) { $list[$row['is_gift']] = $row['num']; } return $list; } /** * 购物车中是否已经有某优惠 * @param array $favourable 优惠活动 * @param array $cart_favourable购物车中已有的优惠活动及数量 */ function favourable_used($favourable, $cart_favourable) { if ($favourable['act_type'] == FAT_GOODS) { return isset($cart_favourable[$favourable['act_id']]) && $cart_favourable[$favourable['act_id']] >= $favourable['act_type_ext'] && $favourable['act_type_ext'] > 0; } else { return isset($cart_favourable[$favourable['act_id']]); } } /** * 添加优惠活动(赠品)到购物车 * @param int $act_id 优惠活动id * @param int $id 赠品id * @param float $price 赠品价格 */ function add_gift_to_cart($act_id, $id, $price) { $sql = "INSERT INTO " . $GLOBALS['ecs']->table('cart') . " (" . "user_id, session_id, goods_id, goods_sn, goods_name, market_price, goods_price, ". "goods_number, is_real, extension_code, parent_id, is_gift, rec_type ) ". "SELECT '$_SESSION[user_id]', '" . SESS_ID . "', goods_id, goods_sn, goods_name, market_price, ". "'$price', 1, is_real, extension_code, 0, '$act_id', '" . CART_GENERAL_GOODS . "' " . "FROM " . $GLOBALS['ecs']->table('goods') . " WHERE goods_id = '$id'"; $GLOBALS['db']->query($sql); } /** * 添加优惠活动(非赠品)到购物车 * @param int $act_id 优惠活动id * @param string $act_name 优惠活动name * @param float $amount 优惠金额 */ function add_favourable_to_cart($act_id, $act_name, $amount) { $sql = "INSERT INTO " . $GLOBALS['ecs']->table('cart') . "(" . "user_id, session_id, goods_id, goods_sn, goods_name, market_price, goods_price, ". "goods_number, is_real, extension_code, parent_id, is_gift, rec_type ) ". "VALUES('$_SESSION[user_id]', '" . SESS_ID . "', 0, '', '$act_name', 0, ". "'" . (-1) * $amount . "', 1, 0, '', 0, '$act_id', '" . CART_GENERAL_GOODS . "')"; $GLOBALS['db']->query($sql); } /** * 取得购物车中某优惠活动范围内的总金额 * @param array $favourable 优惠活动 * @return float */ function cart_favourable_amount($favourable) { /* 查询优惠范围内商品总额的sql */ $sql = "SELECT SUM(c.goods_price * c.goods_number) " . "FROM " . $GLOBALS['ecs']->table('cart') . " AS c, " . $GLOBALS['ecs']->table('goods') . " AS g " . "WHERE c.goods_id = g.goods_id " . "AND c.session_id = '" . SESS_ID . "' " . "AND c.rec_type = '" . CART_GENERAL_GOODS . "' " . "AND c.is_gift = 0 " . "AND c.goods_id > 0 "; /* 根据优惠范围修正sql */ if ($favourable['act_range'] == FAR_ALL) { // sql do not change } elseif ($favourable['act_range'] == FAR_CATEGORY) { /* 取得优惠范围分类的所有下级分类 */ $id_list = array(); $cat_list = explode(',', $favourable['act_range_ext']); foreach ($cat_list as $id) { $id_list = array_merge($id_list, array_keys(cat_list(intval($id), 0, false))); } $sql .= "AND g.cat_id " . db_create_in($id_list); } elseif ($favourable['act_range'] == FAR_BRAND) { $id_list = explode(',', $favourable['act_range_ext']); $sql .= "AND g.brand_id " . db_create_in($id_list); } else { $id_list = explode(',', $favourable['act_range_ext']); $sql .= "AND g.goods_id " . db_create_in($id_list); } /* 优惠范围内的商品总额 */ return $GLOBALS['db']->getOne($sql); } ?>