<?php defined("Hello")||exit("Hello deny!"); /* 购物车类 session 加单例模式 功能分析 添加商品 删除商品 修改商品数量 检查商品是否存在 某商品数量加一 某商品数量减一 查询购物车的商品种类 查询购物车商品数量 是 查询总金额 返回所有商品 添加订单 修改商品库存 清空购物车 */ //defined("Hello")||exit("Hello deny!"); class CartTool{ private static $ins = null; private $items = array(); protected function __construct(){ } //获取实例 private static function getIns(){ if(!(self::$ins instanceof self)){ self::$ins = new self(); return self::$ins; } } //把购物车中的单例对象放到session当中 public static function getCart(){ //先判断购物车当中是否存在该单例 否走创建单例并且存放在购物车当中 并且返回 否则直接返回购物车 if(!isset($_SESSION['cart'])||!($_SESSION['cart'] instanceof self)){ $_SESSION['cart'] = self::getIns(); return $_SESSION['cart']; }else{ return $_SESSION['cart']; } } /* 商品id 、 商品名称 、 商品价格 、 商品数量 */ public function addItem($id,$goods_name,$shop_price,$goods_num = 1,$goods_img){ //判断该商品是否存在 if($this->hasItem($id)){ $this->modNum($id,$goods_num); return true; }else{ //为什么这里不是直接赋值? $this->items[$id] = array( "id"=>$id, "goods_name"=>$goods_name, "shop_price"=>$shop_price, //这里可以不需要写 在goodsModel当中取出 购物车当中的商品的详细信息 "goods_num"=>$goods_num, "addtime"=>time(), "goods_img"=>$goods_img ); return true; } } /*修改购物车 商品id 商品修改后的数量 直接修改*/ public function modNum($id,$num){ //先判断购物车当中是否存在该商品 否则修改商品数量 if(!$this->hasItem($id)){ return false; }else{ //$this->items[$goods_id]['goods_num'] = $num; $this->increaNum($id,$num); } } /*通过点击按钮修改商品数量*/ public function increaNum($id,$num){ if($this->hasItem($id)){ $this->items[$id]['goods_num'] += $num; } } public function decreaNum($id,$num){ //先判断商品是否存在再进行操作 if($this->hasItem($id)){ $this->items[$id]['goods_num'] -= $num; } //如果商品数量为0则删除该商品 if($this->items[$id]['goods_num'] <= 0){ $this->delItem($id); } } /*返回商品种类 即购物车中item的key*/ public function countItem(){ return array_keys($this->items); } /*总的商品个数*/ public function getSum(){ $sum = 0; foreach ($this->items as $value) { $sum+=$value['goods_num']; } return $sum; } /*获取商品的总价格*/ public function getPrice(){ if($this->countItem() == 0){ return 0; } $sum = 0.0; foreach ($this->items as $value) { $sum+=$value['goods_num']*$value['shop_price']; } return $sum; } /*删除商品*/ public function delItem($id){ unset($this->items[$id]); } /*清空购物车*/ public function clear(){ $this->items = array(); } public function getAllItem(){ return $this->items; } /*判断商品是否存在*/ public function hasItem($id){ if($this->countItem() == 0){ return 0; } return array_key_exists($id, $this->items); } /** * 获取购物车当中最新添加的几条数据 * @return [type] [description] */ public function getPre($number){ $num = $this->getAllItem(); if(!empty($num)){ if(count($num)>=$number){ $vo = array(); foreach ($num as $key => $value) { $vo[] = $value['addtime']; } /** * 将数据根据 volume 降序排列,根据 edition 升序排列 把 $data 作为最后一个参数,以通用键排序 array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data); */ $sor = $num; array_multisort($vo,SORT_DESC,$sor); return array_slice($sor,0,$number); }else{ return $num; } }else{ return false; } } } //测试代码 /* $car = CartTool::getCart(); $car->addItem(1,"1",12,1); $car->addItem(2,"1",12,1); $car->addItem(2,"1",12,2); $car->clear(); var_dump($car); ///$car->clear(); echo "<hr/>"; var_dump($car); $car->increaNum(2); $car->clear(); var_dump($car); var_dump($car->getAllItem()); */ //测试代码 /* $car = CartTool::getCart(); $car->addItem(1,"1",12,1); $car->addItem(2,"1",12,1); $car->addItem(2,"1",12,2); $car->clear(); var_dump($car); ///$car->clear(); echo "<hr/>"; var_dump($car); $car->increaNum(2); $car->clear(); var_dump($car); var_dump($car->getAllItem()); */