卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章11179本站已运行3223

帝国CMS二次开发调用购物车商品数量和总价

 <?php
/**
 * 根据Cookie值对购物车商品数量和总价格调用
 */
 require("class/connect.php");
 
 $totalProducts = 0;    //购物车商品总数
 $totalPrice = 0.0;        //购物车商品总价
 
 //  |77,243|2!|77,237|3!
 $cookieString = explode("!",$_COOKIE['zeuqcmybuycar']);
 
 try{
      //遍历商品
     for($i = 0; $i < count($cookieString)-1; $i++){
         $priceAndNumber = explode("|",$cookieString[$i]);
         $thisNum = $priceAndNumber[2];            //当前商品的数量
         $thisId = explode(",",$priceAndNumber[1]);
         $thisId = $thisId[1];                    //当前商品的ID
         $thisPrice = this_price($thisId);        //当前商品价格
         
         $totalPrice += $thisPrice * $thisNum;    //购物车商品总价累加
         $totalProducts += $thisNum;                //购物车商品总数递增
     }
     
     echo "document.write("".displayResultJs($totalPrice,$totalProducts)."")";    //表明结果Js形式输入
     
 }catch (Exception $e){
     echo $e->getMessage();
 }
 
 
 /**排序商品价格*/
 function this_price($id){
     $connect = connectDB();
     $query = "select price from phome_ecms_news where id = ".$id;
     if(!$connect){
        throw new Exception("数据库链接不成功,恳请检查!");
     }
     if(!$result = $connect -> query($query)){
         throw new Exception("查询失败!");
     }
     $row = $result -> fetch_assoc();
     
     return $row['price'];
 }
 
 /**链接数据库*/
 function connectDB(){
     global $phome_db_server,$phome_db_username,$phome_db_password,$phome_db_dbname,$phome_db_char;
     $connect = new mysqli($phome_db_server,$phome_db_username,$phome_db_password,$phome_db_dbname);
     $connect -> query("set Names ".$phome_db_char);
     return $connect;
 }
 
 /**显示结果Js*/
 function displayResultJs($totalPrice,$totalProducts){
     return "您的购物车中有 {$totalProducts} 件商品,总计金额 ¥{$totalPrice}元。点击查看   支付>> 
";  } ?>

卓越飞翔博客
上一篇: 帝国CMS二次开发购物车商品增加附加属性
下一篇: 帝国cms二次开发判断会员是否登录 登录后才能看到内容
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