<转>php购物车类
最近有个项目中涉及到了商品购物,很显然,购物车必须得要有,记得很久以前用php做过购物车的,可惜没保存,这次不能犯以前的错误了,经过各种研究,本人也总结了一个php实现购物车的一个类。实现了购物车中的商品的添加,修改,删除,列表,以及各种计算的相关功能。采用了php单一类的原理,安全高效,简单易扩展。
001 |
/******************************* |
002 |
* author:www.phpernote.com |
003 |
* date:2013 年 04 月 11 日 |
004 |
*******************************/ |
005 |
class Cart{ |
006 |
static protected $ins; //实例变量 |
007 |
public $item=array(); //放商品容器 |
008 |
//禁止外部调用 |
009 |
final protected function __construct(){} |
010 |
//禁止克隆 |
011 |
final protected function __clone(){} |
012 |
//类内部实例化 |
013 |
static protected function getIns(){ |
014 |
if(!(self::$ins instanceof self)){self::$ins=new self();}return self::$ins; |
015 |
} |
016 |
//为了能使商品跨页面保存,把对象放入session里,这里为了防止冲突,设置了一个session后缀参数 |
017 |
public static function getCat($sesSuffix='phpernote'){ |
018 |
if(!isset($_SESSION['cat'.$sesSuffix])||!($_SESSION['cat'.$sesSuffix] instanceof self)){ |
019 |
$_SESSION['cat'.$sesSuffix]=self::getIns(); |
020 |
} |
021 |
return $_SESSION['cat'.$sesSuffix]; |
022 |
} |
023 |
//入列时的检验,是否在$item里存在 |
024 |
public function inItem($goods_id){ |
025 |
if($this->getTypes()==0){ |
026 |
return false; |
027 |
} |
028 |
//这里检验商品是否相同是通过goods_id来检测,并未通过商品名称name来检测,具体情况可做修改 |
029 |
if(!(array_key_exists($goods_id,$this->item))){ |
030 |
return false; |
031 |
}else{ |
032 |
return $this->item[$goods_id]['num']; //返回此商品个数 |
033 |
} |
034 |
} |
035 |
//添加一个商品 |
036 |
/* |
037 |
* goods_id 唯一id |
038 |
* name 名称 |
039 |
* num 数量 |
040 |
* price 单价 |
041 |
*/ |
042 |
public function addItem($goods_id,$name,$num,$price){ |
043 |
if($this->inItem($goods_id)!=false){ |
044 |
$this->item[$goods_id]['num']+=$num; |
045 |
return; |
046 |
} |
047 |
$this->item[$goods_id]=array(); //一个商品为一个数组 |
048 |
$this->item[$goods_id]['num']=$num; //这一个商品的购买数量 |
049 |
$this->item[$goods_id]['name']=$name; //商品名字 |
050 |
$this->item[$goods_id]['price']=floatval($price); //商品单价 |
051 |
} |
052 |
//减少一个商品 |
053 |
public function reduceItem($goods_id,$num){ |
054 |
if($this->inItem($goods_id)==false){ |
055 |
return; |
056 |
} |
057 |
if($num>$this->getNum($goods_id)){ |
058 |
unset($this->item[$goods_id]); |
059 |
}else{ |
060 |
$this->item[$goods_id]['num']-=$num; |
061 |
} |
062 |
} |
063 |
//去掉一个商品 |
064 |
public function delItem($goods_id){ |
065 |
if($this->inItem($goods_id)){ |
066 |
unset($this->item[$goods_id]); |
067 |
} |
068 |
} |
069 |
//返回购买商品列表 |
070 |
public function itemList(){ |
071 |
return $this->item; |
072 |
} |
073 |
//一共有多少种商品 |
074 |
public function getTypes(){ |
075 |
return count($this->item); |
076 |
} |
077 |
//获得一种商品的总个数 |
078 |
public function getNum($goods_id){ |
079 |
return $this->item[$goods_id]['num']; |
080 |
} |
081 |
// 查询购物车中有多少个商品 |
082 |
public function getNumber(){ |
083 |
$num=0; |
084 |
if($this->getTypes()==0){ |
085 |
return 0; |
086 |
} |
087 |
foreach($this->item as $k=>$v){ |
088 |
$num+=$v['num']; |
089 |
} |
090 |
return $num; |
091 |
} |
092 |
//计算总价格 |
093 |
public function getPrice(){ |
094 |
$price=0; |
095 |
if($this->getTypes()==0){ |
096 |
return 0; |
097 |
} |
098 |
foreach($this->item as $k=>$v){ |
099 |
$price+=floatval($v['num']*$v['price']); |
100 |
} |
101 |
return $price; |
102 |
} |
103 |
//清空购物车 |
104 |
public function emptyItem(){ |
105 |
$this->item=array(); |
106 |
} |
107 |
} |
以上购物车类的调用示例如下:
01 |
<?php |
02 |
header("Content-type:text/html;charset=utf-8"); |
03 |
session_start(); |
04 |
$cart = Cart::getCat('_test'); |
05 |
//cart经过一次实例化之后,任意页面都可以通过$_SESSION['cat_test']调用cart类的相关方法 |
06 |
$_SESSION['cat_test']->addItem('1','苹果','1','8.03'); |
07 |
$cart->addItem('2','香蕉','3','6.5'); |
08 |
echo '<pre>'; |
09 |
print_r($_SESSION); |
10 |
echo '获取购物车商品名称:'.$_SESSION['cat_test']->item[1]['name'],';',$_SESSION['cat_test']->item[2]['name']; |
11 |
echo '<br />'; |
12 |
echo '购物车中共有商品总数:',$cart->getNumber(); |
13 |
echo '<br />'; |
14 |
echo '购物车中商品总价:',$_SESSION['cat_test']->getPrice(); |
15 |
//session_destroy(); |
16 |
//$_SESSION['cat_test']->emptyItem(); |
实际效果图:

posted on 2015-10-12 23:20 hahahahahai12 阅读(117) 评论(0) 收藏 举报
浙公网安备 33010602011771号