fixed cart service

This commit is contained in:
Edward Yang 2022-07-04 16:45:13 +08:00
parent dfc22f4702
commit b648967524
2 changed files with 10 additions and 9 deletions

View File

@ -56,7 +56,7 @@ class CartService
if (empty($sku) || $quantity == 0) { if (empty($sku) || $quantity == 0) {
return null; return null;
} }
$cart = Cart::query() $cart = CartProduct::query()
->where('customer_id', $customerId) ->where('customer_id', $customerId)
->where('product_id', $productId) ->where('product_id', $productId)
->where('product_sku_id', $skuId) ->where('product_sku_id', $skuId)
@ -66,7 +66,7 @@ class CartService
$cart->selected = true; $cart->selected = true;
$cart->increment('quantity', $quantity); $cart->increment('quantity', $quantity);
} else { } else {
$cart = Cart::query()->create([ $cart = CartProduct::query()->create([
'customer_id' => $customerId, 'customer_id' => $customerId,
'product_id' => $productId, 'product_id' => $productId,
'product_sku_id' => $skuId, 'product_sku_id' => $skuId,
@ -86,11 +86,11 @@ class CartService
*/ */
public static function select($customer, $cartIds) public static function select($customer, $cartIds)
{ {
Cart::query()->where('customer_id', $customer->id)->update(['selected' => 0]); CartProduct::query()->where('customer_id', $customer->id)->update(['selected' => 0]);
if (empty($cartIds)) { if (empty($cartIds)) {
return; return;
} }
Cart::query()->where('customer_id', $customer->id) CartProduct::query()->where('customer_id', $customer->id)
->whereIn('id', $cartIds) ->whereIn('id', $cartIds)
->update(['selected' => 1]); ->update(['selected' => 1]);
} }
@ -104,7 +104,7 @@ class CartService
if (empty($cartId) || $quantity == 0) { if (empty($cartId) || $quantity == 0) {
return; return;
} }
Cart::query()->where('customer_id', $customer->id) CartProduct::query()->where('customer_id', $customer->id)
->where('id', $cartId) ->where('id', $cartId)
->update(['quantity' => $quantity, 'selected' => 1]); ->update(['quantity' => $quantity, 'selected' => 1]);
} }
@ -115,7 +115,7 @@ class CartService
if (empty($cartId)) { if (empty($cartId)) {
return; return;
} }
Cart::query()->where('customer_id', $customer->id) CartProduct::query()->where('customer_id', $customer->id)
->where('id', $cartId) ->where('id', $cartId)
->delete(); ->delete();
} }

View File

@ -1,17 +1,18 @@
<?php <?php
/** /**
* 创建购物车商品记录 * 创建购物车商品记录
* \Beike\Models\Cart::factory(300)->create(['customer_id'=>2]) * \Beike\Models\CartProduct::factory(300)->create(['customer_id'=>2])
*/ */
namespace Database\Factories\Beike\Models; namespace Database\Factories\Beike\Models;
use Beike\Models\Cart; use Beike\Models\Cart;
use Beike\Models\CartProduct;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
class CartFactory extends Factory class CartProductFactory extends Factory
{ {
protected $model = Cart::class; protected $model = CartProduct::class;
/** /**
* Define the model's default state. * Define the model's default state.