You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.3 KiB
86 lines
2.3 KiB
<?php
|
|
|
|
namespace app\mall\service;
|
|
|
|
use app\mall\model\MallCart;
|
|
|
|
class Cart extends Base
|
|
{
|
|
|
|
/**
|
|
* 获取用户购物车现有有效和无效产品列表
|
|
* @date 2022-08-25
|
|
*/
|
|
public function listUserCartEffectiveAndInvalid()
|
|
{
|
|
$cart_model = new MallCart();
|
|
|
|
// 获取用户购物车商品
|
|
$cart_list = $cart_model->listUserCart();
|
|
|
|
// 购物车有效商品
|
|
$cart_effective = [];
|
|
// 购物车无效商品
|
|
$cart_invalid = [];
|
|
|
|
// 拆分有效和无效商品
|
|
foreach ($cart_list as $k => $v) {
|
|
unset($cart_list[$k]['freight_type']);
|
|
unset($cart_list[$k]['freight_id']);
|
|
unset($cart_list[$k]['freight_money']);
|
|
unset($cart_list[$k]['weight']);
|
|
unset($cart_list[$k]['product_is_publish']);
|
|
unset($cart_list[$k]['product_delete_time']);
|
|
if ($v['is_effective'] == 1) {
|
|
$cart_effective[] = $v;
|
|
} else {
|
|
$cart_invalid[] = $v;
|
|
}
|
|
}
|
|
|
|
return [$cart_effective, $cart_invalid];
|
|
}
|
|
|
|
/**
|
|
* 判断购物车失效商品并修改状态
|
|
* @date 2022-08-25
|
|
*/
|
|
public function updateInvalid()
|
|
{
|
|
$cart_model = new MallCart();
|
|
|
|
$cart = $cart_model->getAllCart([
|
|
['user_id', '=', $this->userId]
|
|
], 'id,product_id,is_product_spec_open,product_sku,number,is_select');
|
|
|
|
if (count($cart)) {
|
|
foreach ($cart as $k => $v) {
|
|
// 1--有效 0--失效
|
|
$is_effective = 1;
|
|
// 原产品删除或下架
|
|
if (empty($v['product_name']) || !empty($v['product_delete_time']) || $v['product_is_publish'] == 0) {
|
|
$is_effective = 0;
|
|
}
|
|
// SKU更改
|
|
if ($v['is_product_spec_open'] == 1 && empty($v['product_sku_name'])) {
|
|
$is_effective = 0;
|
|
}
|
|
// 库存不足
|
|
if ($v['product_stock'] <= 0) {
|
|
$is_effective = 0;
|
|
}
|
|
$data[] = [
|
|
'id' => $v['id'],
|
|
'is_effective' => $is_effective
|
|
];
|
|
}
|
|
|
|
return $cart_model->saveAll($data);
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|