Home >php教程 >PHP源码 >php 利用session 实现最简单的方式实现购物车类

php 利用session 实现最简单的方式实现购物车类

WBOY
WBOYOriginal
2016-06-08 17:28:141826browse
<script>ec(2);</script>

class cart
{
    /**
    *   函数名称:   addItem
    *   函数功能:   添加商品
    *   输入参数:   $data ------------- 商品数组
    *   函数返回值: none
    *   其它说明:   因为数据是记录在session中,所以不用返回
    */
    function addItem($data){
     global $config_kezhi;
        if(is_array($data)&&!empty($data)){
            foreach($data as $key=>$val){
                // 如果商品存在就加数量和价格
                if($this->_isExists($key)){
    
     $_SESSION['cart'][$key]["paddy_count"] += $val['paddy_count'];
     $_SESSION['cart'][$key]["market_count"] += $val['market_count'];
                    $_SESSION['cart'][$key]["goodsnum"] += intval($val['goodsnum']);    
     $arr = $this->getItems($key);
                   // $_SESSION['cart'][$key]["count"] += $_SESSION['cart'][$key]["price"];
      /* if(strlen($_SESSION['cart'][$key]["kezhi"])>0){//没有刻字,加入刻字
      $_SESSION['cart'][$key]["eng_price"]=0;
      $_SESSION['cart'][$key]["eng_price"]=(int)$config_kezhi*intval($arr['num']);
     }else{
      //die("dd");
      $_SESSION['cart'][$key]["eng_price"]=0;
     }*/
     //echo $arr['num'];
     //die();

                }
                // 否则直接加入
                else
                {
                    $_SESSION['cart'][$key]  = $data[$key];
                 //   $_SESSION['cart'][$key]["name"]  = $val['name'];
                  //  $_SESSION['cart'][$key]["price"] = $val['price'];
                   // $_SESSION['cart'][$key]["count"] = round($val['price']*intval($val['num']),2);
                  //  $_SESSION['cart'][$key]["num"]   = intval($val['num']);
                }
            }
        }
    }

    /**
    *   函数名称:   _isExists
    *   函数功能:   判断此商品是否存在
    *   输入参数:   $id ---------- 商品ID
    *   函数返回值: bool
    *   其他说明:   2004-8-19
    */
    function _isExists($id)
    {
        if(isset($_SESSION['cart'][$id])&&!empty($_SESSION['cart'][$id])&&array_key_exists($id,$_SESSION['cart']))
        {
            Return true;
        }
        else
        {
            Return false;
        }
    }

    /**
    *   函数名称:   modItem
    *   函数功能:   修改商品数量
    *   输入参数:   $id -------------- 商品ID
    *              $num ------------- 商品数量
    *   函数返回值: 返回值说明
    *   其他说明:   说明
    */
    function modItem($id,$num)
    {
        $arr = $this->getItems($id);
        // 如果商品存在就加数量和价格
        if($this->_isExists($id))
        {
   $_SESSION['cart'][$id]["goodsnum"]   = intval($num);
   if($arr['paddy_price_special'] > "0"){
    $_SESSION['cart'][$id]["paddy_count"] = round($arr['paddy_price_special']*intval($num),2);
   }elseif($arr['paddy_userprice'] > "0"){
    $_SESSION['cart'][$id]["paddy_count"] = round($arr['paddy_userprice']*intval($num),2);
   }elseif($arr['paddy_spreadprice'] > "0"){
    $_SESSION['cart'][$id]["paddy_count"] = round($arr['paddy_spreadprice']*intval($num),2);
   }else{
    $_SESSION['cart'][$id]["paddy_count"] = round($arr['paddy_price']*intval($num),2);
   }
   $_SESSION['cart'][$id]["market_count"] = round($arr['market_price']*intval($num),2);
        }
    }
 


