PHP实现的购物车类实例

这篇文章主要介绍了PHP实现的购物车类,可实现购物车基本的加入、删除、统计等相关功能,需要的朋友可以参考下

本文实例讲述了PHP实现的购物车类。分享给大家供大家参考。具体分析如下:

该购物车类是基于CodeIgniter的购物车类仿写实现的。

购物车基本功能如下:

1) 将物品加入购物车

2) 从购物车中删除物品

3) 更新购物车物品信息 【+1/-1】

4) 对购物车物品进行统计

1. 总项目

2. 总数量

3. 总金额

5) 对购物单项物品的数量及金额进行统计

6) 清空购物车

1. cart.php文件:

  1. <?php
  2. /**
  3. *
  4. * @author quanshuidingdang
  5. */
  6. class Cart {
  7. //物品id及名称规则,调试信息控制
  8. private $product_id_rule = '\.a-z0-9-_'; //小写字母 | 数字 | ._-
  9. private $product_name_rule = '\.\:a-z0-9-_';//小写字母 | 数字 | ._-:
  10. private $debug = TRUE;
  11. //购物车
  12. private $_cart_contents = array();
  13. /**
  14. * 构造函数
  15. *
  16. * @param array
  17. */
  18. public function __construct() {
  19. //是否第一次使用?
  20. if(isset($_SESSION['cart_contents'])) {
  21. $this->_cart_contents = $_SESSION['cart_contents'];
  22. } else {
  23. $this->_cart_contents['cart_total'] = 0;
  24. $this->_cart_contents['total_items'] = 0;
  25. }
  26. if($this->debug === TRUE) {
  27. //$this->_log("cart_create_success");
  28. }
  29. }
  30. /**
  31. * 将物品加入购物车
  32. *
  33. * @access public
  34. * @param array 一维或多维数组,必须包含键值名:
  35. id -> 物品ID标识,
  36. qty -> 数量(quantity),
  37. price -> 单价(price),
  38. name -> 物品姓名
  39. * @return bool
  40. */
  41. public function insert($items = array()) {
  42. //输入物品参数异常
  43. if( ! is_array($items) OR count($items) == 0) {
  44. if($this->debug === TRUE) {
  45. $this->_log("cart_no_items_insert");
  46. }
  47. return FALSE;
  48. }
  49. //物品参数处理
  50. $save_cart = FALSE;
  51. if(isset($items['id'])) {
  52. if($this->_insert($items) === TRUE) {
  53. $save_cart = TRUE;
  54. }
  55. } else {
  56. foreach($items as $val) {
  57. if(is_array($val) AND isset($val['id'])) {
  58. if($this->_insert($val) == TRUE) {
  59. $save_cart = TRUE;
  60. }
  61. }
  62. }
  63. }
  64. //当插入成功后保存数据到session
  65. if($save_cart) {
  66. $this->_save_cart();
  67. return TRUE;
  68. }
  69. return FALSE;
  70. }
  71. /**
  72. * 更新购物车物品信息
  73. *
  74. * @access public
  75. * @param array
  76. * @return bool
  77. */
  78. public function update($items = array()) {
  79. //输入物品参数异常
  80. if( !is_array($items) OR count($items) == 0) {
  81. if($this->debug === TRUE) {
  82. $this->_log("cart_no_items_insert");
  83. }
  84. return FALSE;
  85. }
  86. //物品参数处理
  87. $save_cart = FALSE;
  88. if(isset($items['rowid']) AND isset($items['qty'])) {
  89. if($this->_update($items) === TRUE) {
  90. $save_cart = TRUE;
  91. }
  92. } else {
  93. foreach($items as $val) {
  94. if(is_array($val) AND isset($val['rowid']) AND isset($val['qty'])) {
  95. if($this->_update($val) === TRUE) {
  96. $save_cart = TRUE;
  97. }
  98. }
  99. }
  100. }
  101. //当更新成功后保存数据到session
  102. if($save_cart) {
  103. $this->_save_cart();
  104. return TRUE;
  105. }
  106. return FALSE;
  107. }
  108. /**
  109. * 获取购物车物品总金额
  110. *
  111. * @return int
  112. */
  113. public function total() {
  114. return $this->_cart_contents['cart_total'];
  115. }
  116. /**
  117. * 获取购物车物品种类
  118. *
  119. * @return int
  120. */
  121. public function total_items() {
  122. return $this->_cart_contents['total_items'];
  123. }
  124. /**
  125. * 获取购物车
  126. *
  127. * @return array
  128. */
  129. public function contents() {
  130. return $this->_cart_contents;
  131. }
  132. /**
  133. * 获取购物车物品options
  134. *
  135. * @param string
  136. * @return array
  137. */
  138. public function options($rowid = '') {
  139. if($this->has_options($rowid)) {
  140. return $this->_cart_contents[$rowid]['options'];
  141. } else {
  142. return array();
  143. }
  144. }
  145. /**
  146. * 清空购物车
  147. *
  148. */
  149. public function destroy() {
  150. unset($this->_cart_contents);
  151. $this->_cart_contents['cart_total'] = 0;
  152. $this->_cart_contents['total_items'] = 0;
  153. unset($_SESSION['cart_contents']);
  154. }
  155. /**
  156. * 判断购物车物品是否有options选项
  157. *
  158. * @param string
  159. * @return bool
  160. */
  161. private function has_options($rowid = '') {
  162. if( ! isset($this->_cart_contents[$rowid]['options']) OR count($this->_cart_contents[$rowid]['options']) === 0) {
  163. return FALSE;
  164. }
  165. return TRUE;
  166. }
  167. /**
  168. * 插入数据
  169. *
  170. * @access private
  171. * @param array
  172. * @return bool
  173. */
  174. private function _insert($items = array()) {
  175. //输入物品参数异常
  176. if( ! is_array($items) OR count($items) == 0) {
  177. if($this->debug === TRUE) {
  178. $this->_log("cart_no_data_insert");
  179. }
  180. return FALSE;
  181. }
  182. //如果物品参数无效(无id/qty/price/name)
  183. if( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name'])) {
  184. if($this->debug === TRUE) {
  185. $this->_log("cart_items_data_invalid");
  186. }
  187. return FALSE;
  188. }
  189. //去除物品数量左零及非数字字符
  190. $items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty']));
  191. $items['qty'] = trim(preg_replace('/^([0]+)/i', '', $items['qty']));
  192. //如果物品数量为0,或非数字,则我们对购物车不做任何处理!
  193. if( ! is_numeric($items['qty']) OR $items['qty'] == 0) {
  194. if($this->debug === TRUE) {
  195. $this->_log("cart_items_data(qty)_invalid");
  196. }
  197. return FALSE;
  198. }
  199. //物品ID正则判断
  200. if( ! preg_match('/^['.$this->product_id_rule.']+$/i', $items['id'])) {
  201. if($this->debug === TRUE) {
  202. $this->_log("cart_items_data(id)_invalid");
  203. }
  204. return FALSE;
  205. }
  206. //物品名称正则判断
  207. if( ! preg_match('/^['.$this->product_name_rule.']+$/i', $items['name'])) {
  208. if($this->debug === TRUE) {
  209. $this->_log("cart_items_data(name)_invalid");
  210. }
  211. return FALSE;
  212. }
  213. //去除物品单价左零及非数字(带小数点)字符
  214. $items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price']));
  215. $items['price'] = trim(preg_replace('/^([0]+)/i', '', $items['price']));
  216. //如果物品单价非数字
  217. if( ! is_numeric($items['price'])) {
  218. if($this->debug === TRUE) {
  219. $this->_log("cart_items_data(price)_invalid");
  220. }
  221. return FALSE;
  222. }
  223. //生成物品的唯一id
  224. if(isset($items['options']) AND count($items['options']) >0) {
  225. $rowid = md5($items['id'].implode('', $items['options']));
  226. } else {
  227. $rowid = md5($items['id']);
  228. }
  229. //加入物品到购物车
  230. unset($this->_cart_contents[$rowid]);
  231. $this->_cart_contents[$rowid]['rowid'] = $rowid;
  232. foreach($items as $key => $val) {
  233. $this->_cart_contents[$rowid][$key] = $val;
  234. }
  235. return TRUE;
  236. }
  237. /**
  238. * 更新购物车物品信息(私有)
  239. *
  240. * @access private
  241. * @param array
  242. * @return bool
  243. */
  244. private function _update($items = array()) {
  245. //输入物品参数异常
  246. if( ! isset($items['rowid']) OR ! isset($items['qty']) OR ! isset($this->_cart_contents[$items['rowid']])) {
  247. if($this->debug == TRUE) {
  248. $this->_log("cart_items_data_invalid");
  249. }
  250. return FALSE;
  251. }
  252. //去除物品数量左零及非数字字符
  253. $items['qty'] = preg_replace('/([^0-9])/i', '', $items['qty']);
  254. $items['qty'] = preg_replace('/^([0]+)/i', '', $items['qty']);
  255. //如果物品数量非数字,对购物车不做任何处理!
  256. if( ! is_numeric($items['qty'])) {
  257. if($this->debug === TRUE) {
  258. $this->_log("cart_items_data(qty)_invalid");
  259. }
  260. return FALSE;
  261. }
  262. //如果购物车物品数量与需要更新的物品数量一致,则不需要更新
  263. if($this->_cart_contents[$items['rowid']]['qty'] == $items['qty']) {
  264. if($this->debug === TRUE) {
  265. $this->_log("cart_items_data(qty)_equal");
  266. }
  267. return FALSE;
  268. }
  269. //如果需要更新的物品数量等于0,表示不需要这件物品,从购物车种清除
  270. //否则修改购物车物品数量等于输入的物品数量
  271. if($items['qty'] == 0) {
  272. unset($this->_cart_contents[$items['rowid']]);
  273. } else {
  274. $this->_cart_contents[$items['rowid']]['qty'] = $items['qty'];
  275. }
  276. return TRUE;
  277. }
  278. /**
  279. * 保存购物车数据到session
  280. *
  281. * @access private
  282. * @return bool
  283. */
  284. private function _save_cart() {
  285. //首先清除购物车总物品种类及总金额
  286. unset($this->_cart_contents['total_items']);
  287. unset($this->_cart_contents['cart_total']);
  288. //然后遍历数组统计物品种类及总金额
  289. $total = 0;
  290. foreach($this->_cart_contents as $key => $val) {
  291. if( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty'])) {
  292. continue;
  293. }
  294. $total += ($val['price'] * $val['qty']);
  295. //每种物品的总金额
  296. $this->_cart_contents[$key]['subtotal'] = ($val['price'] * $val['qty']);
  297. }
  298. //设置购物车总物品种类及总金额
  299. $this->_cart_contents['total_items'] = count($this->_cart_contents);
  300. $this->_cart_contents['cart_total'] = $total;
  301. //如果购物车的元素个数少于等于2,说明购物车为空
  302. if(count($this->_cart_contents) <= 2) {
  303. unset($_SESSION['cart_contents']);
  304. return FALSE;
  305. }
  306. //保存购物车数据到session
  307. $_SESSION['cart_contents'] = $this->_cart_contents;
  308. return TRUE;
  309. }
  310. /**
  311. * 日志记录
  312. *
  313. * @access private
  314. * @param string
  315. * @return bool
  316. */
  317. private function _log($msg) {
  318. return @file_put_contents('cart_err.log', $msg, FILE_APPEND);
  319. }
  320. }
  321. /*End of file cart.php*/
  322. /*Location /htdocs/cart.php*/

2. cart_demo.php文件如下:

  1. <?php
  2. session_start();
  3. require_once('cart.php');
  4. $items = array(
  5. 0 => array(
  6. 'id' => 'sp001',
  7. 'qty' => 20,
  8. 'price' => '10.50',
  9. 'name' => 'a002',
  10. 'options' => array(
  11. 'made' => 'china',
  12. 'company' => 'bgi'
  13. )
  14. ),
  15. 1 => array(
  16. 'id' => 'sp002',
  17. 'qty' => 1,
  18. 'price' => '3.50',
  19. 'name' => 'b002'
  20. )
  21. );
  22. $arr = array(
  23. 'rowid' => '86dbb7cb58a667558b4bbb1f60330028',
  24. 'qty' => 21
  25. );
  26. $cart = new Cart();
  27. $cart->insert($items);
  28. //var_dump($cart->contents());
  29. $cart->update($arr);
  30. var_dump($cart->contents());
  31. //$cart->destroy();
  32. //var_dump($_SESSION['cart_contents']);
  33. /*end of php*/