diff --git a/beike/Shop/Http/Controllers/CartController.php b/beike/Shop/Http/Controllers/CartController.php index 00e1160b..a976f2bc 100644 --- a/beike/Shop/Http/Controllers/CartController.php +++ b/beike/Shop/Http/Controllers/CartController.php @@ -18,7 +18,7 @@ class CartController extends Controller ->whereRelation('product', 'active', '=', true) ->findOrFail($skuId); - $cart = CartService::add($customer, $sku, $quantity); + $cart = CartService::add($sku, $quantity, $customer); return $cart; } diff --git a/beike/Shop/Services/CartService.php b/beike/Shop/Services/CartService.php index d9e79aa3..8e610a6f 100644 --- a/beike/Shop/Services/CartService.php +++ b/beike/Shop/Services/CartService.php @@ -28,12 +28,15 @@ class CartService } - public static function add(Customer $customer, ProductSku $sku, int $quantity) + public static function add($sku, int $quantity, $customer = null) { - $customerId = $customer->id; + $customerId = $customer->id ?? 0; $productId = $sku->product_id; $skuId = $sku->id; + if (empty($sku)) { + throw new \Exception("无效的SKU ID"); + } $cart = Cart::query() ->where('customer_id', $customerId) ->where('product_id', $productId)