diff --git a/beike/Repositories/CartRepo.php b/beike/Repositories/CartRepo.php index 0514963a..b16e48fd 100644 --- a/beike/Repositories/CartRepo.php +++ b/beike/Repositories/CartRepo.php @@ -12,12 +12,17 @@ namespace Beike\Repositories; use Beike\Models\Cart; +use Beike\Models\CartProduct; +use Beike\Models\Customer; class CartRepo { - public static function createCart(int $customerId) + public static function createCart($customer) { - $customer = CustomerRepo::find($customerId); + if (is_numeric($customer)) { + $customer = Customer::query()->find($customer); + } + $customerId = $customer->id; $cart = Cart::query()->where('customer_id', $customerId)->first(); if (empty($cart)) { $shippingMethod = PluginRepo::getShippingMethods()->first(); @@ -33,4 +38,14 @@ class CartRepo } return $cart; } + + public static function clearSelectedCartProducts($customer) + { + if (is_numeric($customer)) { + $customer = Customer::query()->find($customer); + } + $customerId = $customer->id; + Cart::query()->where('customer_id', $customerId)->delete(); + CartProduct::query()->where('customer_id', $customerId)->where('selected', true)->delete(); + } } diff --git a/beike/Shop/Services/CheckoutService.php b/beike/Shop/Services/CheckoutService.php index 1f574c85..bd3e6433 100644 --- a/beike/Shop/Services/CheckoutService.php +++ b/beike/Shop/Services/CheckoutService.php @@ -36,7 +36,7 @@ class CheckoutService if (empty($this->customer) || !($this->customer instanceof Customer)) { throw new \Exception("购物车客户无效"); } - $this->cart = CartRepo::createCart($this->customer->id); + $this->cart = CartRepo::createCart($this->customer); } /** @@ -80,6 +80,7 @@ class CheckoutService $data['customer'] = $customer; $data['checkout'] = self::checkoutData(); $order = OrderRepo::create($data); + CartRepo::clearSelectedCartProducts($customer); // Notification::endmail(); // Notification::sendsms(); return $order;