|
@@ -35,17 +35,24 @@ class StoreCart extends Model
|
|
|
*/
|
|
*/
|
|
|
public function addToCart($data)
|
|
public function addToCart($data)
|
|
|
{
|
|
{
|
|
|
|
|
+ // 确保cart_num为整数
|
|
|
|
|
+ $cartNum = (int)($data['cart_num'] ?? 1);
|
|
|
|
|
+ if ($cartNum <= 0) {
|
|
|
|
|
+ $cartNum = 1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 检查是否已存在
|
|
// 检查是否已存在
|
|
|
$exist = $this->where('uid', $data['uid'])
|
|
$exist = $this->where('uid', $data['uid'])
|
|
|
->where('product_id', $data['product_id'])
|
|
->where('product_id', $data['product_id'])
|
|
|
->where('product_attr_unique', $data['product_attr_unique'] ?? '')
|
|
->where('product_attr_unique', $data['product_attr_unique'] ?? '')
|
|
|
->find();
|
|
->find();
|
|
|
if ($exist) {
|
|
if ($exist) {
|
|
|
- // 更新数量
|
|
|
|
|
- $result = $this->where('id', $exist['id'])->inc('cart_num', $data['cart_num'])->update();
|
|
|
|
|
|
|
+ // 更新数量,使用整数类型
|
|
|
|
|
+ $result = $this->where('id', $exist['id'])->inc('cart_num', $cartNum)->update();
|
|
|
return $result ? $exist['id'] : false;
|
|
return $result ? $exist['id'] : false;
|
|
|
} else {
|
|
} else {
|
|
|
// 新增
|
|
// 新增
|
|
|
|
|
+ $data['cart_num'] = $cartNum;
|
|
|
$result = $this->insert($data);
|
|
$result = $this->insert($data);
|
|
|
return $result ? $this->getLastInsID() : false;
|
|
return $result ? $this->getLastInsID() : false;
|
|
|
}
|
|
}
|