|
|
@@ -28,7 +28,7 @@ class Cart
|
|
|
* 构造方法
|
|
|
*/
|
|
|
public function __construct($user)
|
|
|
- {
|
|
|
+ {
|
|
|
$this->user = $user;
|
|
|
$this->user_id = $user['user_id'];
|
|
|
static::$cart = Cache::get('cart_' . $this->user_id) ?: [];
|
|
|
@@ -100,15 +100,19 @@ class Cart
|
|
|
$this->delete($key);
|
|
|
continue;
|
|
|
}
|
|
|
- if($product['spec_type'] == 20){
|
|
|
+
|
|
|
+ // 购买数量
|
|
|
+ if ($product['spec_type'] == 20) {
|
|
|
// 商品单价
|
|
|
$product['product_price'] = $product['product_sku']['product_price'];
|
|
|
- // 购买数量
|
|
|
- $product['total_num'] = $item['product_num'];
|
|
|
- // 商品总价
|
|
|
- $product['total_price'] = bcmul($product['product_price'], $item['product_num'], 2);
|
|
|
}
|
|
|
|
|
|
+ // 购买数量
|
|
|
+ $product['total_num'] = $item['product_num'];
|
|
|
+
|
|
|
+ // 商品总价
|
|
|
+ $product['total_price'] = bcmul($product['product_price'], $item['product_num'], 2);
|
|
|
+
|
|
|
|
|
|
// 供应商
|
|
|
$product['shop_supplier_id'] = $item['shop_supplier_id'];
|
|
|
@@ -118,7 +122,7 @@ class Cart
|
|
|
}
|
|
|
|
|
|
$supplierIds = array_unique(helper::getArrayColumn($productList, 'shop_supplier_id'));
|
|
|
- foreach($supplierIds as $supplierId){
|
|
|
+ foreach ($supplierIds as $supplierId) {
|
|
|
$supplierData[] = [
|
|
|
'shop_supplier_id' => $supplierId,
|
|
|
'supplier' => SupplierModel::detail($supplierId),
|
|
|
@@ -132,33 +136,35 @@ class Cart
|
|
|
* 获取购物车中所有商品列表
|
|
|
* @return array|mixed
|
|
|
*/
|
|
|
- public function getCartProductList(){
|
|
|
- $carts = Cache::get('cart_products_' . $this->user_id)? : [];
|
|
|
- if($carts){
|
|
|
+ public function getCartProductList()
|
|
|
+ {
|
|
|
+ $carts = Cache::get('cart_products_' . $this->user_id) ?: [];
|
|
|
+ if ($carts) {
|
|
|
return $carts;
|
|
|
}
|
|
|
|
|
|
$cartList = $this->getList([]);
|
|
|
$carts = [];
|
|
|
- if($cartList){
|
|
|
- foreach ($cartList as $item){
|
|
|
- if($item['productList']){
|
|
|
- foreach ($item['productList'] as $v){
|
|
|
+ if ($cartList) {
|
|
|
+ foreach ($cartList as $item) {
|
|
|
+ if ($item['productList']) {
|
|
|
+ foreach ($item['productList'] as $v) {
|
|
|
$carts[$v['product_id']] = $v;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- if($carts){
|
|
|
+ if ($carts) {
|
|
|
Cache::set('cart_products_' . $this->user_id, $carts, rand(10, 30));
|
|
|
}
|
|
|
return $carts;
|
|
|
}
|
|
|
|
|
|
- private function getProductBySupplier($supplierId, $productList){
|
|
|
+ private function getProductBySupplier($supplierId, $productList)
|
|
|
+ {
|
|
|
$result = [];
|
|
|
- foreach ($productList as $product){
|
|
|
- if($product['shop_supplier_id'] == $supplierId){
|
|
|
+ foreach ($productList as $product) {
|
|
|
+ if ($product['shop_supplier_id'] == $supplierId) {
|
|
|
array_push($result, $product);
|
|
|
}
|
|
|
}
|
|
|
@@ -215,16 +221,16 @@ class Cart
|
|
|
}
|
|
|
|
|
|
// 是否是会员商品
|
|
|
- if(count($product['grade_ids']) > 0 && $product['grade_ids'][0] != ''){
|
|
|
- if(!in_array($this->user['grade_id'], $product['grade_ids'])){
|
|
|
+ if (count($product['grade_ids']) > 0 && $product['grade_ids'][0] != '') {
|
|
|
+ if (!in_array($this->user['grade_id'], $product['grade_ids'])) {
|
|
|
$this->setError('很抱歉,此商品仅特定会员可购买');
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
// 是否超过最大购买数
|
|
|
- if($product['limit_num'] > 0){
|
|
|
+ if ($product['limit_num'] > 0) {
|
|
|
$hasNum = OrderModel::getHasBuyOrderNum($this->user['user_id'], $product['product_id']);
|
|
|
- if($hasNum + $product['total_num'] > $product['limit_num']){
|
|
|
+ if ($hasNum + $product['total_num'] > $product['limit_num']) {
|
|
|
$this->error = "很抱歉,购买超过此商品最大限购数量";
|
|
|
return false;
|
|
|
}
|