refactor: 调整新品页数据
This commit is contained in:
@@ -376,24 +376,45 @@ class Product extends Common
|
||||
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||
->select();
|
||||
if (!$products->isEmpty()) {
|
||||
// 按分类分组产品
|
||||
$products_map = [];
|
||||
foreach ($products as $product) {
|
||||
$products_map[$product['category_id']][] = $product;
|
||||
}
|
||||
// 获取产品分类信息
|
||||
$categorys = ProductCategoryModel::field(['id', 'name'])
|
||||
->byPks(array_keys($products_map))
|
||||
$categorys = ProductCategoryModel::field(['id', 'name', 'path', 'level'])
|
||||
->where(function($query) use($products) {
|
||||
$query->where('id', 'IN', Arr::pluck($products, 'category_id'))->whereOr('level', '=', 2);
|
||||
})
|
||||
->language($this->lang_id)
|
||||
->displayed(true)
|
||||
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||
->order(['sort' => 'asc', 'id' => 'asc'])
|
||||
->select();
|
||||
if (!$categorys->isEmpty()) {
|
||||
// 根据产品分类path信息分组
|
||||
$map = [];
|
||||
foreach ($categorys as $category) {
|
||||
$newpros[] = [
|
||||
'category' => $category,
|
||||
'products' => $products_map[$category['id']] ?? [],
|
||||
];
|
||||
$map[$category['id']] = $category;
|
||||
}
|
||||
$pro_map = [];
|
||||
foreach ($products as $pro) {
|
||||
if (!isset($map[$pro['category_id']])) {
|
||||
continue;
|
||||
}
|
||||
$m = $map[$pro['category_id']];
|
||||
$pro_map[$m['path']][] = $pro;
|
||||
}
|
||||
|
||||
// 获取二级分类下的产品信息
|
||||
foreach ($categorys as $val) {
|
||||
if ($val['level'] != 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($pro_map as $k => $pro) {
|
||||
if (in_array($val['id'], explode(',', $k))) {
|
||||
$newpros[] = [
|
||||
'category' => $val,
|
||||
'products' => $pro,
|
||||
];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user