删除购物车不可用产品
This commit is contained in:
parent
09f5acef41
commit
e6143629ff
|
|
@ -25,4 +25,9 @@ class CartProduct extends Model
|
||||||
{
|
{
|
||||||
return $this->belongsTo(ProductSku::class, 'product_sku_id', 'id');
|
return $this->belongsTo(ProductSku::class, 'product_sku_id', 'id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function product(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Product::class, 'product_id', 'id');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,13 +31,23 @@ class CartService
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
$cartBuilder = CartProduct::query()
|
$cartBuilder = CartProduct::query()
|
||||||
->with(['sku.product.description'])
|
->with(['product', 'sku.product.description'])
|
||||||
->where('customer_id', $customer->id)
|
->where('customer_id', $customer->id)
|
||||||
->orderByDesc('id');
|
->orderByDesc('id');
|
||||||
if ($selected) {
|
if ($selected) {
|
||||||
$cartBuilder->where('selected', true);
|
$cartBuilder->where('selected', true);
|
||||||
}
|
}
|
||||||
$cartItems = $cartBuilder->get();
|
$cartItems = $cartBuilder->get();
|
||||||
|
|
||||||
|
$cartItems = $cartItems->filter(function ($item) {
|
||||||
|
$description = $item->sku->product->description ?? '';
|
||||||
|
$product = $item->product ?? null;
|
||||||
|
if (empty($description) || empty($product)) {
|
||||||
|
$item->delete();
|
||||||
|
}
|
||||||
|
return $description && $product;
|
||||||
|
});
|
||||||
|
|
||||||
$cartList = CartList::collection($cartItems)->jsonSerialize();
|
$cartList = CartList::collection($cartItems)->jsonSerialize();
|
||||||
return $cartList;
|
return $cartList;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,6 @@ class CreateCustomerTable extends Migration
|
||||||
$table->string('avatar')->default('');
|
$table->string('avatar')->default('');
|
||||||
$table->unsignedInteger('customer_group_id');
|
$table->unsignedInteger('customer_group_id');
|
||||||
$table->string('locale', 10);
|
$table->string('locale', 10);
|
||||||
$table->text('cart')->nullable();
|
|
||||||
$table->tinyInteger('status')->default(0);
|
$table->tinyInteger('status')->default(0);
|
||||||
$table->string('code', 40)->default('');
|
$table->string('code', 40)->default('');
|
||||||
$table->string('from', 16)->default('');
|
$table->string('from', 16)->default('');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue