PHP session购物车类

<?php
class Cart{
	public function Cart() {
		if(!isset($_SESSION['cart'])){
			$_SESSION['cart'] = array();
		}
	}

	/*
	添加商品
	param int $id 商品主键
		  string $name 商品名称
		  float $price 商品价格
		  int $num 购物数量
	*/
	public  function addItem($id,$name,$price,$num,$img) {
		//如果该商品已存在则直接加其数量
		if (isset($_SESSION['cart'][$id])) {
			$this->incNum($id,$num);
			return;
		}
		$item = array();
		$item['id'] = $id;
		$item['name'] = $name;
		$item['price'] = $price;
		$item['num'] = $num;
		$item['img'] = $img;
		$_SESSION['cart'][$id] = $item;
	}

	/*
	修改购物车中的商品数量
	int $id 商品主键
	int $num 某商品修改后的数量,即直接把某商品
	的数量改为$num
	*/
	public function modNum($id,$num=1) {
		if (!isset($_SESSION['cart'][$id])) {
			return false;
		}
		$_SESSION['cart'][$id]['num'] = $num;
	}

	/*
	商品数量+1
	*/
	public function incNum($id,$num=1) {
		if (isset($_SESSION['cart'][$id])) {
			$_SESSION['cart'][$id]['num'] += $num;
		}
	}

	/*
	商品数量-1
	*/
	public function decNum($id,$num=1) {
		if (isset($_SESSION['cart'][$id])) {
			$_SESSION['cart'][$id]['num'] -= $num;
		}

		//如果减少后,数量为0,则把这个商品删掉
		if ($_SESSION['cart'][$id]['num'] <1) {
			$this->delItem($id);
		}
	}

	/*
	删除商品
	*/
	public function delItem($id) {
		unset($_SESSION['cart'][$id]);
	}
	
	/*
	获取单个商品
	*/
	public function getItem($id) {
		return $_SESSION['cart'][$id];
	}

	/*
	查询购物车中商品的种类
	*/
	public function getCnt() {
		return count($_SESSION['cart']);
	}
	
	/*
	查询购物车中商品的个数
	*/
	public function getNum(){
		if ($this->getCnt() == 0) {
			//种数为0,个数也为0
			return 0;
		}

		$sum = 0;
		$data = $_SESSION['cart'];
		foreach ($data as $item) {
			$sum += $item['num'];
		}
		return $sum;
	}

	/*
	购物车中商品的总金额
	*/
	public function getPrice() {
		//数量为0,价钱为0
		if ($this->getCnt() == 0) {
			return 0;
		}
		$price = 0.00;
		$data = $_SESSION['cart'];
		foreach ($data as $item) {
			$price += $item['num'] * $item['price'];
		}
		return sprintf("%01.2f", $price);
	}

	/*
	清空购物车
	*/
	public function clear() {
		$_SESSION['cart'] = array();
	}
}?>
原文地址:http://www.oschina.net/code/snippet_96541_21570
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
购物车是一个常见的电子商务功能,可以使用session实现。以下是一个简单的购物车代码示例: 1. 首先,创建一个cart.php文件,用于处理购物车相关操作: ```php <?php session_start(); function addToCart($item, $quantity) { if(!isset($_SESSION['cart'])) { $_SESSION['cart'] = array(); } if(isset($_SESSION['cart'][$item])) { $_SESSION['cart'][$item] += $quantity; } else { $_SESSION['cart'][$item] = $quantity; } } function removeFromCart($item) { if(isset($_SESSION['cart'][$item])) { unset($_SESSION['cart'][$item]); } } function updateCart($item, $quantity) { if(isset($_SESSION['cart'][$item])) { $_SESSION['cart'][$item] = $quantity; } } function getCart() { if(isset($_SESSION['cart'])) { return $_SESSION['cart']; } else { return array(); } } ?> ``` 2. 然后,在需要使用购物车的页面中,包含cart.php文件,并调用相应的函数即可: ```php <?php include 'cart.php'; // 添加商品到购物车 addToCart('apple', 2); // 从购物车中移除商品 removeFromCart('banana'); // 更新购物车中商品的数量 updateCart('apple', 3); // 获取购物车中所有商品 $cart = getCart(); // 显示购物车中的商品 foreach($cart as $item => $quantity) { echo "$item: $quantity<br>"; } ?> ``` 以上代码示例中,addToCart函数用于添加商品到购物车中,removeFromCart函数用于从购物车中移除商品,updateCart函数用于更新购物车中商品的数量,getCart函数用于获取购物车中所有商品。在需要使用购物车的页面中,只需要包含cart.php文件,并调用相应的函数即可完成购物车操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值