From b68c1e91c52908d2963e17a85b126961dcc4db04 Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Tue, 5 Jul 2022 10:22:15 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E6=88=90=E8=AE=A2=E5=8D=95=E5=90=8E?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=B4=AD=E7=89=A9=E8=BD=A6=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- beike/Repositories/CartRepo.php | 19 +++++++++++++++++-- beike/Shop/Services/CheckoutService.php | 3 ++- 2 files changed, 19 insertions(+), 3 deletions(-) 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;