如何用PHP寫一個購物車類呢,下面我們來簡單分析學(xué)習(xí)下。
代碼
namespace app\common\helper; use app\common\model\Cart; use app\common\model\GoodsMain; class CartHelper { protected $cart; protected $cartList; public function __construct() { $this->cart = new Cart(); $this->cartList = array(); } // 添加到購物車 public function push($good_main_id, $good_num) { if (!session("?user")) { $this->cartList = session('cartList'); if (isset($this->cartList[$good_main_id])) { $this->cartList[$good_main_id] = $this->cartList[$good_main_id] + $good_num; } else { $this->cartList[$good_main_id] = $good_num; } session('cartList', $this->cartList); } else { $user = session('user'); $cartList = $user->cart()->where('goods_main_id', '=', $good_main_id)->find(); if ($cartList) { $cartList->goods_num = $cartList->goods_num + $good_num; $cartList->save(); } else { $user->cart()->save(["goods_main_id"=>$good_main_id, "goods_num"=>$good_num]); } } } // 刪除購物車 public function del($good_main_id) { if (!is_array($good_main_id)) { $good_main_id[] = $good_main_id; } if (!session("?user")) { // 未登錄用戶 刪除 session $this->cartList = session('cartList'); foreach ($good_main_id as $id) { if (isset($this->cartList[$id])) { unset($this->cartList[$id]); } } session('cartList', $this->cartList); } else { // 登錄用戶 從數(shù)據(jù)庫中刪除購物車 Cart::where([ ["goods_main_id","in", $good_main_id], ["user_id", "=", session("user.id")], ])->delete(); } } // 清空購物車 public function clear() { if (!session("?user")) { session('cartList', null); } else { Cart::where([ ["user_id", "=", session("user.id")] ])->delete(); } } // 購物車清單 public function list() { if (session('?user')) { $user = session('user'); $cartlist = $user->cart()->select(); $list = array(); if (!$cartlist->isEmpty()) { foreach ($cartlist as $k=>$v) { $good = GoodsMain::find($v->goods_main_id); if ($good) { $temp["good"] = $good; $temp["goods_num"] = $v->goods_num; $list[] = $temp; } } } } else { $cartlist = session('cartList'); $list = array(); if (!empty($cartlist)) { foreach ($cartlist as $k=>$v) { $good = GoodsMain::find($k); if ($good) { $temp["good"] = $good; $temp["goods_num"] = $v; $list[] = $temp; } } } } return $list; } // 合并購物車 登錄前和登錄后 public function merge() { if (!session('?user') || !session('?cartList')) { return false; } $user = session('user'); $cartList = session('cartList'); $updateArr = array(); foreach ($cartList as $k=>$v) { $cart = $user->cart()->where("goods_main_id","=",$k)->find(); if ($cart) { $updateArr[] = [ 'id' => $cart->id, 'goods_main_id' => $k, 'goods_num' => $cart->goods_num + $v ]; } else { $updateArr[] = [ 'goods_main_id' => $k, 'goods_num' => $v ]; } } if (!empty($updateArr)) { $user->cart()->saveAll($updateArr); } session('cartList', null); } }
分析
添加購物車有兩種一情況,一種是用戶已經(jīng)登錄,另一種是沒有登錄。登錄的可以直接寫入數(shù)據(jù)庫,沒有登錄的可以用session保存。為了兼容兩種情況,所有操作都判斷了用戶是否登錄。還要考慮用戶原本購物車?yán)镆呀?jīng)有產(chǎn)品,在沒有登錄前購物車?yán)镆灿挟a(chǎn)品,在用戶登錄時需要進(jìn)行合并操作,上面有代碼可以參考。
保存到session的數(shù)據(jù)格式和用thinkphp6從數(shù)據(jù)庫里取出的數(shù)據(jù)格式是不一樣的,為了方便類的調(diào)用,也做了相應(yīng)的處理。
購物車?yán)镏槐4媪水a(chǎn)品的ID和數(shù)量。
雖然很簡陋,但這也是我能想出來的最好的了,一起加沒。