51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

PHP利用cookies实现购物车的方法

这篇文章主要介绍了PHP利用cookies实现购物车的方法,可通过cookie实现对商品的增删改等功能,以及统计与检查等技巧,非常具有实用价值,需要的朋友可以参考下。

PHP购物车是在电子商务网站会用到的,一种像超市购物车一样的,选好商品了,先放到自己的购物车里面等好了再到柜台结算,本款PHP购物车完全按照这个原理来实例的,感兴趣的朋友可以来看看,该实例利用了cookie来实现,代码如下:

代码如下:

class cartApi {
private $cartarray = array(); // 存放购物车的二维数组
private $cartcount; // 统计购物车数量
public $expires = 86400; // cookies过期时间,如果为0则不保存到本地 单位为秒

const CLEAR_FLAT  = 3;//清空
const ADD_FLAT    = 0;//增加
const REMOVE_FLAT = 1;//减少数量
const UPDATE_FLAT = 2;//修改


/\*\*


   


    
* cartApi constructor.


    
* @param string $id


    
* @param string $name


    
* @param string $price1


    
* @param string $count


    
* @param string $image


    
* @param int $expires
  */
  public function __construct($id = "",$name = "",$price1 = "",$count = "",$image = "",$expires = 86400) {
  if ($id != "" && is_numeric($id)) {
  $this->expires = $expires;
  $this->addcart($id,$name,$price1,$count,$image);
  }
  }


   


/\*\*添加


   


    
* @param $id


    
* @param $name


    
* @param $price1


    
* @param $count


    
* @param $image


    
* @return false


    
* @author 黄业兴


    
* @date  2023/4/12 9:51
  */
  public function addcart($id,$name,$price1,$count,$image) {
  $this->cartarray = $this->cartview(); // 把数据读取并写入数组
  if ($this->checkitem($id)) { // 检测商品是否存在
  $this->modifycart($id,$count,self::ADD_FLAT); // 商品数量加$count
  return false;
  }
  $this->cartarray['id'][$id] = $id;
  $this->cartarray['name'][$id] = $name;
  $this->cartarray['price'][$id] = $price1;
  $this->cartarray['count'][$id] = $count;
  $this->cartarray['image'][$id] = $image;
  $this->save();
  }


   


/\*\*修改 $flag 0:加 1:减 2:修改 3:清空


   


    
* @param $id


    
* @param $count


    
* @param string $flag


    
* @return false


    
* @author 黄业兴


    
* @date  2023/4/12 9:52
  */
  public function modifycart($id, $count, $flag = "") {
  $tmpid = $id;
  $this->cartarray = $this->cartview(); // 把数据读取并写入数组
  $tmparray = &$this->cartarray;  // 引用
  if (!is_array($tmparray['id'])) return false;
  if ($id < 1) {
  return false;
  }
  foreach ($tmparray['id'] as $item) {
  if ($item === $tmpid) {
  switch ($flag) {
  case self::ADD_FLAT: // 添加数量 一般$count为1
  $tmparray['count'][$id] += $count;
  break;
  case self::REMOVE_FLAT: // 减少数量
  $tmparray['count'][$id] -= $count;
  break;
  case self::UPDATE_FLAT: // 修改数量
  if ($count == 0) {
  unset($tmparray['id'][$id]);
  unset($tmparray['name'][$id]);
  unset($tmparray['price'][$id]);
  unset($tmparray['count'][$id]);
  unset($tmparray['image'][$id]);
  break;
  } else {
  $tmparray['count'][$id] = $count;
  break;
  }
  case self::CLEAR_FLAT: // 清空商品
  unset($tmparray['id'][$id]);
  unset($tmparray['name'][$id]);
  unset($tmparray['price'][$id]);
  unset($tmparray['count'][$id]);
  unset($tmparray['image'][$id]);
  break;
  default:
  break;
  }
  }
  }
  $this->save();
  }


   


/\*\*清空


   


    
* @author 黄业兴


    
* @date  2023/4/12 9:52
  */
  public function removeall() {
  $this->cartarray = array();
  $this->save();
  }


   


/\*\*查看


   


    
* @return false|mixed


    
* @author 黄业兴


    
* @date  2023/4/12 9:52
  */
  public function cartview() {
  $cookie = $_COOKIE['cartapi'];
  if (!$cookie) return false;
  $tmpunserialize = unserialize($cookie);
  return $tmpunserialize;
  }


   


/\*\*检查购物车是否有商品


   


    
* @return bool


    
* @author 黄业兴


    
* @date  2023/4/12 9:53
  */
  public function checkcart() {
  $tmparray = $this->cartview();
  if (count($tmparray['id']) < 1) {
  return false;
  }
  return true;
  }


   


/\*\*商品统计


   


    
* @return array


    
* @author 黄业兴


    
* @date  2023/4/12 9:53
  */
  public function countprice() {
  $tmparray = $this->cartarray = $this->cartview();
  $outarray = array();
  $i = 0;
  if (is_array($tmparray['id'])) {
  foreach ($tmparray['id'] as $key=>$val) {
  $outarray['total'] += $tmparray['count'][$key] * $tmparray['price'][$key];
  $outarray['num'] += $tmparray['count'][$key];
  $i++;
  }
  }
  return $outarray;
  }


   


/\*\*统计数量


   


    
* @return int


    
* @author 黄业兴


    
* @date  2023/4/12 9:53
  */
  public function cartcount() {
  $tmparray = $this->cartview();
  $tmpcount = count($tmparray['count']);
  $this->cartcount = $tmpcount;
  return $tmpcount;
  }


   


/\*\*保存商品


   


    
* @author 黄业兴


    
* @date  2023/4/12 9:54
  */
  public function save() {
  $tmparray = $this->cartarray;
  $tmpserialize = serialize($tmparray);
  setcookie("cartapi",$tmpserialize,time()+$this->expires);
  }


   


/\*\*检查商品是否存在


  
   `
`
* @param $id


* @return bool|void


* @author 黄业兴



   
* `@date  2023/4/12 9:54
  */
  private function checkitem($id) {
  $tmparray = $this->cartarray;
  if (!is_array($tmparray['id'])) return;
  foreach ($tmparray['id'] as $item) {
  if ($item === $id) return true;
  }
  return false;
  }}
  `

前端页面下载:
car.zip
car1.rar

赞(6)
未经允许不得转载:工具盒子 » PHP利用cookies实现购物车的方法