diff --git a/beike/Repositories/CartRepo.php b/beike/Repositories/CartRepo.php new file mode 100644 index 00000000..966d2139 --- /dev/null +++ b/beike/Repositories/CartRepo.php @@ -0,0 +1,32 @@ + + * @created 2022-07-04 17:14:14 + * @modified 2022-07-04 17:14:14 + */ + +namespace Beike\Repositories; + +use Beike\Models\Cart; + +class CartRepo +{ + public static function createCart(int $customerId) + { + $cart = Cart::query()->where('customer_id', $customerId)->first(); + if (empty($cart)) { + $cart = Cart::query()->create([ + 'customer_id' => $customerId, + 'shipping_address_id' => 0, + 'shipping_method_code' => '', + 'payment_address_id' => 0, + 'payment_method_code' => '' + ]); + } + return $cart; + } +} diff --git a/beike/Shop/Services/CheckoutService.php b/beike/Shop/Services/CheckoutService.php index 5148528c..3eaf846a 100644 --- a/beike/Shop/Services/CheckoutService.php +++ b/beike/Shop/Services/CheckoutService.php @@ -13,6 +13,7 @@ namespace Beike\Shop\Services; use Beike\Models\Cart; use Beike\Models\Customer; +use Beike\Repositories\CartRepo; use Beike\Repositories\PluginRepo; use Beike\Repositories\AddressRepo; use Beike\Repositories\CountryRepo; @@ -29,12 +30,10 @@ class CheckoutService if (is_int($customer) || empty($customer)) { $this->customer = current_customer(); } - if (empty($this->customer) && !($this->customer instanceof Customer)) { + if (empty($this->customer) || !($this->customer instanceof Customer)) { throw new \Exception("购物车客户无效"); } - $this->cart = Cart::query() - ->where('customer_id', $customer->id) - ->firstOrCreate(); + $this->cart = CartRepo::createCart($this->customer->id); } /** diff --git a/database/migrations/2022_07_04_065314_add_cart_item_orders.php b/database/migrations/2022_07_04_065314_add_cart_item_orders.php index 2c1a4ef0..758a1c1d 100644 --- a/database/migrations/2022_07_04_065314_add_cart_item_orders.php +++ b/database/migrations/2022_07_04_065314_add_cart_item_orders.php @@ -19,9 +19,9 @@ class AddCartItemOrders extends Migration $table->id(); $table->integer('customer_id'); $table->integer('shipping_address_id'); - $table->integer('shipping_method_code'); + $table->string('shipping_method_code'); $table->integer('payment_address_id'); - $table->integer('payment_method_code'); + $table->string('payment_method_code'); $table->timestamps(); });