作者 v_bairong06

购物车部分制作

... ... @@ -26,7 +26,9 @@ class AppframeController extends Controller {
// 一级分类列表
public function product() {
$where['parent'] = 0;
$product = M('Product')
->where($where)
->select();
$this->assign('product', $product);
}
... ...
<?php
/**
* Created by PhpStorm.
* User: 29925
* Date: 2018/5/1
* Time: 17:59
*/
namespace Common\Model;
use Common\Controller\AdminbaseController;
class CartModel extends AdminbaseController {
// 获取用户购物车列表
public function getListByUser($user_id) {
$where['user_id'] = $user_id;
return $this->where($where)->getField('goods_id',true);
}
}
\ No newline at end of file
... ...
<?php
/**
* Created by PhpStorm.
* User: 29925
* Date: 2018/5/1
* Time: 17:59
*/
namespace Portal\Controller;
use Common\Controller\HomebaseController;
class CartController extends HomebaseController {
protected $cart_model;
protected $goods_model;
protected $goods_brand_model;
protected $product_model;
function _initialize() {
parent::_initialize();
$this->cart_model = D("Common/Cart");
$this->goods_model = D("Common/Goods");
$this->goods_brand_model = D("Common/GoodsBrand");
$this->product_model = D("Common/Product");
}
/**
* 用户购物车列表
*/
public function index() {
$user_id = sp_get_current_userid();
if(!$user_id) {
}
}
}
\ No newline at end of file
... ...
... ... @@ -12,12 +12,14 @@ use Common\Controller\HomebaseController;
class GoodsController extends HomebaseController {
protected $cart_model;
protected $goods_model;
protected $goods_brand_model;
protected $product_model;
function _initialize() {
parent::_initialize();
$this->cart_model = D("Common/Cart");
$this->goods_model = D("Common/Goods");
$this->goods_brand_model = D("Common/GoodsBrand");
$this->product_model = D("Common/Product");
... ... @@ -51,4 +53,42 @@ class GoodsController extends HomebaseController {
$this->assign($info);
$this->display();
}
/**
* 加入购物车
*/
public function addCart() {
if(IS_AJAX) {
$user_id = sp_get_current_userid();
if(!$user_id) {
}
// 判断商品是否存在
$goods_id = I('post.id',0,'intval');
$info = $this->goods_model->getInfo($goods_id);
if(!$info) {
$this->ajaxReturn(array('status'=>false,'msg'=>'该商品不存在'));
}
$cartGoodsList = $this->cart_model->getListByUser($user_id);
if(in_array($goods_id,$cartGoodsList)) {
$result = $this->cart_model->where(array('user_id'=>$user_id,'goods_id'=>$goods_id))->setInc('num');
} else {
$cart['user_id'] = $user_id;
$cart['goods_id'] = $goods_id;
$cart['price'] = $info['goods_price'];
$cart['num'] = 1;
$cart['ctime'] = $cart['utime'] = time();
if(!$this->cart_model->create($cart)) {
$this->ajaxReturn(array('status'=>false,'msg'=>$this->cart_model->getError()));
}
$result = $this->cart_model->add($cart);
}
if(!$result) {
$this->ajaxReturn(array('status'=>false,'msg'=>'添加失败'));
}
$this->ajaxReturn(array('status'=>true,'msg'=>'添加成功'));
} else {
$this->error('非法操作');
}
}
}
\ No newline at end of file
... ...
... ... @@ -35,7 +35,7 @@
</div>
</div>
</div>
<notempty>
<notempty name="product_son">
<div class="filter-row filter-bg-row">
<div class="filter-key">详细分类</div>
<div class="filter-body">
... ...