Ejemplo n.º 1
0
/**
 * 更新购物车中的商品
 */
function update_cart()
{
    global $ecs, $db;
    //删除已下架商品
    $sql = "DELETE c FROM " . $ecs->table('cart', 'c') . " WHERE " . get_cart_cond('c.') . " AND NOT EXISTS (SELECT 1 FROM " . $ecs->table('goods', 'g') . " WHERE g.goods_id = c.goods_id AND g.is_on_sale = 1 AND g.is_delete = 0) " . " AND c.extension_code <> 'flash_sale' AND c.rec_type = " . CART_GENERAL_GOODS;
    $db->query($sql);
    //删除已过期限时抢购
    $now = time();
    $sql = "DELETE c FROM " . $ecs->table('cart', 'c') . " WHERE " . get_cart_cond('c.') . " AND NOT EXISTS (SELECT 1 FROM " . $ecs->table('flash_sale', 'f') . " WHERE f.id = c.extension_id AND f.is_on_sale = 1 AND f.start_time <= {$now} AND f.end_time > {$now}) " . " AND c.extension_code = 'flash_sale' AND c.rec_type = " . CART_GENERAL_GOODS;
    $db->query($sql);
    //限时抢购商品限购数量检查
    check_cartgoods_number(true);
    //更新产品价格、规格等
    $sql = "SELECT c.rec_id, c.goods_id, c.goods_number, c.goods_attr_id, g.goods_name, g.market_price, g.shop_price, g.amount_desc, g.free_more FROM " . $ecs->table('cart', 'c') . ',' . $ecs->table('goods', 'g') . " WHERE c.goods_id = g.goods_id AND c.extension_code <> 'flash_sale' AND " . get_cart_cond('c.');
    $query = $db->query($sql);
    while ($rs = $db->fetch_array($query)) {
        $spec = explode(',', $rs['goods_attr_id']);
        $goods_attr = get_goods_attr_info($spec);
        if (empty($goods_attr)) {
            $goods_attr = $rs['amount_desc'];
        } else {
            $goods_attr = $rs['amount_desc'] . "\n" . $goods_attr;
        }
        $cart = array('goods_name' => $rs['goods_name'], 'market_price' => $rs['market_price'], 'goods_price' => get_final_price($rs['goods_id'], $rs['goods_number'], true, $spec), 'goods_attr' => $goods_attr, 'free_more' => $rs['free_more']);
        $db->autoExecute($ecs->table('cart'), $cart, 'UPDATE', 'rec_id=' . $rs['rec_id']);
    }
}
Ejemplo n.º 2
0
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'price') {
    include 'includes/cls_json.php';
    $json = new JSON();
    $res = array('err_msg' => '', 'result' => '', 'qty' => 1);
    $attr_id = isset($_REQUEST['attr']) ? explode(',', $_REQUEST['attr']) : array();
    $number = isset($_REQUEST['number']) ? intval($_REQUEST['number']) : 1;
    if ($goods_id == 0) {
        $res['err_msg'] = $_LANG['err_change_attr'];
        $res['err_no'] = 1;
    } else {
        if ($number == 0) {
            $res['qty'] = $number = 1;
        } else {
            $res['qty'] = $number;
        }
        $shop_price = get_final_price($goods_id, $number, true, $attr_id);
        $res['result'] = price_format($shop_price * $number);
    }
    die($json->encode($res));
}
/* 修改 start by zhouH*/
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'getGoodsInfo') {
    include 'includes/cls_json.php';
    $json = new JSON();
    $res = array('err_msg' => '', 'result' => '', 'goods_img' => '');
    $goods_id = $_REQUEST['id'];
    if (!empty($goods_id)) {
        /* 获得商品的信息 */
        $goods = get_goods_info($goods_id);
        $res['result'] = $goods;
        $res['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($goods['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $goods['goods_name'];
Ejemplo n.º 3
0
/**
 * 更新购物车中的商品数量
 *
 * @access  public
 * @param   array   $arr
 * @return  void
 */
function flow_update_cart($arr)
{
    /* 处理 */
    foreach ($arr as $key => $val) {
        $val = intval(make_semiangle($val));
        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']));
                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']));
                    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\n                FROM " . $GLOBALS['ecs']->table('cart') . " a, " . $GLOBALS['ecs']->table('cart') . " b\n                WHERE a.rec_id = '{$key}'\n                AND a.session_id = '" . SESS_ID . "'\n                AND a.extension_code <> 'package_buy'\n                AND b.parent_id = a.goods_id\n                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 . "'";
            }
        } 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);
}
Ejemplo n.º 4
0
/**
 * 重新计算购物车中的商品价格:目的是当用户登录时享受会员价格,当用户退出登录时不享受会员价格
 * 如果商品有促销,价格不变
 *
 * @access  public
 * @return  void
 */
function recalculate_price()
{
    /* 取得有可能改变价格的商品:除配件和赠品之外的商品 */
    $sql = 'SELECT c.rec_id, c.goods_id, c.goods_attr_id, g.promote_price, g.promote_start_date, c.goods_number,' . "g.promote_end_date, IFNULL(mp.user_price, g.shop_price * '{$_SESSION['discount']}') AS member_price " . 'FROM ' . $GLOBALS['ecs']->table('cart') . ' AS c ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . ' AS g ON g.goods_id = c.goods_id ' . "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp " . "ON mp.goods_id = g.goods_id AND mp.user_rank = '" . $_SESSION['user_rank'] . "' " . "WHERE session_id = '" . SESS_ID . "' AND c.parent_id = 0 AND c.is_gift = 0 AND c.goods_id > 0 " . "AND c.rec_type = '" . CART_GENERAL_GOODS . "' AND c.extension_code <> 'package_buy'";
    $res = $GLOBALS['db']->getAll($sql);
    foreach ($res as $row) {
        $attr_id = empty($row['goods_attr_id']) ? array() : explode(',', $row['goods_attr_id']);
        $goods_price = get_final_price($row['goods_id'], $row['goods_number'], true, $attr_id);
        $goods_sql = "UPDATE " . $GLOBALS['ecs']->table('cart') . " SET goods_price = '{$goods_price}' " . "WHERE goods_id = '" . $row['goods_id'] . "' AND session_id = '" . SESS_ID . "' AND rec_id = '" . $row['rec_id'] . "'";
        $GLOBALS['db']->query($goods_sql);
    }
    /* 删除赠品,重新选择 */
    $GLOBALS['db']->query('DELETE FROM ' . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' AND is_gift > 0");
}
Ejemplo n.º 5
0
            $res['err_no'] = 1;
        }
    }
    die($json->encode($res));
}
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'allprice') {
    include 'includes/cls_json.php';
    $json = new JSON();
    $res = array('err_msg' => '', 'result' => '', 'qty' => 1);
    $number = isset($_REQUEST['number']) ? intval($_REQUEST['number']) : 1;
    $attr_id = isset($_REQUEST['attr']) ? $_REQUEST['attr'] : array();
    if (empty($attr_id)) {
        $attr_id = 0;
    }
    $res['attr_num'] = get_product_attr_num($goods_id, $attr_id);
    $min_price = get_final_price($goods_id, $number, true, 0);
    $mark_price_min = get_mark_price($goods_id);
    $sql = "SELECT *\n\t\t\tFROM " . $ecs->table('goods_attr') . "\n\t\t\tWHERE `goods_id` =" . $goods_id;
    $row = $GLOBALS['db']->getAll($sql);
    //file_put_contents('./88.txt',var_export($row,true));
    if ($row) {
        $ret = array();
        foreach ($row as $key => $val) {
            if ($val['attr_price']) {
                $ret[$val['attr_id']][$val['attr_price']] = $val;
            }
        }
        $ret1 = $ret2 = $ret3 = $ret4 = array();
        foreach ($ret as $k => $v) {
            ksort($v);
            $ret2 = end($v);
Ejemplo n.º 6
0
function flow_update_cart($arr)
{
    foreach ($arr as $key => $val) {
        $val = intval(make_semiangle($val));
        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']));
                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']));
                    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;
            }
        }
        $sql = "SELECT b.goods_number, b.rec_id\r\n                FROM " . $GLOBALS['ecs']->table('cart') . " a, " . $GLOBALS['ecs']->table('cart') . " b\r\n                WHERE a.rec_id = '{$key}'\r\n                AND a.session_id = '" . SESS_ID . "'\r\n                AND a.extension_code <> 'package_buy'\r\n                AND b.parent_id = a.goods_id\r\n                AND b.session_id = '" . SESS_ID . "'";
        $offers_accessories_res = $GLOBALS['db']->query($sql);
        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 . "'";
            }
        } 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);
}
Ejemplo n.º 7
0
function tongbu_cart_price($goods_id)
{
    global $db, $ecs;
    $sql = "select c.rec_id,c.goods_id,c.goods_attr_id,c.user_id,c.session_id,g.market_price from " . $ecs->table('cart') . " as c left join " . $ecs->table('goods') . " as g on c.goods_id=g.goods_id where c.goods_id=" . $goods_id . " and c.rec_type='" . CART_GENERAL_GOODS . "' AND c.extension_code <> 'package_buy'";
    $query = $db->query($sql);
    while ($row = $db->fetchRow($query)) {
        if ($row['user_id'] > 0) {
            //已经有用户的商品
            $sql1 = "select u.user_rank,IFNULL(ur.discount,100) as discount from " . $ecs->table('users') . " as u left join " . $ecs->table('user_rank') . " as ur on u.user_rank=ur.rank_id where u.user_id=" . $row['user_id'];
            $data = $db->getRow($sql1);
            $GLOBALS['tongbu_user_discount'] = $data['discount'] / 100;
            $GLOBALS['tongbu_user_rank'] = $data['user_rank'];
        } else {
            $GLOBALS['tongbu_user_discount'] = 1;
            $GLOBALS['tongbu_user_rank'] = 1;
        }
        $attr_id = empty($row['goods_attr_id']) ? array() : explode(',', $row['goods_attr_id']);
        $price = get_final_price($row['goods_id'], 1, true, $attr_id);
        $db->query("update " . $ecs->table('cart') . " set market_price='" . $row['market_price'] . "',goods_price='" . $price . "' where rec_id=" . $row['rec_id']);
    }
}
Ejemplo n.º 8
0
/**
 * 添加商品到购物车
 *
 * @access  public
 * @param   integer $goods_id   商品编号
 * @param   integer $num        商品数量
 * @param   array   $spec       规格值对应的id数组
 * @param   integer $parent     基本件
 * @return  boolean
 */
function addto_cart($goods_id, $num = 1, $spec = array(), $parent = 0)
{
    $GLOBALS['err']->clean();
    $_parent_id = $parent;
    /* 取得商品信息 */
    $sql = "SELECT g.goods_name, g.goods_sn, g.is_on_sale, g.is_real, " . "g.market_price, g.cost_price, g.shop_price AS org_price, g.promote_price, g.promote_start_date, " . "g.promote_end_date, g.goods_weight, g.integral, g.extension_code, " . "g.goods_number, g.is_alone_sale, g.is_shipping," . "IFNULL(mp.user_price, g.shop_price * '{$_SESSION['discount']}') AS shop_price " . " FROM " . $GLOBALS['ecs']->table('goods') . " AS g " . " LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp " . "ON mp.goods_id = g.goods_id AND mp.user_rank = '{$_SESSION['user_rank']}' " . " WHERE g.goods_id = '{$goods_id}'" . " AND g.is_delete = 0";
    $goods = $GLOBALS['db']->getRow($sql);
    if (empty($goods)) {
        $GLOBALS['err']->add($GLOBALS['_LANG']['goods_not_exists'], ERR_NOT_EXISTS);
        return false;
    }
    /* 如果是作为配件添加到购物车的,需要先检查购物车里面是否已经有基本件 */
    if ($parent > 0) {
        $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('cart') . " WHERE goods_id='{$parent}' AND session_id='" . SESS_ID . "' AND extension_code <> 'package_buy'";
        if ($GLOBALS['db']->getOne($sql) == 0) {
            $GLOBALS['err']->add($GLOBALS['_LANG']['no_basic_goods'], ERR_NO_BASIC_GOODS);
            return false;
        }
    }
    /* 是否正在销售 */
    if ($goods['is_on_sale'] == 0) {
        $GLOBALS['err']->add($GLOBALS['_LANG']['not_on_sale'], ERR_NOT_ON_SALE);
        return false;
    }
    /* 不是配件时检查是否允许单独销售 */
    if (empty($parent) && $goods['is_alone_sale'] == 0) {
        $GLOBALS['err']->add($GLOBALS['_LANG']['cannt_alone_sale'], ERR_CANNT_ALONE_SALE);
        return false;
    }
    /* 如果商品有规格则取规格商品信息 配件除外 */
    $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('products') . " WHERE goods_id = '{$goods_id}' LIMIT 0, 1";
    $prod = $GLOBALS['db']->getRow($sql);
    if (is_spec($spec) && !empty($prod)) {
        $product_info = get_products_info($goods_id, $spec);
    }
    if (empty($product_info)) {
        $product_info = array('product_number' => '', 'product_id' => 0);
    }
    /* 检查:库存 */
    if ($GLOBALS['_CFG']['use_storage'] == 1) {
        //检查:商品购买数量是否大于总库存
        if ($num > $goods['goods_number']) {
            $GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['shortage'], $goods['goods_number']), ERR_OUT_OF_STOCK);
            return false;
        }
        //商品存在规格 是货品 检查该货品库存
        if (is_spec($spec) && !empty($prod)) {
            if (!empty($spec)) {
                /* 取规格的货品库存 */
                if ($num > $product_info['product_number']) {
                    $GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['shortage'], $product_info['product_number']), ERR_OUT_OF_STOCK);
                    return false;
                }
            }
        }
    }
    /* 计算商品的促销价格 */
    $spec_price = spec_price($spec);
    $goods_price = get_final_price($goods_id, $num, true, $spec);
    $goods['market_price'] += $spec_price;
    $goods_attr = get_goods_attr_info($spec);
    $goods_attr_id = join(',', $spec);
    /* 初始化要插入购物车的基本件数据 */
    $parent = array('user_id' => $_SESSION['user_id'], 'session_id' => SESS_ID, 'goods_id' => $goods_id, 'goods_sn' => addslashes($goods['goods_sn']), 'product_id' => $product_info['product_id'], 'goods_name' => addslashes($goods['goods_name']), 'market_price' => $goods['market_price'], 'goods_attr' => addslashes($goods_attr), 'goods_attr_id' => $goods_attr_id, 'cost_price' => $goods['cost_price'], 'is_real' => $goods['is_real'], 'extension_code' => $goods['extension_code'], 'is_gift' => 0, 'is_shipping' => $goods['is_shipping'], 'add_time' => gmtime(), 'rec_type' => CART_GENERAL_GOODS);
    //初始化为普通商品
    $_SESSION['flow_type'] = CART_GENERAL_GOODS;
    /* 如果该配件在添加为基本件的配件时,所设置的“配件价格”比原价低,即此配件在价格上提供了优惠, */
    /* 则按照该配件的优惠价格卖,但是每一个基本件只能购买一个优惠价格的“该配件”,多买的“该配件”不享 */
    /* 受此优惠 */
    $basic_list = array();
    $sql = "SELECT parent_id, goods_price " . "FROM " . $GLOBALS['ecs']->table('group_goods') . " WHERE goods_id = '{$goods_id}'" . " AND goods_price < '{$goods_price}'" . " AND parent_id = '{$_parent_id}'" . " ORDER BY goods_price";
    $res = $GLOBALS['db']->query($sql);
    while ($row = $GLOBALS['db']->fetchRow($res)) {
        $basic_list[$row['parent_id']] = $row['goods_price'];
    }
    /* 取得购物车中该商品每个基本件的数量 */
    $basic_count_list = array();
    if ($basic_list) {
        $sql = "SELECT goods_id, SUM(goods_number) AS count " . "FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "'" . " AND parent_id = 0" . " AND extension_code <> 'package_buy' " . " AND goods_id " . db_create_in(array_keys($basic_list)) . " GROUP BY goods_id";
        $res = $GLOBALS['db']->query($sql);
        while ($row = $GLOBALS['db']->fetchRow($res)) {
            $basic_count_list[$row['goods_id']] = $row['count'];
        }
    }
    /* 取得购物车中该商品每个基本件已有该商品配件数量,计算出每个基本件还能有几个该商品配件 */
    /* 一个基本件对应一个该商品配件 */
    if ($basic_count_list) {
        $sql = "SELECT parent_id, SUM(goods_number) AS count " . "FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "'" . " AND goods_id = '{$goods_id}'" . " AND extension_code <> 'package_buy' " . " AND parent_id " . db_create_in(array_keys($basic_count_list)) . " GROUP BY parent_id";
        $res = $GLOBALS['db']->query($sql);
        while ($row = $GLOBALS['db']->fetchRow($res)) {
            $basic_count_list[$row['parent_id']] -= $row['count'];
        }
    }
    /* 循环插入配件 如果是配件则用其添加数量依次为购物车中所有属于其的基本件添加足够数量的该配件 */
    foreach ($basic_list as $parent_id => $fitting_price) {
        /* 如果已全部插入,退出 */
        if ($num <= 0) {
            break;
        }
        /* 如果该基本件不再购物车中,执行下一个 */
        if (!isset($basic_count_list[$parent_id])) {
            continue;
        }
        /* 如果该基本件的配件数量已满,执行下一个基本件 */
        if ($basic_count_list[$parent_id] <= 0) {
            continue;
        }
        /* 作为该基本件的配件插入 */
        $parent['goods_price'] = max($fitting_price, 0) + $spec_price;
        //允许该配件优惠价格为0
        $parent['goods_number'] = min($num, $basic_count_list[$parent_id]);
        $parent['parent_id'] = $parent_id;
        /* 添加 */
        $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('cart'), $parent, 'INSERT');
        /* 改变数量 */
        $num -= $parent['goods_number'];
    }
    /* 如果数量不为0,作为基本件插入 */
    if ($num > 0) {
        /* 检查该商品是否已经存在在购物车中 */
        $sql = "SELECT goods_number FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' AND goods_id = '{$goods_id}' " . " AND parent_id = 0 AND goods_attr = '" . get_goods_attr_info($spec) . "' " . " AND extension_code <> 'package_buy' " . " AND user_id= '" . $_SESSION['user_id'] . "'" . " AND rec_type = 'CART_GENERAL_GOODS'";
        $row = $GLOBALS['db']->getRow($sql);
        if ($row) {
            $num += $row['goods_number'];
            if (is_spec($spec) && !empty($prod)) {
                $goods_storage = $product_info['product_number'];
            } else {
                $goods_storage = $goods['goods_number'];
            }
            if ($GLOBALS['_CFG']['use_storage'] == 0 || $num <= $goods_storage) {
                $goods_price = get_final_price($goods_id, $num, true, $spec);
                $sql = "UPDATE " . $GLOBALS['ecs']->table('cart') . " SET goods_number = '{$num}'" . " , goods_price = '{$goods_price}'" . " WHERE session_id = '" . SESS_ID . "' AND goods_id = '{$goods_id}' " . " AND parent_id = 0 AND goods_attr = '" . get_goods_attr_info($spec) . "' " . " AND extension_code <> 'package_buy' " . "AND rec_type = 'CART_GENERAL_GOODS'";
                $GLOBALS['db']->query($sql);
            } else {
                $GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['shortage'], $num), ERR_OUT_OF_STOCK);
                return false;
            }
        } else {
            // 判断是否为预售商品
            $pre_sale_id = is_pre_sale_goods($goods_id);
            if (!empty($pre_sale_id)) {
                /* 更新:记录购物流程类型:预售 */
                $_SESSION['flow_type'] = CART_PRE_SALE_GOODS;
                $_SESSION['extension_code'] = PRE_SALE_CODE;
                $_SESSION['extension_id'] = $pre_sale_id;
                $parent['extension_code'] = PRE_SALE_CODE;
                $parent['rec_type'] = CART_PRE_SALE_GOODS;
                //获取预售信息
                $pre_sale = pre_sale_info($pre_sale_id, $num);
                if ($pre_sale['deposit'] > 0) {
                    //定金大于0则使用定金金额
                    $goods_price = $pre_sale['deposit'];
                } else {
                    //计算当前价格
                    $goods_price = $pre_sale['cur_price'];
                    //加入规格价格
                    if (!empty($spec)) {
                        $spec_price = spec_price($spec);
                        $goods_price += $spec_price;
                    }
                }
            } else {
                $goods_price = get_final_price($goods_id, $num, true, $spec);
            }
            $parent['goods_price'] = max($goods_price, 0);
            $parent['goods_number'] = $num;
            $parent['parent_id'] = 0;
            $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('cart'), $parent, 'INSERT');
        }
        /**
         * 判断是否为虚拟团购商品
         */
        $virtual_sale_id = is_virtual_sale_goods($goods_id);
        if (!empty($virtual_sale_id)) {
            /* 更新:记录购物流程类型:预售 */
            $_SESSION['flow_type'] = CART_VIRTUAL_GROUP_GOODS;
            $_SESSION['extension_code'] = VIRTUAL_SALE_CODE;
            $_SESSION['extension_id'] = $virtual_sale_id;
            $GLOBALS['db']->query("update " . $GLOBALS['ecs']->table('cart') . " set rec_type=" . CART_VIRTUAL_GROUP_GOODS . " WHERE session_id = '" . SESS_ID . "' AND goods_id = '{$goods_id}' ");
        }
    }
    /* 把赠品删除 */
    $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' AND is_gift <> 0";
    $GLOBALS['db']->query($sql);
    return true;
}
Ejemplo n.º 9
0
/**
 * 获得商品的详细信息
 *
 * @access  public
 * @param   integer     $goods_id
 * @return  void
 */
