您的位置:首页 > 其它

放入购物车

2015-09-16 19:27 260 查看
放入购物车

<?php

define('ACC',true);
require('./include/init.php');

// 设置一个动作参数,判断用户想干什么,比如是下订单/写地址/提交/清空购物车等
$act = isset($_GET['act'])?$_GET['act']:'buy';

$cart = CartTool::getCart(); // 获取购物车实例
$goods = new GoodsModel();

if($act == 'buy') { // 这是把商品加到购物车
$goods_id = isset($_GET['goods_id'])?$_GET['goods_id']+0:0;
$num = isset($_GET['num'])?$_GET['num']+0:1;

if($goods_id) { // $goods_id为真,是想把商品放到购物车里
$g = $goods->find($goods_id);
if(!empty($g)) { // 有此商品

// 需要判断此商品,是否在回收站
// 此商品是否已下架
if($g['is_delete'] == 1 || $g['is_on_sale'] == 0) {
$msg = '此商品不能购买';
include(ROOT . 'view/front/msg.html');
exit;
}

// 先把商品加到购物车
$cart->addItem($goods_id,$g['goods_name'],$g['shop_price'],$num);

// 判断库存够不够
$items = $cart->all();

if($items[$goods_id]['num'] > $g['goods_number']) {
// 库存不够了,把刚才加到购物车的动作撤回!
$cart->decNum($goods_id,$num);

$msg = '库存不足';
include(ROOT . 'view/front/msg.html');
exit;
}

}

//print_r($cart->all());
}

$items = $cart->all();

if(empty($items)) { // 如果购物车为空,返回首页
header('location: index.php');
exit;
}

// 把购物车里的商品详细信息取出来
$items = $goods->getCartGoods($items);

//print_r($items);exit;

$total = $cart->getPrice(); //获取购物车中的商品总价格
$market_total = 0.0;
foreach($items as $v) {
$market_total += $v['market_price'] * $v['num'];
}

$discount = $market_total - $total;
$rate = round(100 * $discount/$total,2);

include(ROOT . 'view/front/jiesuan.html');
} else if($act == 'clear') {
$cart->clear();
$msg = '购物车已清空';
include(ROOT . 'view/front/msg.html');
} else if($act == 'tijiao') {

$items = $cart->all(); // 取出购物车中的商品

// 把购物车里的商品详细信息取出来
$items = $goods->getCartGoods($items);

//print_r($items);exit;

$total = $cart->getPrice(); //获取购物车中的商品总价格
$market_total = 0.0;
foreach($items as $v) {
$market_total += $v['market_price'] * $v['num'];
}

$discount = $market_total - $total;
$rate = round(100 * $discount/$total,2);

include(ROOT . 'view/front/tijiao.html');
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: