放入购物车
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'); }
相关文章推荐
- LeetCode(73)Set Matrix Zeroes
- Android开发环境的演变
- asp.net将sql语句封装在类库中
- ios学习之 关于Certificate、Provisioning Profile、App ID的介绍及其之间的关系
- 自定义View
- JDK源码阅读之Cloneable接口
- HTML&CSS Learning Notes 4
- leetcode-31 Next Permutation 数字排列组合找到下一个更大值
- 从今天起,正式开始记录学习编程的点点滴滴
- C++入门第一步:一些格式上的注意
- c++简单用户界面
- 如何用代码控制NGUI中Sprite的图片?
- OGNL表达式
- leetcode-29 Divide Two Integers
- Palette状态栏颜色提取,写的不错就分享了
- Palette状态栏颜色提取,写的不错就分享了
- My New Blog
- Palette状态栏颜色提取,写的不错就分享了
- 大数据笔记06:大数据之Hadoop的HDFS(文件的读写操作)
- git