溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

CodeIgniter框架如何實(shí)現(xiàn)購物車功能

發(fā)布時間:2021-06-03 14:28:29 來源:億速云 閱讀:129 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下CodeIgniter框架如何實(shí)現(xiàn)購物車功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

1. 源代碼

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class cart extends Home_Controller {
  private $info = array();    #前臺提交數(shù)據(jù)
  private $specData = array();  #規(guī)格信息
  private $prodData = array();  #貨品組合信息
  private $cartData = array();  #購物車入庫數(shù)據(jù)
  /**
   * 構(gòu)造函數(shù)
   */
  public function __construct()
  {
    parent::__construct();
    $this->load->model('goodsModel','goods');
    $this->load->model('productModel','product');
    $this->load->model('goodsAttrModel','goodsAttr');
  }
  /**
   * [購物車]數(shù)據(jù)添加
   */
  public function cartAdd()
  {
    #接收購物車提交數(shù)據(jù)
    $this->info = $this->input->post();
  // $this->ajaxReturn($this->info);
    #1.驗證商品庫存、貨品庫存
    $this->checkGoodsNumber();
    #2.查詢規(guī)格名稱、價格
    $this->getSpecData();
    #3.組裝購物車添加de數(shù)據(jù)
    $cartData = $this->setCartData();
  p(json_decode($this->input->cookie('cart'),true));
    # 一、判斷是否登錄
    if(!UID){
      //未登錄 數(shù)據(jù)存入Cookie中
      //1:獲取cookie中的購物車數(shù)據(jù)
      $cookieCartData = $this->input->cookie('cart');
      //2:判斷cookie中數(shù)據(jù)是否為空
      if(empty($cookieCartData)){
        //2-1:為空則表示用戶沒有添加過購物車
        //2-1-1.設(shè)置Key-->生成購物車數(shù)據(jù)
        $key = $cartData['goods_id'].'-'.$cartData['product_id'];
        $cookieCart = array($key => $cartData);
        //2-1-2.設(shè)置購物車返回值(商品數(shù)量、總價)
        $this->setCartReturn(1,$cartData['goods_price']);
        //2-1-3.設(shè)置Cookie存儲購物車數(shù)據(jù)
      }else{
        //2-2:不為空 表示用戶添加過購物車
        //2-2-1.追加購物數(shù)據(jù)
        $cookieCart = $this->addCartData($cartData,json_decode($cookieCartData,true));
        //2-2-2.設(shè)置購物車返回值(商品數(shù)量、總價)
        $this->setCartReturn(count($cookieCart),array_sum(array_column($cookieCart, 'goods_price')));
      }
      //3:設(shè)置Cookie存儲購物車數(shù)據(jù)
      setCookie('cart',json_encode($cookieCart),LEFT_TIME,'/');
    }else{
      //已登錄 數(shù)據(jù)存入數(shù)據(jù)庫
    }
    //返回購物車提示數(shù)據(jù)
    $this->ajaxReturn($this->msg);
  }
  /**
   * 驗證商品庫存
   */
  public function checkGoodsNumber()
  {
    $this->goods->map = array(
      'goods_id'     =>  $this->info['goods_id'],
      'goods_number >='  => $this->info['buy_number'],
    );
    $this->goods = $this->goods->find('goods_id,goods_name,goods_sn,goods_img,shop_price');
    if(!$this->goods){
      $this->msg['msg'] = "商品庫存不足";
      $this->ajaxReturn($this->msg);
    }
    #驗證貨品庫存
    $this->product->map = array(
      'goods_id'     =>  $this->info['goods_id'],
      'product_attr'   =>  $this->info['prod_attr'],
      'product_number >=' => $this->info['buy_number'],
    );
    $this->prodData = $this->product->find();
    if(!$this->prodData){
      $this->msg['msg'] = "貨品庫存不足";
      $this->ajaxReturn($this->msg);
    }
    return true;
  }
  /**
   * 組合規(guī)格名稱、價格
   */
  public function getSpecData()
  {
    $this->goodsAttr->map = inToType(explode("|", $this->info['prod_attr']),'goods_attr_id');
    $goodsAttrInfo = $this->goodsAttr->select('goods_attr_value,goods_attr_price');
    $this->specData['product_attr_value'] = implode("|", array_column($goodsAttrInfo, 'goods_attr_value'));
    $this->specData['product_price'] = array_sum(array_column($goodsAttrInfo,'goods_attr_price'));
    # 返回規(guī)格信息 $this->specData
  }
  /**
   * 組裝購物車添加的數(shù)組
   */
  public function setCartData()
  {
    $this->cartData = array(
      'product_id'  =>  $this->prodData['product_id'],
      'product_attr' =>  $this->prodData['product_attr'],
      'buy_number'  =>  $this->info['buy_number'],
      'goods_price'  =>  $this->info['shop_price'],
      'goods_sum'   =>  $this->info['shop_price'] * $this->info['buy_number'],
      'product_price' =>  '',
      'product_attr_value'  =>  '',
      'uid'      =>  UID,
    );
    $this->cartData = array_merge($this->cartData,$this->goods);
    #若存在規(guī)格【添加規(guī)格信息】
    if(!empty($this->info['prod_attr'])){
      $this->cartData['product_price'] = $this->specData['product_price'];
      $this->cartData['product_attr_value'] = $this->specData['product_attr_value'];
    }
    return $this->cartData;
    # 購物車 添加的總數(shù)據(jù) $this->cartData;
  }
  /**
   * 設(shè)置購物車返回提示數(shù)據(jù)
   * @param [商品數(shù)量,總價]
   */
  public function setCartReturn($number,$prices)
  {
    $this->msg['code'] = self::STATUS_ON;
    $this->msg['data'] = array(
        'number'  =>  $number,
        'prices'  =>  $prices,
    );
  }
  /**
   * 購物車 新添加數(shù)據(jù)
   * @param [新數(shù)據(jù),原購物車數(shù)據(jù)]
   */
  public function addCartData($newData,$oldData)
  {
    #組合Key
    $key = $newData['goods_id'].'-'.$newData['product_id'];
    // #判斷購物車中是否有該商品
    if(isset($oldData[$key])){
      //1.有 合并商品數(shù)量、價格
      $oldData[$key]['buy_number'] = $oldData[$key]['buy_number'] + $newData['buy_number'];
      $oldData[$key]['goods_price'] = $newData['goods_price'];
      $oldData[$key]['goods_sum'] = $oldData[$key]['buy_number'] * $oldData[$key]['goods_price'];
    }else{
      //2.沒有 追加新商品
      $oldData[$key] = $newData;
    }
    #返回購物車數(shù)據(jù)
    return $oldData;
  }
}
?>

2. 數(shù)據(jù)庫

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;

以上是“CodeIgniter框架如何實(shí)現(xiàn)購物車功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI