diff --git a/beike/Exceptions/CartException.php b/beike/Exceptions/CartException.php new file mode 100644 index 00000000..d2f983cc --- /dev/null +++ b/beike/Exceptions/CartException.php @@ -0,0 +1,16 @@ + + * @created 2023-06-02 17:08:18 + * @modified 2023-06-02 17:08:18 + */ + +namespace Beike\Exceptions; + +class CartException extends \Exception +{ +} diff --git a/beike/Shop/Http/Controllers/CheckoutController.php b/beike/Shop/Http/Controllers/CheckoutController.php index 419eb7b7..1da89caa 100644 --- a/beike/Shop/Http/Controllers/CheckoutController.php +++ b/beike/Shop/Http/Controllers/CheckoutController.php @@ -11,6 +11,7 @@ namespace Beike\Shop\Http\Controllers; +use Beike\Exceptions\CartException; use Beike\Repositories\OrderRepo; use Beike\Shop\Services\CheckoutService; use Illuminate\Http\Request; @@ -24,8 +25,10 @@ class CheckoutController extends Controller $data = hook_filter('checkout.index.data', $data); return view('checkout', $data); - } catch (\Exception $e) { + } catch (CartException $e) { return redirect(shop_route('carts.index'))->withErrors(['error' => $e->getMessage()]); + } catch (\Exception $e) { + return redirect(shop_route('checkout.index'))->withErrors(['error' => $e->getMessage()]); } } diff --git a/beike/Shop/Services/CheckoutService.php b/beike/Shop/Services/CheckoutService.php index 22a866e7..095ee77c 100644 --- a/beike/Shop/Services/CheckoutService.php +++ b/beike/Shop/Services/CheckoutService.php @@ -11,10 +11,10 @@ namespace Beike\Shop\Services; +use Beike\Exceptions\CartException; use Beike\Libraries\Weight; use Beike\Models\Address; use Beike\Models\Country; -use Beike\Models\Customer; use Beike\Models\Order; use Beike\Models\Zone; use Beike\Repositories\AddressRepo; @@ -46,13 +46,11 @@ class CheckoutService if (is_int($customer) || empty($customer)) { $this->customer = current_customer(); } - // if (empty($this->customer) || !($this->customer instanceof Customer)) { - // // throw new \Exception(trans('shop/carts.invalid_customer')); - // } + $this->cart = CartRepo::createCart($this->customer); $this->selectedProducts = CartRepo::selectedCartProducts($this->customer->id ?? 0); if ($this->selectedProducts->count() == 0) { - throw new \Exception(trans('shop/carts.empty_selected_products')); + throw new CartException(trans('shop/carts.empty_selected_products')); } }