优化获取购物车总价和商品数量的方法
This commit is contained in:
parent
96f687cb5f
commit
ec72e0dd0a
@ -63,22 +63,10 @@ class LocalStoreGoodsCartLogic extends Logic
|
||||
// 获取对应的购物车商品
|
||||
public static function getCartGoods($post, $user_id)
|
||||
{
|
||||
$data = [];
|
||||
$cart = LocalStoreGoodsCart::alias('c')
|
||||
->join('local_store_goods g', 'c.lsg_id = g.id')
|
||||
->where(['c.user_id' => $user_id, 'c.lstore_id' => $post['lstore_id'], 'c.is_delete' => 0, 'g.state' => 1, 'g.audit_status' => 1, 'g.delete_time' => 0])
|
||||
->field('c.id, c.item_id, c.goods_num, g.id as lsg_id, g.title, g.price')
|
||||
->append(['pic', 'item'])
|
||||
->select()
|
||||
->toArray();
|
||||
$result = self::selfCart($user_id, $post['lstore_id']);
|
||||
|
||||
$price = 0;
|
||||
foreach ($cart as &$v) {
|
||||
$price += ($v['goods_num'] * $v['price']);
|
||||
}
|
||||
|
||||
$data['total_amount'] = number_format($price, 2, '.', '');
|
||||
$data['list'] = $cart;
|
||||
$data['total_amount'] = $result['total_amount'];
|
||||
$data['list'] = $result['cart'];
|
||||
|
||||
return $data;
|
||||
}
|
||||
@ -114,20 +102,7 @@ class LocalStoreGoodsCartLogic extends Logic
|
||||
'goods_num' => $post['goods_num']
|
||||
]);
|
||||
|
||||
$result = LocalStoreGoodsCart::alias('c')
|
||||
->join('local_store_goods g', 'c.lsg_id = g.id')
|
||||
->where(['c.user_id' => $user_id, 'c.lstore_id' => $cart['lstore_id'], 'c.is_delete' => 0, 'g.state' => 1, 'g.audit_status' => 1, 'g.delete_time' => 0])
|
||||
->field('c.id, c.item_id, c.goods_num, g.id as lsg_id, g.title, g.price')
|
||||
->append(['pic', 'item'])
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$price = 0;
|
||||
foreach ($result as &$v) {
|
||||
$price += ($v['goods_num'] * $v['price']);
|
||||
}
|
||||
|
||||
$price = number_format($price, 2, '.', '');
|
||||
$result = self::selfCart($user_id, $cart['lstore_id']);
|
||||
} else {
|
||||
throw new \Exception('商品信息不存在');
|
||||
}
|
||||
@ -136,7 +111,7 @@ class LocalStoreGoodsCartLogic extends Logic
|
||||
}
|
||||
Db::commit();
|
||||
return [
|
||||
'total_amount' => $price,
|
||||
'total_amount' => $result['total_amount'],
|
||||
];
|
||||
} catch (Exception $e) {
|
||||
Db::rollback();
|
||||
@ -170,31 +145,42 @@ class LocalStoreGoodsCartLogic extends Logic
|
||||
]);
|
||||
}
|
||||
|
||||
$result = LocalStoreGoodsCart::alias('c')
|
||||
->join('local_store_goods g', 'c.lsg_id = g.id')
|
||||
->where(['c.user_id' => $user_id, 'c.lstore_id' => $post['lstore_id'], 'c.is_delete' => 0, 'g.state' => 1, 'g.audit_status' => 1, 'g.delete_time' => 0])
|
||||
->field('c.id, c.item_id, c.goods_num, g.id as lsg_id, g.title, g.price')
|
||||
->append(['pic', 'item'])
|
||||
->select()
|
||||
->toArray();
|
||||
$cart = self::selfCart($user_id, $post['lstore_id']);
|
||||
|
||||
$price = $goods_num = 0;
|
||||
foreach ($result as &$v) {
|
||||
$price += ($v['goods_num'] * $v['price']);
|
||||
$goods_num += $v['goods_num'];
|
||||
}
|
||||
$price = number_format($price, 2, '.', '');
|
||||
Db::commit();
|
||||
|
||||
return [
|
||||
'total_amount' => $price,
|
||||
'goods_num' => $goods_num
|
||||
'total_amount' => $cart['total_amount'],
|
||||
'goods_num' => $cart['goods_num']
|
||||
];
|
||||
} catch (Exception $e) {
|
||||
Db::rollback();
|
||||
dump($e->getMessage());die;
|
||||
self::$error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 计算个人购物车商品总价和总数
|
||||
private static function selfCart($user_id, $lstore_id)
|
||||
{
|
||||
$total_amount = $goods_num = 0;
|
||||
|
||||
$cart = LocalStoreGoodsCart::alias('c')
|
||||
->join('local_store_goods g', 'c.lsg_id = g.id')
|
||||
->where(['c.user_id' => $user_id, 'c.lstore_id' => $lstore_id, 'c.is_delete' => 0, 'g.state' => 1, 'g.audit_status' => 1, 'g.delete_time' => 0])
|
||||
->field('c.id, c.item_id, c.goods_num, g.id as lsg_id, g.title, g.price')
|
||||
->append(['pic', 'item'])
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
foreach ($cart as &$v) {
|
||||
$total_amount += ($v['goods_num'] * $v['price']);
|
||||
$goods_num += $v['goods_num'];
|
||||
}
|
||||
|
||||
return [
|
||||
'cart' => $cart,
|
||||
'total_amount' => $total_amount, // 购物车商品总价
|
||||
'goods_num' => $goods_num // 购物车商品总数
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user