function get_goods_info($goods_id)
{
    $time = gmtime();
    $sql = 'SELECT g.*, c.measure_unit, b.brand_id, b.brand_logo, g.comments_number, g.sales_volume,b.brand_name AS goods_brand, m.type_money AS bonus_money, ' . 'IFNULL(AVG(r.comment_rank), 0) AS comment_rank, ' . "IFNULL(mp.user_price, g.shop_price * '{$_SESSION['discount']}') AS rank_price " . 'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS c ON g.cat_id = c.cat_id ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON g.brand_id = b.brand_id ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('comment') . ' AS r ' . 'ON r.id_value = g.goods_id AND comment_type = 0 AND r.parent_id = 0 AND r.status = 1 ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('bonus_type') . ' AS m ' . "ON g.bonus_type_id = m.type_id AND m.send_start_date <= '{$time}' AND m.send_end_date >= '{$time}'" . " LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp " . "ON mp.goods_id = g.goods_id AND mp.user_rank = '{$_SESSION['user_rank']}' " . "WHERE g.goods_id = '{$goods_id}' AND g.is_delete = 0 " . "GROUP BY g.goods_id";
    $row = $GLOBALS['db']->getRow($sql);
    /*add by hg for date 2014-04-24 获取实际库存*/
    /* if($row['host_goods_id']){
    		$row['goods_number'] = host_number($row['host_goods_id']);
    	} 作用暂时不需要*/
    /*end*/
    if ($row !== false) {
        /* 用户评论级别取整 */
        $row['comment_rank'] = ceil($row['comment_rank']) == 0 ? 5 : ceil($row['comment_rank']);
        /* 折扣节省计算 by ecmoban start */
        if ($row['market_price'] > 0) {
            $discount_arr = get_discount($row['goods_id']);
            //函数get_discount参数goods_id
        }
        $row['zhekou'] = $discount_arr['discount'];
        //zhekou
        $row['jiesheng'] = $discount_arr['jiesheng'];
        //jiesheng
        /* 折扣节省计算 by ecmoban end */
        /* 获得商品的销售价格 */
        $row['market_price'] = price_format($row['market_price'], true, true);
        //$row['shop_price_formated'] = price_format($row['shop_price']);
        $row['shop_price_formated'] = price_format(get_final_price($goods_id));
        /* 修正促销价格 */
        if ($row['promote_price'] > 0) {
            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
        } else {
            $promote_price = 0;
        }
        /* 处理商品水印图片 */
        $watermark_img = '';
        if ($promote_price != 0) {
            $watermark_img = "watermark_promote";
        } elseif ($row['is_new'] != 0) {
            $watermark_img = "watermark_new";
        } elseif ($row['is_best'] != 0) {
            $watermark_img = "watermark_best";
        } elseif ($row['is_hot'] != 0) {
            $watermark_img = 'watermark_hot';
        }
        if ($watermark_img != '') {
            $row['watermark_img'] = $watermark_img;
        }
        $row['promote_price_org'] = $promote_price;
        $row['promote_price'] = price_format($promote_price);
        /* 修正重量显示 */
        $row['goods_weight'] = intval($row['goods_weight']) > 0 ? $row['goods_weight'] . $GLOBALS['_LANG']['kilogram'] : $row['goods_weight'] * 1000 . $GLOBALS['_LANG']['gram'];
        /* 修正上架时间显示 */
        $row['add_time'] = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']);
        /* 促销时间倒计时 */
        $time = gmtime();
        if ($time >= $row['promote_start_date'] && $time <= $row['promote_end_date']) {
            $row['gmt_end_time'] = $row['promote_end_date'];
        } else {
            $row['gmt_end_time'] = 0;
        }
        /* 是否显示商品库存数量 */
        $row['goods_number'] = $GLOBALS['_CFG']['use_storage'] == 1 ? $row['goods_number'] : '';
        /* 修正积分:转换为可使用多少积分(原来是可以使用多少钱的积分) */
        $row['integral'] = $GLOBALS['_CFG']['integral_scale'] ? round($row['integral'] * 100 / $GLOBALS['_CFG']['integral_scale']) : 0;
        /* 修正优惠券 */
        $row['bonus_money'] = $row['bonus_money'] == 0 ? 0 : price_format($row['bonus_money'], false);
        /* 修正商品图片 */
        $row['goods_img'] = get_image_path($goods_id, $row['goods_img']);
        $row['goods_thumb'] = get_image_path($goods_id, $row['goods_thumb'], true);
        return $row;
    } else {
        return false;
    }
}
Ejemplo n.º 10
0
/**
* 根据条形码获取对应商品信息
* @param array $info  条形码数组信息
*/
function get_goods_by_txm($info)
{
    global $db, $ecs;
    $sql = "select bc.*,g.* from " . $ecs->table('bar_code') . " as bc left join " . $ecs->table('goods') . " as g on bc.goods_id=g.goods_id where bc.bar_code in(" . implode(',', $info) . ")";
    $ret = $db->query($sql);
    $goodsinfo = array();
    while ($row = $db->fetchRow($ret)) {
        $attr_info = get_goods_attr_txm($row['goods_id'], $row['taypes']);
        //获取商品属性信息
        if (empty($attr_info['info'])) {
            $goods_number = $row['goods_number'];
            $attr_ids = '';
            $row['goods_attr'] = $row['goods_attr_id'] = $row['goods_attr_price'] = '';
        } else {
            $attr_ids = array_keys($attr_info['info']);
            $attr_ids = sort_goods_attr_id_array($attr_ids);
            //商品属性id排序
            $row['goods_attr_id'] = implode(',', $attr_ids['sort']);
            $row['goods_attr'] = $attr_info['info'];
            $row['goods_attr_price'] = is_array($attr_info['price']) ? array_sum($attr_info['price']) : 0;
            $goods_number = get_goods_nums_txm($row['goods_id'], $attr_ids['sort']);
            //货品总数量
        }
        $goodsinfo[$row['bar_code']]['goods_id'] = $row['goods_id'];
        $goodsinfo[$row['bar_code']]['goods_name'] = $row['goods_name'];
        $goodsinfo[$row['bar_code']]['goods_sn'] = $row['goods_sn'];
        $goodsinfo[$row['bar_code']]['product_id'] = '';
        $goodsinfo[$row['bar_code']]['goods_name'] = $row['goods_name'];
        $goodsinfo[$row['bar_code']]['market_price'] = $row['market_price'] + $row['goods_attr_price'];
        $goodsinfo[$row['bar_code']]['goods_price'] = get_final_price($row['goods_id'], 1, true, $attr_ids['sort']);
        //最终结算价格
        $goodsinfo[$row['bar_code']]['goods_number'] = intval($goods_number);
        $goodsinfo[$row['bar_code']]['goods_attr'] = $row['goods_attr'];
        $goodsinfo[$row['bar_code']]['goods_attr_id'] = $row['goods_attr_id'];
        $goodsinfo[$row['bar_code']]['goods_attr_price'] = $row['goods_attr_price'];
        $goodsinfo[$row['bar_code']]['goods_thumb'] = $row['goods_thumb'];
        $goodsinfo[$row['bar_code']]['goods_img'] = $row['goods_img'];
    }
    return $goodsinfo;
}
Ejemplo n.º 11
0
/**
 * 重新计算购物车中的商品价格:目的是当用户登录时享受会员价格,当用户退出登录时不享受会员价格
 * 如果商品有促销,价格不变
 *
 * @access  public
 * @return  void
 */
function recalculate_price()
{
    /* 取得有可能改变价格的商品:除配件和赠品之外的商品 */
    $sql = 'SELECT c.rec_id, c.goods_id, c.goods_attr_id, g.promote_price, g.promote_start_date, c.goods_number,' . "g.promote_end_date, IFNULL(mp.user_price, g.shop_price * '{$_SESSION['discount']}') AS member_price " . 'FROM ' . $GLOBALS['ecs']->table('cart') . ' AS c ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . ' AS g ON g.goods_id = c.goods_id ' . "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp " . "ON mp.goods_id = g.goods_id AND mp.user_rank = '" . $_SESSION['user_rank'] . "' " . "WHERE session_id = '" . SESS_ID . "' AND c.parent_id = 0 AND c.is_gift = 0 AND c.goods_id > 0 " . "AND c.rec_type = '" . CART_GENERAL_GOODS . "' AND c.extension_code <> 'package_buy'";
    $res = $GLOBALS['db']->getAll($sql);
    foreach ($res as $row) {
        $attr_id = empty($row['goods_attr_id']) ? array() : explode(',', $row['goods_attr_id']);
        $goods_price = get_final_price($row['goods_id'], $row['goods_number'], true, $attr_id);
        $goods_sql = "UPDATE " . $GLOBALS['ecs']->table('cart') . " SET goods_price = '{$goods_price}' " . "WHERE goods_id = '" . $row['goods_id'] . "' AND session_id = '" . SESS_ID . "' AND rec_id = '" . $row['rec_id'] . "'";
        $GLOBALS['db']->query($goods_sql);
    }
    /* 代码增加_start  By  www.68ecshop.com */
    $time1 = local_strtotime('today');
    $time2 = local_strtotime('today') + 86400;
    $sql = "select rec_id,goods_id,goods_attr,goods_number " . " from " . $GLOBALS['ecs']->table('cart') . " where user_id=0 " . " AND session_id = '" . SESS_ID . "' AND parent_id = 0 " . " AND is_gift = 0 AND goods_id > 0 " . "AND rec_type = '" . CART_GENERAL_GOODS . "' AND extension_code <> 'package_buy' ";
    $res = $GLOBALS['db']->query($sql);
    while ($row = $GLOBALS['db']->fetchRow($res)) {
        $sql = "select rec_id from " . $GLOBALS['ecs']->table('cart') . " where user_id='" . $_SESSION['user_id'] . "' " . " AND add_time >='{$time1}' and add_time<'{$time2}' " . " AND goods_id='{$row['goods_id']}' and goods_attr= '{$row['goods_attr']}' ";
        $rec_id = $GLOBALS['db']->getOne($sql);
        if ($rec_id) {
            $sql = "update " . $GLOBALS['ecs']->table('cart') . " set goods_number= goods_number + " . $row['goods_number'] . " where rec_id='{$rec_id}' ";
            $GLOBALS['db']->query($sql);
            $sql = "delete from " . $GLOBALS['ecs']->table('cart') . " where rec_id='" . $row['rec_id'] . "'";
            $GLOBALS['db']->query($sql);
        } else {
            $sql = "update " . $GLOBALS['ecs']->table('cart') . " set user_id='{$_SESSION['user_id']}' " . " where rec_id='" . $row['rec_id'] . "'";
            $GLOBALS['db']->query($sql);
        }
    }
    /* 代码增加_end  By  www.68ecshop.com */
    /* 删除赠品,重新选择 */
    $GLOBALS['db']->query('DELETE FROM ' . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' AND is_gift > 0");
}
Ejemplo n.º 12
0
function get_discount($goods_id)
{
    /*$sql = 'SELECT market_price,shop_price,promote_price FROM '.$GLOBALS['ecs']->table('goods')." WHERE goods_id = $goods_id ";
    		
    		
    		$row = $GLOBALS['db']->getRow($sql);
    		
    		$price=$row['market_price']; //原价 
    		if($row['promote_price'] > 0) //如果促销价大于0则现价为促销价
    		{
    			$nowprice=$row['promote_price']; //现价 
    		}
    		else //否则为本店价
    		{
    			$nowprice=$row['shop_price']; //现价 
    		}
    		$jiesheng=$price-$nowprice; //节省金额 
    		
    		*/
    $final_price = get_final_price($goods_id);
    $sql = 'SELECT market_price FROM ' . $GLOBALS['ecs']->table('goods') . " WHERE goods_id = {$goods_id} ";
    $market_price = $GLOBALS['db']->getOne($sql);
    $jiesheng = $market_price - $final_price;
    //节省金额
    $arr['jiesheng'] = $jiesheng;
    //$discount折扣计算
    if ($final_price > 0) {
        $arr['discount'] = round(10 * ($final_price / $market_price), 1);
    } else {
        $arr['discount'] = 0;
    }
    if ($arr['discount'] <= 0) {
        $arr['discount'] = 0;
    }
    return $arr;
}
Ejemplo n.º 13
0
/**
 * 添加商品到购物车
 *
 * @access  public
 * @param   integer $goods_id   商品编号
 * @param   integer $num        商品数量
 * @param   array   $spec       规格
 * @param   integer $parent     基本件
 * @return  boolean
 */
function addto_cart($goods_id, $num = 1, $spec = array(), $parent = 0)
{
    $GLOBALS['err']->clean();
    /* 取得商品信息 */
    $sql = "SELECT g.goods_name, g.goods_sn, g.is_on_sale, g.is_real, " . "g.market_price, g.shop_price AS org_price, g.promote_price, g.promote_start_date, " . "g.promote_end_date, g.goods_weight, g.integral, g.extension_code, " . "g.goods_number, g.is_alone_sale, " . "IFNULL(mp.user_price, g.shop_price * '{$_SESSION['discount']}') AS shop_price " . " FROM " . $GLOBALS['ecs']->table('goods') . " AS g " . " LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp " . "ON mp.goods_id = g.goods_id AND mp.user_rank = '{$_SESSION['user_rank']}' " . " WHERE g.goods_id = '{$goods_id}'" . " AND g.is_delete = 0";
    $goods = $GLOBALS['db']->getRow($sql);
    if (empty($goods)) {
        $GLOBALS['err']->add($GLOBALS['_LANG']['goods_not_exists'], ERR_NOT_EXISTS);
        return false;
    }
    /* 如果是作为配件添加到购物车的,需要先检查购物车里面是否已经有基本件 */
    if ($parent > 0) {
        $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('cart') . " WHERE goods_id='{$parent}' AND session_id='" . SESS_ID . "'";
        if ($GLOBALS['db']->getOne($sql) == 0) {
            $GLOBALS['err']->add($GLOBALS['_LANG']['no_basic_goods'], ERR_NO_BASIC_GOODS);
            return false;
        }
        /* 检查该配件是否已经添加过了。 */
        $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('cart') . " WHERE goods_id = '{$goods_id}' AND parent_id='{$parent}' AND session_id='" . SESS_ID . "'";
        if ($GLOBALS['db']->getOne($sql) > 0) {
            $GLOBALS['err']->add($GLOBALS['_LANG']['fitting_goods_exists'], ERR_NOT_EXISTS);
            return false;
        }
    }
    /* 是否正在销售 */
    if ($goods['is_on_sale'] == 0) {
        $GLOBALS['err']->add($GLOBALS['_LANG']['not_on_sale'], ERR_NOT_ON_SALE);
        return false;
    }
    /* 不是配件时检查是否允许单独销售 */
    if (empty($parent) && $goods['is_alone_sale'] == 0) {
        $GLOBALS['err']->add($GLOBALS['_LANG']['cannt_alone_sale'], ERR_CANNT_ALONE_SALE);
        return false;
    }
    /* 检查库存 */
    if ($GLOBALS['_CFG']['use_storage'] == 1 && $num > $goods['goods_number']) {
        $num = $goods['goods_number'];
        $GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['shortage'], $num), ERR_OUT_OF_STOCK);
        return false;
    }
    /* 计算商品的促销价格 */
    $spec_price = spec_price($spec);
    $goods_price = get_final_price($goods_id, $num, true, $spec);
    $goods['market_price'] += $spec_price;
    $goods_attr = get_goods_attr_info($spec);
    $goods_attr_id = join(',', $spec);
    /* 初始化要插入购物车的基本件数据 */
    $parent = array('user_id' => $_SESSION['user_id'], 'session_id' => SESS_ID, 'goods_id' => $goods_id, 'goods_sn' => addslashes($goods['goods_sn']), 'goods_name' => addslashes($goods['goods_name']), 'market_price' => $goods['market_price'], 'goods_attr' => addslashes($goods_attr), 'goods_attr_id' => $goods_attr_id, 'is_real' => $goods['is_real'], 'extension_code' => $goods['extension_code'], 'is_gift' => 0, 'rec_type' => CART_GENERAL_GOODS);
    /* 取得该商品的基本件和该商品作为其配件的价格(条件是价格低) */
    $basic_list = array();
    $sql = "SELECT parent_id, goods_price " . "FROM " . $GLOBALS['ecs']->table('group_goods') . " WHERE goods_id = '{$goods_id}'" . " AND goods_price < '{$goods_price}'" . " ORDER BY goods_price";
    $res = $GLOBALS['db']->query($sql);
    while ($row = $GLOBALS['db']->fetchRow($res)) {
        $basic_list[$row['parent_id']] = $row['goods_price'];
    }
    /* 取得购物车中该商品每个基本件的数量 */
    $basic_count_list = array();
    if ($basic_list) {
        $sql = "SELECT goods_id, SUM(goods_number) AS count " . "FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "'" . " AND parent_id = 0" . " AND goods_id " . db_create_in(array_keys($basic_list)) . " GROUP BY goods_id";
        $res = $GLOBALS['db']->query($sql);
        while ($row = $GLOBALS['db']->fetchRow($res)) {
            $basic_count_list[$row['goods_id']] = $row['count'];
        }
    }
    /* 取得购物车中该商品每个基本件已有该商品配件数量,计算出每个基本件还能有几个该商品配件 */
    if ($basic_count_list) {
        $sql = "SELECT parent_id, SUM(goods_number) AS count " . "FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "'" . " AND goods_id = '{$goods_id}'" . " AND parent_id " . db_create_in(array_keys($basic_count_list)) . " GROUP BY parent_id";
        $res = $GLOBALS['db']->query($sql);
        while ($row = $GLOBALS['db']->fetchRow($res)) {
            $basic_count_list[$row['parent_id']] -= $row['count'];
        }
    }
    /* 循环插入配件 */
    foreach ($basic_list as $parent_id => $fitting_price) {
        /* 如果已全部插入,退出 */
        if ($num <= 0) {
            break;
        }
        /* 如果该基本件不再购物车中,执行下一个 */
        if (!isset($basic_count_list[$parent_id])) {
            continue;
        }
        /* 如果该基本件的配件数量已满,执行下一个基本件 */
        if ($basic_count_list[$parent_id] <= 0) {
            continue;
        }
        /* 作为该基本件的配件插入 */
        $parent['goods_price'] = max($fitting_price, 0);
        $parent['goods_number'] = min($num, $basic_count_list[$parent_id]);
        $parent['parent_id'] = $parent_id;
        $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('cart'), $parent, 'INSERT');
        /* 改变数量 */
        $num -= $parent['goods_number'];
    }
    /* 如果数量不为0,作为基本件插入 */
    if ($num > 0) {
        /* 检查该商品是否已经存在在购物车中 */
        $sql = "SELECT goods_number FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' AND goods_id = '{$goods_id}' " . "AND parent_id = 0 AND goods_attr = '" . get_goods_attr_info($spec) . "' " . "AND rec_type = 'CART_GENERAL_GOODS'";
        $row = $GLOBALS['db']->getRow($sql);
        if ($row) {
            $num += $row['goods_number'];
            if ($GLOBALS['_CFG']['use_storage'] == 0 || $num <= $goods['goods_number']) {
                $goods_price = get_final_price($goods_id, $num, true, $spec);
                $sql = "UPDATE " . $GLOBALS['ecs']->table('cart') . " SET goods_number = '{$num}'" . " , goods_price = '{$goods_price}'" . " WHERE session_id = '" . SESS_ID . "' AND goods_id = '{$goods_id}' " . "AND parent_id = 0 AND goods_attr = '" . get_goods_attr_info($spec) . "' " . "AND rec_type = 'CART_GENERAL_GOODS'";
                $GLOBALS['db']->query($sql);
            } else {
                $GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['shortage'], $num), ERR_OUT_OF_STOCK);
                return false;
            }
        } else {
            $goods_price = get_final_price($goods_id, $num, true, $spec);
            $parent['goods_price'] = max($goods_price, 0);
            $parent['goods_number'] = $num;
            $parent['parent_id'] = 0;
            $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('cart'), $parent, 'INSERT');
        }
    }
    /* 把赠品删除 */
    $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' AND is_gift <> 0";
    $GLOBALS['db']->query($sql);
    return true;
}
Ejemplo n.º 14
0
/**
 * 添加商品到购物车
 *
 * @access  public
 * @param   integer $goods_id   商品编号
 * @param   integer $num        商品数量
 * @param   array   $spec       规格值对应的id数组
 * @param   integer $parent     基本件
 * @return  boolean
 */
