<?php
defined('BASEPATH') OR
exit
('No direct script access allowed');
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('goodsModel','goods');
$this
->load->model('productModel','product');
$this
->load->model('goodsAttrModel','goodsAttr');
}
public
function
cartAdd()
{
#接收购物车提交数据
$this
->info =
$this
->input->post();
#1.验证商品库存、货品库存
$this
->checkGoodsNumber();
#2.查询规格名称、价格
$this
->getSpecData();
#3.组装购物车添加de数据
$cartData
=
$this
->setCartData();
p(json_decode(
$this
->input->cookie('cart'),true));
# 一、判断是否登录
if
(!UID){
$cookieCartData
=
$this
->input->cookie('cart');
if
(
empty
(
$cookieCartData
)){
$key
=
$cartData
['goods_id'].'-'.
$cartData
['product_id'];
$cookieCart
=
array
(
$key
=>
$cartData
);
$this
->setCartReturn(1,
$cartData
['goods_price']);
}
else
{
$cookieCart
=
$this
->addCartData(
$cartData
,json_decode(
$cookieCartData
,true));
$this
->setCartReturn(
count
(
$cookieCart
),
array_sum
(array_column(
$cookieCart
, 'goods_price')));
}
setCookie('cart',json_encode(
$cookieCart
),LEFT_TIME,'/');
}
else
{
}
$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;
}
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'));
# 返回规格信息
$this
->specData
}
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);
#若存在规格【添加规格信息】
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;
# 购物车 添加的总数据
$this
->cartData;
}
public
function
setCartReturn(
$number
,
$prices
)
{
$this
->msg['code'] = self::STATUS_ON;
$this
->msg['data'] =
array
(
'number' =>
$number
,
'prices' =>
$prices
,
);
}
public
function
addCartData(
$newData
,
$oldData
)
{
#组合Key
$key
=
$newData
['goods_id'].'-'.
$newData
['product_id'];
if
(isset(
$oldData
[
$key
])){
$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
{
$oldData
[
$key
] =
$newData
;
}
#返回购物车数据
return
$oldData
;
}
}
?>