0

0

PHP购物车种,移植于CodeIgniter

php中文网

php中文网

发布时间:2016-06-13 10:39:27

|

957人浏览过

|

来源于php中文网

原创

PHP购物车类,移植于CodeIgniter

session('cart_contents') !== FALSE)		{			$this->_cart_contents = $this->session('cart_contents');		}		else		{			// 初始化数据			$this->_cart_contents['cart_total'] = 0;			$this->_cart_contents['total_items'] = 0;		}	}	// --------------------------------	/**	 * 添加到购物车	 *	 * @access	public	 * @param	array	 * @return	bool	 */	function insert($items = array())	{		// 检测数据是否正确		if ( ! is_array($items) OR count($items) == 0)		{			return FALSE;		}		// 可以添加一个商品(一维数组),也可以添加多个商品(二维数组)		$save_cart = FALSE;		if (isset($items['id']))		{			if ($this->_insert($items) == TRUE)			{				$save_cart = TRUE;			}		}		else		{			foreach ($items as $val)			{				if (is_array($val) AND isset($val['id']))				{					if ($this->_insert($val) == TRUE)					{						$save_cart = TRUE;					}				}			}		}		// 更新数据		if ($save_cart == TRUE)		{			$this->_save_cart();			return TRUE;		}		return FALSE;	}	// --------------------------------	/**	 * 处理插入购物车数据	 *	 * @access	private	 * @param	array	 * @return	bool	 */	function _insert($items = array())	{		// 检查购物车		if ( ! is_array($items) OR count($items) == 0)		{			return FALSE;		}		// --------------------------------		/* 前四个数组索引 (id, qty, price 和name) 是 必需的。		   如果缺少其中的任何一个,数据将不会被保存到购物车中。		   第5个索引 (options) 是可选的。当你的商品包含一些相关的选项信息时,你就可以使用它。		   请使用一个数组来保存选项信息。注意:$data['price'] 的值必须大于0 		   如:$data = array(               'id'      => 'sku_123ABC',               'qty'     => 1,               'price'   => 39.95,               'name'    => 'T-Shirt',               'options' => array('Size' => 'L', 'Color' => 'Red')            );		*/		if ( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name']))		{			return FALSE;		}		// --------------------------------		// 数量验证,不是数字替换为空		$items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty']));		// 数量验证		$items['qty'] = trim(preg_replace('/(^[0]+)/i', '', $items['qty']));		// 数量必须是数字或不为0		if ( ! is_numeric($items['qty']) OR $items['qty'] == 0)		{			return FALSE;		}		// --------------------------------		// 产品ID验证		if ( ! preg_match("/^[".$this->product_id_rules."]+$/i", $items['id']))		{			return FALSE;		}		// --------------------------------		// 验证产品名称,考虑到汉字,暂不使用		/*		if ( ! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name']))		{			return FALSE;		}		*/		// --------------------------------		// 价格验证		$items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price']));		$items['price'] = trim(preg_replace('/(^[0]+)/i', '', $items['price']));		// 验证价格是否是数值		if ( ! is_numeric($items['price']))		{			return FALSE;		}		// --------------------------------		// 属性验证,如果属性存在,属性值+产品ID进行加密保存在$rowid中		if (isset($items['options']) AND count($items['options']) > 0)		{			$rowid = md5($items['id'].implode('', $items['options']));		}		else		{			// 没有属性时直接对产品ID加密			$rowid = md5($items['id']);		}				// 检测购物车中是否有该产品,如果有,在原来的基础上加上本次新增的商品数量		$_contents = $this->_cart_contents;		$_tmp_contents = array();		foreach ($_contents as $val)		{			if (is_array($val) AND isset($val['rowid']) AND isset($val['qty']) AND $val['rowid']==$rowid)			{				$_tmp_contents[$val['rowid']]['qty'] = $val['qty'];			} else {				$_tmp_contents[$val['rowid']]['qty'] = 0;			}		}		// --------------------------------		// 清除原来的数据		unset($this->_cart_contents[$rowid]);		// 重新赋值		$this->_cart_contents[$rowid]['rowid'] = $rowid;		// 添加新项目		foreach ($items as $key => $val)		{			if ($key=='qty' && isset($_tmp_contents[$rowid][$key])) {				$this->_cart_contents[$rowid][$key] = $val+$_tmp_contents[$rowid][$key];			} else {				$this->_cart_contents[$rowid][$key] = $val;			}		}		return TRUE;	}	// --------------------------------	/**	 * 更新购物车	 * 	 * @access	public	 * @param	array	 * @param	string	 * @return	bool	 */	function update($items = array())	{		// 验证		if ( ! is_array($items) OR count($items) == 0)		{			return FALSE;		}		$save_cart = FALSE;		if (isset($items['rowid']) AND isset($items['qty']))		{			if ($this->_update($items) == TRUE)			{				$save_cart = TRUE;			}		}		else		{			foreach ($items as $val)			{				if (is_array($val) AND isset($val['rowid']) AND isset($val['qty']))				{					if ($this->_update($val) == TRUE)					{						$save_cart = TRUE;					}				}			}		}		if ($save_cart == TRUE)		{			$this->_save_cart();			return TRUE;		}		return FALSE;	}	// --------------------------------	/**	 * 处理更新购物车	 *	 * @access	private	 * @param	array	 * @return	bool	 */	function _update($items = array())	{		if ( ! isset($items['qty']) OR ! isset($items['rowid']) OR ! isset($this->_cart_contents[$items['rowid']]))		{			return FALSE;		}		// 检测数量		$items['qty'] = preg_replace('/([^0-9])/i', '', $items['qty']);		if ( ! is_numeric($items['qty']))		{			return FALSE;		}		if ($this->_cart_contents[$items['rowid']]['qty'] == $items['qty'])		{			return FALSE;		}		if ($items['qty'] == 0)		{			unset($this->_cart_contents[$items['rowid']]);		}		else		{			$this->_cart_contents[$items['rowid']]['qty'] = $items['qty'];		}		return TRUE;	}	// --------------------------------	/**	 * 保存购物车到Session里	 *	 * @access	private	 * @return	bool	 */	function _save_cart()	{		unset($this->_cart_contents['total_items']);		unset($this->_cart_contents['cart_total']);		$total = 0;		$items = 0;		foreach ($this->_cart_contents as $key => $val)		{			if ( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty']))			{				continue;			}			$total += ($val['price'] * $val['qty']);			$items += $val['qty'];			$this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty']);		}		$this->_cart_contents['total_items'] = $items;		$this->_cart_contents['cart_total'] = $total;		if (count($this->_cart_contents) <= 2)		{			$this->session('cart_contents', array());			return FALSE;		}		$this->session('cart_contents',$this->_cart_contents);		return TRUE;	}	// --------------------------------	/**	 * 购物车中的总计金额	 *	 * @access	public	 * @return	integer	 */	function total()	{		return $this->_cart_contents['cart_total'];	}	// --------------------------------	/**	 * 购物车中总共的项目数量	 *	 *	 * @access	public	 * @return	integer	 */	function total_items()	{		return $this->_cart_contents['total_items'];	}	// --------------------------------	/**	 * 购物车中所有信息的数组	 *	 * 返回一个包含了购物车中所有信息的数组	 *	 * @access	public	 * @return	array	 */	function contents()	{		$cart = $this->_cart_contents;		unset($cart['total_items']);		unset($cart['cart_total']);		return $cart;	}	// --------------------------------	/**	 * 购物车中是否有特定的列包含选项信息	 *	 * 如果购物车中特定的列包含选项信息,本函数会返回 TRUE(布尔值),本函数被设计为与 contents() 一起在循环中使用	 *	 * @access	public	 * @return	array	 */	function has_options($rowid = '')	{		if ( ! isset($this->_cart_contents[$rowid]['options']) OR count($this->_cart_contents[$rowid]['options']) === 0)		{			return FALSE;		}		return TRUE;	}	// --------------------------------	/**	 * 以数组的形式返回特定商品的选项信息	 *	 * 本函数被设计为与 contents() 一起在循环中使用	 *	 * @access	public	 * @return	array	 */	function product_options($rowid = '')	{		if ( ! isset($this->_cart_contents[$rowid]['options']))		{			return array();		}		return $this->_cart_contents[$rowid]['options'];	}	// --------------------------------	/**	 * 格式化数值	 *	 * 返回格式化后带小数点的值(小数点后有2位),一般价格使用	 *	 * @access	public	 * @return	integer	 */	function format_number($n = '')	{		if ($n == '')		{			return '';		}		$n = trim(preg_replace('/([^0-9\.])/i', '', $n));		return number_format($n, 2, '.', ',');	}	// --------------------------------	/**	 * 销毁购物车	 *	 * 这个函数一般是在处理完用户订单后调用	 *	 * @access	public	 * @return	null	 */	function destroy()	{		unset($this->_cart_contents);		$this->_cart_contents['cart_total'] = 0;		$this->_cart_contents['total_items'] = 0;		$this->session('cart_contents', array());	}		// --------------------------------	/**	 * 保存Session	 *	 * 须有session_start();	 *	 * @access	private	 * @return	bool	 */	function session($name = 'cart_contents',$value = NULL) {		if ($name=='') $name = 'cart_contents';		if ($value == NULL) {			return @$_SESSION[$name];		} else {			if (!empty($value) && is_array($value)) {				$_SESSION[$name] = $value;				return TRUE;			} else {				return FALSE;			}		}	}}?>

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
微信文件过期恢复教程
微信文件过期恢复教程

本专题整合了微信文件过期恢复方法、技巧教程,阅读专题下面的文章了解更多详细内容。

0

2026.02.04

抖音网页版入口与视频观看指南 抖音官网视频在线访问
抖音网页版入口与视频观看指南 抖音官网视频在线访问

本专题汇总了抖音网页版的入口链接、官方登录页面以及视频观看入口,帮助用户快速访问抖音网页版,提供免登录访问方式和直接进入视频播放页面的方法,确保顺利浏览和观看抖音视频。

63

2026.02.04

学习通网页版入口与在线学习指南 学习通官网登录与使用方法
学习通网页版入口与在线学习指南 学习通官网登录与使用方法

本专题详细汇总了学习通网页版入口与登录方法,提供学习通官方网页端入口、学生登录平台、网页版使用指南等内容,帮助用户快速稳定地登录学习通官网,顺利进入学习平台,提升学习效率和体验。

9

2026.02.04

Python Web 框架 Django 深度开发
Python Web 框架 Django 深度开发

本专题系统讲解 Python Django 框架的核心功能与进阶开发技巧,包括 Django 项目结构、数据库模型与迁移、视图与模板渲染、表单与认证管理、RESTful API 开发、Django 中间件与缓存优化、部署与性能调优。通过实战案例,帮助学习者掌握 使用 Django 快速构建功能全面的 Web 应用与全栈开发能力。

9

2026.02.04

Java 流式处理与 Apache Kafka 实战
Java 流式处理与 Apache Kafka 实战

本专题专注讲解 Java 在流式数据处理与消息队列系统中的应用,系统讲解 Apache Kafka 的基础概念、生产者与消费者模型、Kafka Streams 与 KSQL 流式处理框架、实时数据分析与监控,结合实际业务场景,帮助开发者构建 高吞吐量、低延迟的实时数据流管道,实现高效的数据流转与处理。

3

2026.02.04

Golang 容器化与 Docker 实战
Golang 容器化与 Docker 实战

本专题深入讲解 Golang 应用的容器化与 Docker 部署,涵盖 Docker 基础概念、容器构建与镜像管理、Go 应用的 Dockerfile 编写、跨平台容器部署与优化、Docker Compose 和 Kubernetes 部署工具。通过实际案例,帮助学习者掌握 如何将 Golang 应用容器化并实现高效部署与管理,提升系统的可扩展性与运维效率。

3

2026.02.04

全国统一发票查询平台入口合集
全国统一发票查询平台入口合集

本专题整合了全国统一发票查询入口地址合集,阅读专题下面的文章了解更多详细入口。

59

2026.02.03

短剧入口地址汇总
短剧入口地址汇总

本专题整合了短剧app推荐平台,阅读专题下面的文章了解更多详细入口。

110

2026.02.03

植物大战僵尸版本入口地址汇总
植物大战僵尸版本入口地址汇总

本专题整合了植物大战僵尸版本入口地址汇总,前往文章中寻找想要的答案。

56

2026.02.03

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
JavaScript高级框架设计视频教程
JavaScript高级框架设计视频教程

共22课时 | 3.6万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号