登录  /  注册

CI框架实现购物车功能步骤详解

php中世界最好的语言
发布: 2018-05-17 10:19:39
原创
1740人浏览过

这次给大家带来CI框架实现购物车功能步骤详解,CI框架实现购物车功能的注意事项有哪些,下面就是实战案例,一起来看一下。

在商城项目中,购物车是非常重要的一环,此处留下源码,留作笔记!!!

话不多说,往下看:

1. 源代码

<?php
defined(&#39;BASEPATH&#39;) OR exit(&#39;No direct script access allowed&#39;);
class cart extends Home_Controller {
  private $info = array();    #前台提交数据
  private $specData = array();  #规格信息
  private $prodData = array();  #货品组合信息
  private $cartData = array();  #购物车入库数据
  /**
   * 构造函数
   */
  public function construct()
  {
    parent::construct();
    $this->load->model(&#39;goodsModel&#39;,&#39;goods&#39;);
    $this->load->model(&#39;productModel&#39;,&#39;product&#39;);
    $this->load->model(&#39;goodsAttrModel&#39;,&#39;goodsAttr&#39;);
  }
  /**
   * [购物车]数据添加
   */
  public function cartAdd()
  {
    #接收购物车提交数据
    $this->info = $this->input->post();
  // $this->ajaxReturn($this->info);
    #1.验证商品库存、货品库存
    $this->checkGoodsNumber();
    #2.查询规格名称、价格
    $this->getSpecData();
    #3.组装购物车添加de数据
    $cartData = $this->setCartData();
  p(json_decode($this->input->cookie(&#39;cart&#39;),true));
    # 一、判断是否登录
    if(!UID){
      //未登录 数据存入Cookie中
      //1:获取cookie中的购物车数据
      $cookieCartData = $this->input->cookie(&#39;cart&#39;);
      //2:判断cookie中数据是否为空
      if(empty($cookieCartData)){
        //2-1:为空则表示用户没有添加过购物车
        //2-1-1.设置Key-->生成购物车数据
        $key = $cartData[&#39;goods_id&#39;].&#39;-&#39;.$cartData[&#39;product_id&#39;];
        $cookieCart = array($key => $cartData);
        //2-1-2.设置购物车返回值(商品数量、总价)
        $this->setCartReturn(1,$cartData[&#39;goods_price&#39;]);
        //2-1-3.设置Cookie存储购物车数据
      }else{
        //2-2:不为空 表示用户添加过购物车
        //2-2-1.追加购物数据
        $cookieCart = $this->addCartData($cartData,json_decode($cookieCartData,true));
        //2-2-2.设置购物车返回值(商品数量、总价)
        $this->setCartReturn(count($cookieCart),array_sum(array_column($cookieCart, &#39;goods_price&#39;)));
      }
      //3:设置Cookie存储购物车数据
      setCookie(&#39;cart&#39;,json_encode($cookieCart),LEFT_TIME,&#39;/&#39;);
    }else{
      //已登录 数据存入数据库
    }
    //返回购物车提示数据
    $this->ajaxReturn($this->msg);
  }
  /**
   * 验证商品库存
   */
  public function checkGoodsNumber()
  {
    $this->goods->map = array(
      &#39;goods_id&#39;     =>  $this->info[&#39;goods_id&#39;],
      &#39;goods_number >=&#39;  => $this->info[&#39;buy_number&#39;],
    );
    $this->goods = $this->goods->find(&#39;goods_id,goods_name,goods_sn,goods_img,shop_price&#39;);
    if(!$this->goods){
      $this->msg[&#39;msg&#39;] = "商品库存不足";
      $this->ajaxReturn($this->msg);
    }
    #验证货品库存
    $this->product->map = array(
      &#39;goods_id&#39;     =>  $this->info[&#39;goods_id&#39;],
      &#39;product_attr&#39;   =>  $this->info[&#39;prod_attr&#39;],
      &#39;product_number >=&#39; => $this->info[&#39;buy_number&#39;],
    );
    $this->prodData = $this->product->find();
    if(!$this->prodData){
      $this->msg[&#39;msg&#39;] = "货品库存不足";
      $this->ajaxReturn($this->msg);
    }
    return true;
  }
  /**
   * 组合规格名称、价格
   */
  public function getSpecData()
  {
    $this->goodsAttr->map = inToType(explode("|", $this->info[&#39;prod_attr&#39;]),&#39;goods_attr_id&#39;);
    $goodsAttrInfo = $this->goodsAttr->select(&#39;goods_attr_value,goods_attr_price&#39;);
    $this->specData[&#39;product_attr_value&#39;] = implode("|", array_column($goodsAttrInfo, &#39;goods_attr_value&#39;));
    $this->specData[&#39;product_price&#39;] = array_sum(array_column($goodsAttrInfo,&#39;goods_attr_price&#39;));
    # 返回规格信息 $this->specData
  }
  /**
   * 组装购物车添加的数组
   */
  public function setCartData()
  {
    $this->cartData = array(
      &#39;product_id&#39;  =>  $this->prodData[&#39;product_id&#39;],
      &#39;product_attr&#39; =>  $this->prodData[&#39;product_attr&#39;],
      &#39;buy_number&#39;  =>  $this->info[&#39;buy_number&#39;],
      &#39;goods_price&#39;  =>  $this->info[&#39;shop_price&#39;],
      &#39;goods_sum&#39;   =>  $this->info[&#39;shop_price&#39;] * $this->info[&#39;buy_number&#39;],
      &#39;product_price&#39; =>  &#39;&#39;,
      &#39;product_attr_value&#39;  =>  &#39;&#39;,
      &#39;uid&#39;      =>  UID,
    );
    $this->cartData = array_merge($this->cartData,$this->goods);
    #若存在规格【添加规格信息】
    if(!empty($this->info[&#39;prod_attr&#39;])){
      $this->cartData[&#39;product_price&#39;] = $this->specData[&#39;product_price&#39;];
      $this->cartData[&#39;product_attr_value&#39;] = $this->specData[&#39;product_attr_value&#39;];
    }
    return $this->cartData;
    # 购物车 添加的总数据 $this->cartData;
  }
  /**
   * 设置购物车返回提示数据
   * @param [商品数量,总价]
   */
  public function setCartReturn($number,$prices)
  {
    $this->msg[&#39;code&#39;] = self::STATUS_ON;
    $this->msg[&#39;data&#39;] = array(
        &#39;number&#39;  =>  $number,
        &#39;prices&#39;  =>  $prices,
    );
  }
  /**
   * 购物车 新添加数据
   * @param [新数据,原购物车数据]
   */
  public function addCartData($newData,$oldData)
  {
    #组合Key
    $key = $newData[&#39;goods_id&#39;].&#39;-&#39;.$newData[&#39;product_id&#39;];
    // #判断购物车中是否有该商品
    if(isset($oldData[$key])){
      //1.有 合并商品数量、价格
      $oldData[$key][&#39;buy_number&#39;] = $oldData[$key][&#39;buy_number&#39;] + $newData[&#39;buy_number&#39;];
      $oldData[$key][&#39;goods_price&#39;] = $newData[&#39;goods_price&#39;];
      $oldData[$key][&#39;goods_sum&#39;] = $oldData[$key][&#39;buy_number&#39;] * $oldData[$key][&#39;goods_price&#39;];
    }else{
      //2.没有 追加新商品
      $oldData[$key] = $newData;
    }
    #返回购物车数据
    return $oldData;
  }
}
?>
登录后复制

2. 数据库

CREATE TABLE `shop_goods` (
 `goods_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `goods_name` varchar(255) NOT NULL,
 `type_id` int(11) DEFAULT NULL,
 PRIMARY KEY (`goods_id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
CREATE TABLE `shop_product` (
 `product_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `goods_id` int(11) NOT NULL,
 `goods_price` decimal(10,2) NOT NULL,
 `goods_num` int(11) NOT NULL,
 `goods_sn` varchar(50) NOT NULL,
 `goods_attr_id` varchar(100) NOT NULL,
 PRIMARY KEY (`product_id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
CREATE TABLE `shop_goods_attr` (
 `goods_attr_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `goods_id` int(11) NOT NULL,
 `attr_id` int(11) NOT NULL,
 `attr_value` varchar(255) NOT NULL,
 PRIMARY KEY (`goods_attr_id`)
) ENGINE=InnoDB AUTO_INCREMENT=126 DEFAULT CHARSET=utf8;
登录后复制

CI购物车总结完毕!!!

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

PHP实现大文件切割与合并步骤详解

PHP组词算法实现详解

以上就是CI框架实现购物车功能步骤详解的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

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