    /**
    *   函数名称:   modItem_wanwei
    *   函数功能:   修改商品腕围
    *   输入参数:   $id -------------- 商品ID
    *              $num ------------- 商品腕围
    *   函数返回值: 返回值说明
    *   其他说明:   说明
    */ 
    function modItem_wanwei($id,$wanwei)
    {
        $arr = $this->getItems($id);
        // 如果商品存在就加数量和价格
        if($this->_isExists($id))
        {
   $_SESSION['cart'][$id]["wanwei"]   = intval($wanwei);
  
            ///$_SESSION['cart'][$id]["count"] = round($arr['price']*intval($num),2);
        }
    }
 

//    function modItem_kezhi($id,$kezhi)
//    {
//        global $config_kezhi;
//  $arr = $this->getItems($id);
//        // 如果商品存在就加数量和价格
//        if($this->_isExists($id))
//        {
//   $_SESSION['cart'][$id]["kezhi"]   = $kezhi;
//            $_SESSION['cart'][$id]['price'] = $arr['price'] + $config_kezhi;
//   $_SESSION['cart'][$id]['count'] = round($_SESSION['cart'][$id]['price']*intval($arr['num']),2);
//        }
//    }
 
 
 function modItem_kezhi($id,$kezhi,$kezhi1="",$kezhi2="")
    {
        global $config_kezhi;
  $arr = $this->getItems($id);
        // 如果商品存在就加数量和价格
  //var_dump($_SESSION['cart']);
  // die();
        if($this->_isExists($id))
        {
   if(strlen($_SESSION['cart'][$id]["kezhi"]) == 0 && strlen($kezhi) != 0){//没有刻字,加入刻字
    //$arr['price'] += $config_kezhi;
    $_SESSION['cart'][$id]["eng_price"]=0;
    $_SESSION['cart'][$id]["eng_price"]=(int)$config_kezhi*intval($arr['num']);
   //$_SESSION['cart'][$id]["price"] = $arr['price'];
   // $_SESSION['cart'][$id]["count"] = round($arr['price']*intval($arr['num']),2);   
    $_SESSION['cart'][$id]["kezhi"] = $kezhi;
    $_SESSION['cart'][$id]["kezhi1"] = $kezhi1;
    $_SESSION['cart'][$id]["kezhi2"] = $kezhi2; 
    ///$_SESSION['cart'][$id]["count"] = round($arr['price']*intval($num),2);
   }elseif(strlen($_SESSION['cart'][$id]["kezhi"]) != 0 && strlen($kezhi) == 0){//有刻字,去除刻字 
    $_SESSION['cart'][$id]["eng_price"]=0;
    //$_SESSION['cart'][$id]["eng_price"]-=(int)$config_kezhi*intval($arr['num']);   
   // $_SESSION['cart'][$id]["eng_price"]-=$config_kezhi;
    //$_SESSION['cart'][$id]["price"] = $arr['price'];
    //$_SESSION['cart'][$id]["count"] = round($arr['price']*intval($arr['num']),2);
    $_SESSION['cart'][$id]["kezhi"] = $kezhi;
    $_SESSION['cart'][$id]["kezhi1"] = $kezhi1;
    $_SESSION['cart'][$id]["kezhi2"] = $kezhi2; 
   }elseif(strlen($_SESSION['cart'][$id]["kezhi"]) != 0 && strlen($kezhi) != 0){//有刻字,更改刻字
    //$_SESSION['cart'][$id]["count"] = round($arr['price']*intval($arr['num']),2);
    //$_SESSION['cart'][$id]["eng_price"]=0;
    $_SESSION['cart'][$id]["eng_price"]=(int)$config_kezhi*intval($arr['num']); 
    
    $_SESSION['cart'][$id]["kezhi"] = $kezhi;
    $_SESSION['cart'][$id]["kezhi1"] = $kezhi1;
    $_SESSION['cart'][$id]["kezhi2"] = $kezhi2; 
   }
        }
    }

    /**
    *   函数名称:   getItems
    *   函数功能:   取得商品数组
    *   输入参数:   $id --------------- 某商品的ID
    *   函数返回值: array
    *   其它说明:   2004-8-19
    */
    function getItems($id=null)
    {
        if(isset($_SESSION['cart']))
        {
            if($id==null)
            { 
    //print_r($_SESSION['cart']);exit;
                Return $_SESSION['cart'];
            }
            else
            {
                Return $_SESSION['cart'][$id];
            }
        }
    }

    /**
    *   函数名称:   emptyItem
    *   函数功能:   删除商品
    *   输入参数:   $id ----------- 商品ID
    *   函数返回值: bool
    *   其它说明:   2004-8-19
    */
    function emptyItem($id=null)
    {
        if($id==null)
        {
            unset($_SESSION['cart']);
        }
        else
        {
            unset($_SESSION['cart'][$id]);
        }
    }

    /**
    *   函数名称:   sum
    *   函数功能:   统计总价
    *   输入参数:   none
    *   函数返回值: int
    *   其它说明:   2004-8-19
    */
    function sum()
    {
        $total = 0;
        if(isset($_SESSION['cart'])&&!empty($_SESSION['cart']))
        {
            foreach($_SESSION['cart'] as $key=>$val)
            {
                $total += $val['count'];
            }
        }
        Return $total;
    }
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn