where('user_id', $user_id) ->select(); if (empty($cartItems)) { throw new Exception("购物车数据不存在"); } $goods_list = []; foreach ($cartItems as $cart) { $goods_list[] = [ 'goods_id' => $cart->goods_id, 'goods_sku_id' => $cart->goods_sku_id, 'nums' => $cart->nums ]; } return $goods_list; } /** * @ DateTime 2021-05-31 * @ 获取购物车商品列表 * @param string $ids * @param integer $user_id * @param integer $sceneval * @return array */ public static function getGoodsList($ids, $user_id) { return Carts::field("c.*,GROUP_CONCAT(sp.name,':',sv.value order by sp.id asc) sku_attr") ->with([ 'Goods' => function ($query) { $query->where('status', 'normal'); }, 'Sku' ]) ->alias('c') ->join('shop_goods_sku sku', 'c.goods_sku_id=sku.id', 'LEFT') ->join('shop_goods_sku_spec p', "FIND_IN_SET(p.id,sku.sku_id)", 'LEFT') ->join('shop_spec sp', 'sp.id=p.spec_id', 'LEFT') ->join('shop_spec_value sv', 'sv.id=p.spec_value_id', 'LEFT') ->where(function ($query) use ($ids) { if ($ids) { $query->where('c.id', 'in', $ids); } }) ->where('c.user_id', $user_id) ->order('c.createtime desc') ->group('c.id') ->select(); } /** * 添加商品到购物车 * * @param string $goods_id 商品ID * @param string $goods_sku_id 商品SKUID * @param int $nums 数量 * @param int $user_id 会员ID * @return mixed */ public static function push($goods_id, $goods_sku_id, $nums = 1, $user_id = 0) { $row = Carts::where('goods_id', $goods_id) ->where('goods_sku_id', $goods_sku_id) ->where('user_id', $user_id) ->find(); //已存在,数量加 if ($row) { $row->setInc('nums', $nums); } else { $row = new Carts(); $row->save([ 'goods_id' => $goods_id, 'goods_sku_id' => $goods_sku_id, 'user_id' => $user_id, 'nums' => $nums ]); } return $row->id; } /** * 清空购物车 */ public static function clear($cart_ids) { Carts::where('id', 'IN', $cart_ids)->delete(); } }