diff --git a/beike/Repositories/CartRepo.php b/beike/Repositories/CartRepo.php index a55d9484..e7a94458 100644 --- a/beike/Repositories/CartRepo.php +++ b/beike/Repositories/CartRepo.php @@ -24,17 +24,27 @@ class CartRepo } $customerId = $customer->id; $cart = Cart::query()->where('customer_id', $customerId)->first(); + $defaultAddress = AddressRepo::listByCustomer($customer)->first(); + $defaultAddressId = $defaultAddress->id ?? 0; + if (empty($cart)) { $shippingMethod = PluginRepo::getShippingMethods()->first(); $paymentMethod = PluginRepo::getPaymentMethods()->first(); - $defaultAddress = AddressRepo::listByCustomer($customer)->first(); $cart = Cart::query()->create([ 'customer_id' => $customerId, - 'shipping_address_id' => $defaultAddress->id ?? 0, + 'shipping_address_id' => $defaultAddressId, 'shipping_method_code' => $shippingMethod->code, - 'payment_address_id' => $defaultAddress->id ?? 0, + 'payment_address_id' => $defaultAddressId, 'payment_method_code' => $paymentMethod->code ]); + } else { + if ($cart->shipping_address_id == 0) { + $cart->shipping_address_id = $defaultAddressId; + } + if ($cart->payment_address_id == 0) { + $cart->payment_address_id = $defaultAddressId; + } + $cart->save(); } return $cart; }