function addto_cart($goods_id, $num = 1, $spec = array(), $parent = 0)
{
    $GLOBALS['err']->clean();
    $_parent_id = $parent;
    /*ccx 2015-03-03 判断该商品是否已经通过自动下架功能 start*/
    $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('auto_manage') . " WHERE item_id = {$goods_id} AND type = 'goods' ";
    $autodb = $GLOBALS['db']->getRow($sql);
    //print_r($autodb);exit;
    if ($autodb) {
        $where = " WHERE goods_id = '{$goods_id}'";
        if ($autodb['endtime'] < gmtime() && $autodb['endtime'] > 0) {
            $sql = "UPDATE " . $GLOBALS['ecs']->table('goods') . " SET is_on_sale = 0 {$where}";
            $GLOBALS['db']->query($sql);
            $sql_1 = "DELETE FROM " . $GLOBALS['ecs']->table('auto_manage') . "WHERE item_id = '{$goods_id}' AND type = 'goods'";
            $GLOBALS['db']->query($sql_1);
            $GLOBALS['err']->add($GLOBALS['_LANG']['not_on_sale'], ERR_NOT_ON_SALE);
            return false;
        }
        //var_dump($up);
    }
    /*ccx 2015-03-03 end*/
    /* 取得商品信息 */
    $sql = "SELECT g.goods_name, g.goods_sn, g.is_on_sale, g.is_real, " . "g.market_price, g.shop_price AS org_price, g.promote_price, g.promote_start_date, " . "g.promote_end_date, g.goods_weight, g.integral, g.extension_code, " . "g.goods_number, g.is_alone_sale, g.is_shipping," . "IFNULL(mp.user_price, g.shop_price * '{$_SESSION['discount']}') AS shop_price " . " FROM " . $GLOBALS['ecs']->table('goods') . " AS g " . " LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp " . "ON mp.goods_id = g.goods_id AND mp.user_rank = '{$_SESSION['user_rank']}' " . " WHERE g.goods_id = '{$goods_id}'" . " AND g.is_delete = 0";
    $goods = $GLOBALS['db']->getRow($sql);
    if (empty($goods)) {
        $GLOBALS['err']->add($GLOBALS['_LANG']['goods_not_exists'], ERR_NOT_EXISTS);
        return false;
    }
    /* 如果是作为配件添加到购物车的,需要先检查购物车里面是否已经有基本件 */
    if ($parent > 0) {
        $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('cart') . " WHERE goods_id='{$parent}' AND session_id='" . SESS_ID . "' AND extension_code <> 'package_buy'";
        if ($GLOBALS['db']->getOne($sql) == 0) {
            $GLOBALS['err']->add($GLOBALS['_LANG']['no_basic_goods'], ERR_NO_BASIC_GOODS);
            return false;
        }
    }
    /* 是否正在销售 */
    if ($goods['is_on_sale'] == 0) {
        $GLOBALS['err']->add($GLOBALS['_LANG']['not_on_sale'], ERR_NOT_ON_SALE);
        return false;
    }
    /* 不是配件时检查是否允许单独销售 */
    if (empty($parent) && $goods['is_alone_sale'] == 0) {
        $GLOBALS['err']->add($GLOBALS['_LANG']['cannt_alone_sale'], ERR_CANNT_ALONE_SALE);
        return false;
    }
    /* 如果商品有规格则取规格商品信息 配件除外 */
    $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('products') . " WHERE goods_id = '{$goods_id}' LIMIT 0, 1";
    $prod = $GLOBALS['db']->getRow($sql);
    if (is_spec($spec) && !empty($prod)) {
        $product_info = get_products_info($goods_id, $spec);
    }
    if (empty($product_info)) {
        $product_info = array('product_number' => '', 'product_id' => 0);
    }
    /* 检查:库存 */
    /* 2014-12-18 ccx 把购物的时候, 把检查库存这块屏蔽了, 不做库存判断
        if ($GLOBALS['_CFG']['use_storage'] == 1)
        {
            //检查:商品购买数量是否大于总库存
            if ($num > $goods['goods_number'])
            {
                $GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['shortage'], $goods['goods_number']), ERR_OUT_OF_STOCK);
    
                return false;
            }
    
            //商品存在规格 是货品 检查该货品库存
            if (is_spec($spec) && !empty($prod))
            {
                if (!empty($spec))
                {
                    // 取规格的货品库存 
                    if ($num > $product_info['product_number'])
                    {
                        $GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['shortage'], $product_info['product_number']), ERR_OUT_OF_STOCK);
        
                        return false;
                    }
                }
            }       
        }
        */
    /* 计算商品的促销价格 */
    $spec_price = spec_price($spec);
    $goods_price = get_final_price($goods_id, $num, true, $spec);
    $goods['market_price'] += $spec_price;
    $goods_attr = get_goods_attr_info($spec);
    $goods_attr_id = join(',', $spec);
    /* 初始化要插入购物车的基本件数据 */
    $parent = array('user_id' => $_SESSION['user_id'], 'session_id' => SESS_ID, 'goods_id' => $goods_id, 'goods_sn' => addslashes($goods['goods_sn']), 'product_id' => $product_info['product_id'], 'goods_name' => addslashes($goods['goods_name']), 'market_price' => $goods['market_price'], 'goods_attr' => addslashes($goods_attr), 'goods_attr_id' => $goods_attr_id, 'is_real' => $goods['is_real'], 'extension_code' => $goods['extension_code'], 'is_gift' => 0, 'is_shipping' => $goods['is_shipping'], 'rec_type' => CART_GENERAL_GOODS);
    /* 如果该配件在添加为基本件的配件时,所设置的“配件价格”比原价低,即此配件在价格上提供了优惠, */
    /* 则按照该配件的优惠价格卖,但是每一个基本件只能购买一个优惠价格的“该配件”,多买的“该配件”不享 */
    /* 受此优惠 */
    $basic_list = array();
    $sql = "SELECT parent_id, goods_price " . "FROM " . $GLOBALS['ecs']->table('group_goods') . " WHERE goods_id = '{$goods_id}'" . " AND goods_price < '{$goods_price}'" . " AND parent_id = '{$_parent_id}'" . " ORDER BY goods_price";
    $res = $GLOBALS['db']->query($sql);
    while ($row = $GLOBALS['db']->fetchRow($res)) {
        $basic_list[$row['parent_id']] = $row['goods_price'];
    }
    /* 取得购物车中该商品每个基本件的数量 */
    $basic_count_list = array();
    if ($basic_list) {
        $sql = "SELECT goods_id, SUM(goods_number) AS count " . "FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "'" . " AND parent_id = 0" . " AND extension_code <> 'package_buy' " . " AND goods_id " . db_create_in(array_keys($basic_list)) . " GROUP BY goods_id";
        $res = $GLOBALS['db']->query($sql);
        while ($row = $GLOBALS['db']->fetchRow($res)) {
            $basic_count_list[$row['goods_id']] = $row['count'];
        }
    }
    /* 取得购物车中该商品每个基本件已有该商品配件数量,计算出每个基本件还能有几个该商品配件 */
    /* 一个基本件对应一个该商品配件 */
    if ($basic_count_list) {
        $sql = "SELECT parent_id, SUM(goods_number) AS count " . "FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "'" . " AND goods_id = '{$goods_id}'" . " AND extension_code <> 'package_buy' " . " AND parent_id " . db_create_in(array_keys($basic_count_list)) . " GROUP BY parent_id";
        $res = $GLOBALS['db']->query($sql);
        while ($row = $GLOBALS['db']->fetchRow($res)) {
            $basic_count_list[$row['parent_id']] -= $row['count'];
        }
    }
    /* 循环插入配件 如果是配件则用其添加数量依次为购物车中所有属于其的基本件添加足够数量的该配件 */
    foreach ($basic_list as $parent_id => $fitting_price) {
        /* 如果已全部插入,退出 */
        if ($num <= 0) {
            break;
        }
        /* 如果该基本件不再购物车中,执行下一个 */
        if (!isset($basic_count_list[$parent_id])) {
            continue;
        }
        /* 如果该基本件的配件数量已满,执行下一个基本件 */
        if ($basic_count_list[$parent_id] <= 0) {
            continue;
        }
        /* 作为该基本件的配件插入 */
        $parent['goods_price'] = max($fitting_price, 0) + $spec_price;
        //允许该配件优惠价格为0
        $parent['goods_number'] = min($num, $basic_count_list[$parent_id]);
        $parent['parent_id'] = $parent_id;
        /* 添加 */
        $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('cart'), $parent, 'INSERT');
        /* 改变数量 */
        $num -= $parent['goods_number'];
    }
    /* 如果数量不为0,作为基本件插入 */
    if ($num > 0) {
        /* 检查该商品是否已经存在在购物车中 */
        $sql = "SELECT goods_number FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' AND goods_id = '{$goods_id}' " . " AND parent_id = 0 AND goods_attr = '" . get_goods_attr_info($spec) . "' " . " AND extension_code <> 'package_buy' " . " AND rec_type = 'CART_GENERAL_GOODS'";
        $row = $GLOBALS['db']->getRow($sql);
        if ($row) {
            $num += $row['goods_number'];
            if (is_spec($spec) && !empty($prod)) {
                $goods_storage = $product_info['product_number'];
            } else {
                $goods_storage = $goods['goods_number'];
            }
            /* 2014-12-23 因为取消库存,这里购物的时候,商品的库存要先跟购物车那里进行过比较,如果库存小于购物车的数量,就执行else,输出库存数量不足
               if ($GLOBALS['_CFG']['use_storage'] == 0 || $num <= $goods_storage)
               */
            if ($GLOBALS['_CFG']['use_storage'] == 1) {
                $goods_price = get_final_price($goods_id, $num, true, $spec);
                $sql = "UPDATE " . $GLOBALS['ecs']->table('cart') . " SET goods_number = '{$num}'" . " , goods_price = '{$goods_price}'" . " WHERE session_id = '" . SESS_ID . "' AND goods_id = '{$goods_id}' " . " AND parent_id = 0 AND goods_attr = '" . get_goods_attr_info($spec) . "' " . " AND extension_code <> 'package_buy' " . "AND rec_type = 'CART_GENERAL_GOODS'";
                $GLOBALS['db']->query($sql);
            } else {
                $GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['shortage'], $num), ERR_OUT_OF_STOCK);
                return false;
            }
        } else {
            $goods_price = get_final_price($goods_id, $num, true, $spec);
            $parent['goods_price'] = max($goods_price, 0);
            $parent['goods_number'] = $num;
            $parent['parent_id'] = 0;
            $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('cart'), $parent, 'INSERT');
        }
    }
    /* 把赠品删除 */
    $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' AND is_gift <> 0";
    $GLOBALS['db']->query($sql);
    return true;
}
Ejemplo n.º 15
0
/**
 * 获取商品的原价、配件价、库存(配件组合) by mike
 * 返回数组
 */
function get_combo_goods_info($goods_id, $num = 1, $spec = array(), $parent = 0)
{
    $result = array();
    /* 取得商品信息 */
    $sql = "SELECT goods_number FROM " . $GLOBALS['ecs']->table('goods') . " WHERE goods_id = '{$goods_id}' AND is_delete = 0";
    $goods = $GLOBALS['db']->getRow($sql);
    /* 如果商品有规格则取规格商品信息 配件除外 */
    $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('products') . " WHERE goods_id = '{$goods_id}' LIMIT 0, 1";
    $prod = $GLOBALS['db']->getRow($sql);
    if (is_spec($spec) && !empty($prod)) {
        $product_info = get_products_info($goods_id, $spec);
    }
    if (empty($product_info)) {
        $product_info = array('product_number' => '', 'product_id' => 0);
    }
    //商品库存
    $result['stock'] = $goods['goods_number'];
    //商品存在规格 是货品 检查该货品库存
    if (is_spec($spec) && !empty($prod)) {
        if (!empty($spec)) {
            /* 取规格的货品库存 */
            $result['stock'] = $product_info['product_number'];
        }
    }
    /* 如果该配件在添加为基本件的配件时,所设置的“配件价格”比原价低,即此配件在价格上提供了优惠, */
    $sql = "SELECT parent_id, goods_price " . "FROM " . $GLOBALS['ecs']->table('group_goods') . " WHERE goods_id = '{$goods_id}'" . " AND parent_id = '{$parent}'" . " ORDER BY goods_price";
    $res = $GLOBALS['db']->query($sql);
    while ($row = $GLOBALS['db']->fetchRow($res)) {
        $result['fittings_price'] = $row['goods_price'];
    }
    /* 计算商品的促销价格 */
    $result['fittings_price'] = isset($result['fittings_price']) ? $result['fittings_price'] : get_final_price($goods_id, $num, true, $spec);
    $result['spec_price'] = spec_price($spec);
    //属性价格
    $result['goods_price'] = get_final_price($goods_id, $num, true, $spec);
    return $result;
}
Ejemplo n.º 16
0
/**
 *  将指定订单中的商品添加到购物车
 *
 * @access  public
 * @param   int         $order_id
 *
 * @return  mix         $message        成功返回true, 错误返回出错信息
 */
function return_to_cart($order_id)
{
    /* 初始化基本件数量 goods_id => goods_number */
    $basic_number = array();
    /* 查订单商品:不考虑赠品 */
    $sql = "SELECT goods_id, product_id,goods_number, goods_attr, parent_id, goods_attr_id" . " FROM " . $GLOBALS['ecs']->table('order_goods') . " WHERE order_id = '{$order_id}' AND is_gift = 0 AND extension_code <> 'package_buy'" . " ORDER BY parent_id ASC";
    $res = $GLOBALS['db']->query($sql);
    $time = gmtime();
    while ($row = $GLOBALS['db']->fetchRow($res)) {
        // 查该商品信息:是否删除、是否上架
        $sql = "SELECT goods_sn, goods_name, goods_number, market_price, " . "IF(is_promote = 1 AND '{$time}' BETWEEN promote_start_date AND promote_end_date, promote_price, shop_price) AS goods_price," . "is_real, extension_code, is_alone_sale, goods_type " . "FROM " . $GLOBALS['ecs']->table('goods') . " WHERE goods_id = '{$row['goods_id']}' " . " AND is_delete = 0 LIMIT 1";
        $goods = $GLOBALS['db']->getRow($sql);
        // 如果该商品不存在,处理下一个商品
        if (empty($goods)) {
            continue;
        }
        if ($row['product_id']) {
            $order_goods_product_id = $row['product_id'];
            $sql = "SELECT product_number from " . $GLOBALS['ecs']->table('products') . "where product_id='{$order_goods_product_id}'";
            $product_number = $GLOBALS['db']->getOne($sql);
        }
        // 如果使用库存,且库存不足,修改数量
        if (C('use_storage') == 1 && ($row['product_id'] ? $product_number < $row['goods_number'] : $goods['goods_number'] < $row['goods_number'])) {
            if ($goods['goods_number'] == 0 || $product_number === 0) {
                // 如果库存为0,处理下一个商品
                continue;
            } else {
                if ($row['product_id']) {
                    $row['goods_number'] = $product_number;
                } else {
                    // 库存不为0,修改数量
                    $row['goods_number'] = $goods['goods_number'];
                }
            }
        }
        //检查商品价格是否有会员价格
        $sql = "SELECT goods_number FROM" . $GLOBALS['ecs']->table('cart') . " " . "WHERE session_id = '" . SESS_ID . "' " . "AND goods_id = '" . $row['goods_id'] . "' " . "AND rec_type = '" . CART_GENERAL_GOODS . "' LIMIT 1";
        $temp_number = $GLOBALS['db']->getOne($sql);
        $row['goods_number'] += $temp_number;
        $attr_array = empty($row['goods_attr_id']) ? array() : explode(',', $row['goods_attr_id']);
        $goods['goods_price'] = get_final_price($row['goods_id'], $row['goods_number'], true, $attr_array);
        // 要返回购物车的商品
        $return_goods = array('goods_id' => $row['goods_id'], 'goods_sn' => addslashes($goods['goods_sn']), 'goods_name' => addslashes($goods['goods_name']), 'market_price' => $goods['market_price'], 'goods_price' => $goods['goods_price'], 'goods_number' => $row['goods_number'], 'goods_attr' => empty($row['goods_attr']) ? '' : addslashes($row['goods_attr']), 'goods_attr_id' => empty($row['goods_attr_id']) ? '' : addslashes($row['goods_attr_id']), 'is_real' => $goods['is_real'], 'extension_code' => addslashes($goods['extension_code']), 'parent_id' => '0', 'is_gift' => '0', 'rec_type' => CART_GENERAL_GOODS);
        // 如果是配件
        if ($row['parent_id'] > 0) {
            // 查询基本件信息:是否删除、是否上架、能否作为普通商品销售
            $sql = "SELECT goods_id " . "FROM " . $GLOBALS['ecs']->table('goods') . " WHERE goods_id = '{$row['parent_id']}' " . " AND is_delete = 0 AND is_on_sale = 1 AND is_alone_sale = 1 LIMIT 1";
            $parent = $GLOBALS['db']->getRow($sql);
            if ($parent) {
                // 如果基本件存在,查询组合关系是否存在
                $sql = "SELECT goods_price " . "FROM " . $GLOBALS['ecs']->table('group_goods') . " WHERE parent_id = '{$row['parent_id']}' " . " AND goods_id = '{$row['goods_id']}' LIMIT 1";
                $fitting_price = $GLOBALS['db']->getOne($sql);
                if ($fitting_price) {
                    // 如果组合关系存在,取配件价格,取基本件数量,改parent_id
                    $return_goods['parent_id'] = $row['parent_id'];
                    $return_goods['goods_price'] = $fitting_price;
                    $return_goods['goods_number'] = $basic_number[$row['parent_id']];
                }
            }
        } else {
            // 保存基本件数量
            $basic_number[$row['goods_id']] = $row['goods_number'];
        }
        // 返回购物车:看有没有相同商品
        $sql = "SELECT goods_id " . "FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' " . " AND goods_id = '{$return_goods['goods_id']}' " . " AND goods_attr = '{$return_goods['goods_attr']}' " . " AND parent_id = '{$return_goods['parent_id']}' " . " AND is_gift = 0 " . " AND rec_type = '" . CART_GENERAL_GOODS . "'";
        $cart_goods = $GLOBALS['db']->getOne($sql);
        if (empty($cart_goods)) {
            // 没有相同商品,插入
            $return_goods['session_id'] = SESS_ID;
            $return_goods['user_id'] = $_SESSION['user_id'];
            $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('cart'), $return_goods, 'INSERT');
        } else {
            // 有相同商品,修改数量
            $sql = "UPDATE " . $GLOBALS['ecs']->table('cart') . " SET " . "goods_number = '" . $return_goods['goods_number'] . "' " . ",goods_price = '" . $return_goods['goods_price'] . "' " . "WHERE session_id = '" . SESS_ID . "' " . "AND goods_id = '" . $return_goods['goods_id'] . "' " . "AND rec_type = '" . CART_GENERAL_GOODS . "' LIMIT 1";
            $GLOBALS['db']->query($sql);
        }
    }
    // 清空购物车的赠品
    $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" . SESS_ID . "' AND is_gift = 1";
    $GLOBALS['db']->query($sql);
    return true;
}
Ejemplo n.º 17
0
/**
 * 更新购物车中的商品数量
 *
 * @access  public
 * @param   array   $arr
 * @return  void
 */
