<?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()); */