购物车的添加 展示


添加:

详情页接到的值:goos_id,spec_goods_id,number

控制层:

  public function cart(Request $request)
{
// 1. 接收数据

$params= input();
// 2. 验证

//加入购物车
CartLogic::addCart($params['goods_id'],$params['spec_goods_id'],$params['number']);

$goods = CartLogic::getGoodsWithSpec($params['spec_goods_id'], $params['goods_id']);
return view('addcart', ['goods' => $goods, 'number' => $params['number']]);


}

服务层:
<?php

namespace app\admin\logic;

use app\admin\model\Cart;
use think\Cookie;

class CartLogic
{
static function addCart($goods_id,$spec_goods_id,$number,$is_selected=1){

if(session('user_id')){
$user_id=session('user_id');
$where=[
'user_id'=>$user_id,
'goods_id'=>$goods_id,
'spec_goods_id'=>$spec_goods_id,
'number'=>$number
];
$info=Cart::where($where)->find();
if($info){
$info->number +=$number;
$info->is_selected=$is_selected;
$info->save();
}else{
$where['number']=$number;
$where['is_selected']=$is_selected;
Cart::create($where);
}

}else{
$data=\cookie('data')?:[];
$key=$goods_id.'_'.$spec_goods_id;
if(isset($data[$key])){
$data[$key]['number']+=$number;
$data[$key]['is_selected']+=$is_selected;

}else{
$data[$key]=[
'id'=>$key,
'user_id'=>'',
'spec_goods_id'=>$spec_goods_id,
'number'=>$number,
'is_selected'=>$is_selected
];
}
\cookie('cart',$data);
}
}

public static function getGoodsWithSpec($spec_goods_id, $goods_id)
{
//如果有sku的id, 就以之作为查询条件
if($spec_goods_id){
//有sku,且有多个,需要根据sku的id取指定的一个
$where = ['t2.id'=>$spec_goods_id];
}else{
//没有sku, 就根据商品id查询,sku相关的字段都是null
$where = ['t1.id' => $goods_id];
}
//如果没有,就以商品id作为查询条件
$goods = self::alias('t1')
->join('pyg_spec_goods t2', 't1.id=t2.goods_id', 'left')
->field('t1.*, t2.value_ids, t2.value_names, t2.price, t2.cost_price as cost_price2, t2.store_count')
->where($where)
->find();
// //如果sku信息中,price cost_price 大于0,则覆盖商品信息中的对应字段
if($goods['price'] > 0){
$goods['goods_price'] = $goods['price'];
}
if($goods['cost_price2'] > 0){
$goods['cost_price'] = $goods['cost_price2'];
}
return $goods;
}




}

模型层:需要购物车表



展示:需要 goods 和 spec_goods cart

控制层:

  public function index()
{
$getGoods= \app\goods\model\Cart::show();
// return json($getGoods);
if(!empty($getGoods['spec_goods'])){
if($getGoods['spec_goods'][0]['price'] > 0){
$getGoods['goods_price'] = $getGoods['spec_goods'][0]['price'];
}
if($getGoods['spec_goods'][0]['cost_price'] > 0){
$getGoods['cost_price'] = $getGoods['spec_goods'][0]['cost_price'];
}
if($getGoods['spec_goods'][0]['store_count'] > 0){
$getGoods['store_count'] = $getGoods['spec_goods'][0]['store_count'];
}else{
$goods['store_count'] = 0;
}
}
$this->assign('getGoods',$getGoods);
return view();
}

模型:

<?php

namespace app\goods\model;

use think\Model;

class Cart extends Model
{
static function show(){
return self::with(['goods','goods.specGoods'])->where('is_selected', 1)->select();
}

function goods(){
return $this->belongsTo('Goods','goods_id','id');
}

}

<?php

namespace app\goods\model;

use think\Model;

class Goods extends Model
{
static function getAll()
{
return self::paginate(10);
}


static function show($id)
{
return self::with('SpecGoods')->where('id',$id)->find();
}



function SpecGoods()
{
return $this->belongsTo('SpecGoods', 'id', 'goods_id');
}


}


 * 登录后将cookie购物车迁移到数据表
*/
public static function cookieToDb()
{
//从cookie中取出所有数据
$data = cookie('cart')?:[];
//将数据添加/修改到数据表
foreach($data as $v){
self::addCart($v['goods_id'], $v['spec_goods_id'], $v['number']);
}
//删除cookie购物车数据
cookie('cart', null);
}