凌的博客

您现在的位置是:首页  > 学无止境 > PHP

PHP

一个购物车Cart类

2015-05-05PHP0
/** 购物车 */ class Cart{ /** @access private $data */ private $data; private $user_id; private $user_key; private $CartModel; /** 初始化 */ public function __construct($user_id){ $th
/**
购物车
*/
class Cart{
	/**
	@access private $data
	*/
	private $data;
	private $user_id;
	private $user_key;
	private $CartModel;
	
	/**
	初始化
	*/
	public function __construct($user_id){
		$this->CartModel = D('Admin/Cart');
		$this->user_id     = $user_id;
		//使用Cookie存储用户标识
		if(Cookie('user_key') ==''){
			$user_key = session_id();
			Cookie('user_key',session_id());
		}else{
			$user_key = Cookie('user_key');
		}
		$this->user_key = $user_key;
		
		$this->data = $this->getCart();
		//自动清空30天之前的购物车
		$this->autoClear();
	}
	
	/**
	获取商品详细信息
	@param int $goods_id
	*/
	private function getGoods($goods_id){
		return D('Admin/Goods')->field(true)->where(array( 'is_delete' => 0, 'goods_id'=> (int)$goods_id))->find();
	}
	
	/**
	添加一个商品
	@param int $goods_id 商品id
	@param int $quantity 商品数量
	*/
	public function add($goods_id,$quantity=1){
		$goods = $this->getGoods($goods_id);
		//检查购物车里有没有该商品
		if(($key = $this->getCartGoodsKey($goods_id)) !== false){
			//总数量
			$quantity = $this->data['cart'][$key]['quantity']+$quantity;
			$this->data['cart'][$key] = array(
				'goods_id' => $goods_id,
				'quantity' => $quantity,
				'name'     => $goods['name'],
				'price'    => $goods['price'],
			); 
		}else{
			$this->data['cart'][] = array(
				'goods_id' => $goods_id,
				'quantity' => $quantity,
				'name'     => $goods['name'],
				'price'    => $goods['price'],
			); 
		}

		return $this->save();
	}
	
	/**
	删除一个商品
	@param int $goods_id 商品id
	*/
	public function del($goods_id){
		if(($key = $this->getCartGoodsKey($goods_id)) !== false){
			unset($this->data['cart'][$key]);
			sort($this->data['cart']);
			$this->save();
		}else{
			return true;	
		}
	}
	
	/**
	清空购物车
	*/
	public function clear(){
		$this->data = array();
		$this->save();
		return true;	
	}
	
	/**
	更新购物车
	@param int $goods_id 商品id
	@param int $quantity 商品数量
	*/
	public function update($goods_id,$quantity){
		if(($key = $this->getCartGoodsKey($goods_id)) !== false){
			$this->data['cart'][$key]['quantity'] = $quantity;
			$this->save();
		}else{
			//没有则添加
			$this->add($goods_id,$quantity);	
		}
	}
	
	/**
	获取购物车商品
	*/
	public function getCart(){
		if($this->user_id >0){
			//已登录
			$ret = $this->CartModel->where(array('is_delete'=>0,'user_id'=>$this->user_id))->getField('data');
		}else{
			//未登录
			$ret = $this->CartModel->where(array('is_delete'=>0,'user_key'=>$this->user_key))->getField('data');
		}
		return unserialize($ret);
	}
	
	
	/**
	获取购物车中商品的键值
	*/
	private function getCartGoodsKey($goods_id){
		if(!empty($this->data['cart'])){
			foreach($this->data['cart'] as $key=>$value){
				if($value['goods_id'] == $goods_id){
					return $key;
				}	
			}	
		}
		return false;
	}
	
	
	
	/**
	统计购物车信息
	*/
	public function countData(){
		$cart = $this->data['cart'];

		$count = array(
			'total_price' => 0.00,
			'quantity'    => 0,
			'total'       => 0,
			
		);
		if(!empty($cart)){
			$count['total']   = count($cart);
			foreach($cart as $key=>$value){
				$count['total_price'] += $value['quantity'] * $value['price'];
				$count['quantity']    += $value['quantity'];
			}
		}
		$count['total_price'] = sprintf('%.2f',$count['total_price']);
		$this->data['count'] = $count;
		return $this->data;
	}
	
	/**
	保存购物车到数据库
	*/
	public function save(){
		$now = time();
				
		$data = array(
			'user_id'      => $this->user_id,
			'user_key'     => $this->user_key,
			'updatetime'   => $now,
			'data'         => serialize($this->countData()),
		);
	
		if($this->user_id >0){
			//已登录
			$ret = $this->CartModel->field('id')->where(array('is_delete'=>0, 'user_id'=>$this->user_id))->find();
			if($ret['id'] >0){
				$this->CartModel->where(array('user_id'=>$this->user_id))->save($data);
			}else{
				$data['addtime'] = $now;
				$this->CartModel->add($data);
			}
				
		}else{
			//未登录
			$ret = $this->CartModel->field('id')->where(array('is_delete'=>0, 'user_key'=> $this->user_key))->find();
			if(!empty($ret['id'])){
				$this->CartModel->where(array('user_key'=>$this->user_key))->save($data);
			}else{
				$data['addtime'] = $now;
				$this->CartModel->add($data);
			}
		}
		return true;
	}
	
	/**
	自动清空 30天之前的购物车
	*/
	public function autoClear(){
		$this->CartModel->where(array('updatetime'=> array('lt', time()-86400*30 ) ))->find();
	}
	
	
	/**
	记录日志
	*/
	public function _log(){
		
		
	}
	
}

如果本文对您有所帮助,请支持下本站哦!!!^_^

文章评论

0条评论