fixed cart
This commit is contained in:
parent
d421521dbf
commit
4872dc48f9
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Beike\Models\AdminUser;
|
||||
use Beike\Models\Customer;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
|
||||
|
|
@ -46,11 +47,21 @@ function shop_route($route, $params = []): string
|
|||
*
|
||||
* @return Authenticatable|null
|
||||
*/
|
||||
function logged_admin_user(): ?Authenticatable
|
||||
function current_user(): ?Authenticatable
|
||||
{
|
||||
return auth()->guard(AdminUser::AUTH_GUARD)->user();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前台当前登录客户
|
||||
*
|
||||
* @return Authenticatable|null
|
||||
*/
|
||||
function current_customer(): ?Authenticatable
|
||||
{
|
||||
return auth()->guard(Customer::AUTH_GUARD)->user();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缩略图
|
||||
*
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@
|
|||
namespace Beike\Shop\Http\Controllers;
|
||||
|
||||
use Beike\Models\ProductSku;
|
||||
use Beike\Services\CartService;
|
||||
use Illuminate\Http\Request;
|
||||
use Beike\Shop\Services\CartService;
|
||||
|
||||
class CartController extends Controller
|
||||
{
|
||||
|
|
@ -12,13 +12,20 @@ class CartController extends Controller
|
|||
{
|
||||
$skuId = $request->sku_id;
|
||||
$quantity = $request->quantity ?? 1;
|
||||
$customer = current_customer();
|
||||
|
||||
$sku = ProductSku::query()
|
||||
->whereRelation('product', 'active', '=', true)
|
||||
->findOrFail($skuId);
|
||||
|
||||
$cart = (new CartService)->add($sku, $quantity);
|
||||
$cart = CartService::add($customer, $sku, $quantity);
|
||||
|
||||
return $cart;
|
||||
}
|
||||
|
||||
public function miniCart()
|
||||
{
|
||||
$customer = current_customer();
|
||||
return CartService::list($customer);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ Route::prefix('/')
|
|||
Route::get('/', [HomeController::class, 'index'])->name('home.index');
|
||||
|
||||
Route::post('carts', [CartController::class, 'store'])->name('carts.store');
|
||||
Route::get('carts/mini', [CartController::class, 'miniCart'])->name('carts.mini');
|
||||
|
||||
Route::get('categories', [CategoryController::class, 'index'])->name('categories.index');
|
||||
Route::get('categories/{category}', [CategoryController::class, 'show'])->name('categories.show');
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
* @modified 2022-01-05 10:12:57
|
||||
*/
|
||||
|
||||
namespace Beike\Services;
|
||||
namespace Beike\Shop\Services;
|
||||
|
||||
|
||||
use Beike\Models\Cart;
|
||||
|
|
@ -17,10 +17,17 @@ use Beike\Models\ProductSku;
|
|||
|
||||
class CartService
|
||||
{
|
||||
public function add(ProductSku $sku, int $quantity)
|
||||
public static function list($customer)
|
||||
{
|
||||
$cartList = Cart::query()->where('customer_id', $customer->id)->get();
|
||||
return $cartList;
|
||||
}
|
||||
|
||||
|
||||
public static function add($customer, ProductSku $sku, int $quantity)
|
||||
{
|
||||
$cart = Cart::query()->create([
|
||||
'customer_id' => 0,
|
||||
'customer_id' => $customer->id,
|
||||
'product_id' => $sku->product_id,
|
||||
'product_sku_id' => $sku->id,
|
||||
'quantity' => $quantity,
|
||||
|
|
|
|||
Loading…
Reference in New Issue