生成订单后删除购物车数据

This commit is contained in:
Edward Yang 2022-07-05 10:22:15 +08:00
parent e6143629ff
commit b68c1e91c5
2 changed files with 19 additions and 3 deletions

View File

@ -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();
}
}

View File

@ -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;