function flow_update_cart($arr)
{
    foreach ($arr as $key => $val) {
        $val = intval(make_semiangle($val));
        if ($val <= 0) {
            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);
        /* 系统启用了库存,检查输入的商品数量是否有效 */
        if (intval($GLOBALS['_CFG']['use_storage']) > 0 && $goods['extension_code'] != 'package_buy') {
            $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 ($row['goods_number'] < $val) {
                show_message(sprintf($GLOBALS['_LANG']['stock_insufficiency'], $row['goods_name'], $row['goods_number'], $row['goods_number']));
                exit;
            }
        }
        /* 检查该项是否为基本件以及有没有配件存在 */
        $sql = "SELECT a.goods_number, a.rec_id FROM " . $GLOBALS['ecs']->table('cart') . " AS b " . "LEFT JOIN " . $GLOBALS['ecs']->table('cart') . " AS a " . "ON a.parent_id = b.goods_id AND a.session_id = '" . SESS_ID . "' AND a.extension_code <> 'package_buy'" . "WHERE b.rec_id = '{$key}'";
        $fittings = $GLOBALS['db']->getAll($sql);
        if ($val > 0) {
            foreach ($fittings as $k => $v) {
                if ($v['goods_number'] != null && $v['rec_id'] != null) {
                    /* 该商品有配件,更新配件的商品数量 */
                    $num = $v['goods_number'] > $val ? $val : $v['goods_number'];
                    $sql = "UPDATE " . $GLOBALS['ecs']->table('cart') . " SET goods_number = '{$num}' WHERE rec_id = {$v['rec_id']}";
                    $GLOBALS['db']->query($sql);
                }
            }
            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 . "'";
            }
        } else {
            if (is_object($fittings) && $fittings->goods_number != null && $fittings->rec_id != null) {
                $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') . " WHERE rec_id={$fittings['rec_id']}";
                $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);
}