fixed checkout

This commit is contained in:
Edward Yang 2022-07-04 17:20:38 +08:00
parent 00ede7adcd
commit e086401619
3 changed files with 37 additions and 6 deletions

View File

@ -0,0 +1,32 @@
<?php
/**
* CartRepo.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @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;
}
}

View File

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

View File

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