我目前正在寻找帮助,将标题菜单中的主要类别链接直接链接到他们的第一个子类别。
换句话说:
点击类别链接时,我想在最后添加第一个子类别的ID,即
route=product/category&path=18_59
而不是
route=product/category&path=18
有没有人建议如何做到这一点?
<div id="menu">
<ul>
<?php foreach ($categories as $category) { ?>
<li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
<?php if ($category['children']) { ?>
<div>
<?php for ($i = 0; $i < count($category['children']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['children'][$i])) { ?>
<li><a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?></a></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
</div>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>
答案 0 :(得分:1)
欢迎使用 StackOverflow !
这可以通过获得主要类别的第一个孩子来实现,我们有两个选择,一个更清洁,一个更容易。
清洁者。这需要catalog/controller/common/header.php
和模型catalog/model/catalog/category.php
中的更改,让我们一步一步地进行。首先,将新的必要功能添加到类别模型中:
public function getCategoryFirstChildId($category_id) {
$query = $this->db->query('SELECT category_id FROM '. DB_PREFIX .'category WHERE parent_id = '. (int)$category_id .' ORDER BY category_id ASC LIMIT 1');
return $query->row['category_id'];
}
此函数将获取parent_id
为$category_id
作为参数的所有类别,按category_id
升序排序(您可以将其更改为您想要的任何排序)并返回只有第一个。
现在让我们转到控制器 - 我们将编辑加载类别的部分:
foreach ($categories as $category) {
if ($category['top']) {
// Level 2
$children_data = array();
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach ($children as $child) {
$data = array(
'filter_category_id' => $child['category_id'],
'filter_sub_category' => true
);
$product_total = $this->model_catalog_product->getTotalProducts($data);
$children_data[] = array(
'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
);
}
/*NEW =>*/ $first_child_id = $this->model_catalog_category->getCategoryFirstChildId($category['category_id']);
// Level 1
$this->data['categories'][] = array(
'name' => $category['name'],
'children' => $children_data,
'column' => $category['column'] ? $category['column'] : 1,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $first_child_id)
// >>> >>> >>> >>> >>> >>> >>> ^^^^^^^^^^^^^^^^^^^^^^^^
);
}
}
这应该是它。明智的是,这种更清洁的方式每个类别需要多一个数据库查询,因此在处理大量类别时可能会减慢(尽管一点点)网站。
更简单的方法 - 只需要在标头控制器内完成更改(不会向模型添加新功能):
foreach ($categories as $category) {
if ($category['top']) {
// Level 2
$children_data = array();
/*NEW =>*/ $first_child_id = 0;
/*NEW =>*/ $first_child = true;
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach ($children as $child) {
/*NEW =>*/ if($first_child) {
/*NEW =>*/ $first_child = false;
/*NEW =>*/ $first_child_id = $child['category_id'];
/*NEW =>*/ }
$data = array(
'filter_category_id' => $child['category_id'],
'filter_sub_category' => true
);
$product_total = $this->model_catalog_product->getTotalProducts($data);
$children_data[] = array(
'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
);
}
// Level 1
$this->data['categories'][] = array(
'name' => $category['name'],
'children' => $children_data,
'column' => $category['column'] ? $category['column'] : 1,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $first_child_id)
// >>> >>> >>> >>> >>> >>> >>> ^^^^^^^^^^^^^^^^^^^^^^^^
);
}
}
更简单的方法也可以很好地运行并且会更快一点但是它将指向的第一个类别将始终是按sort_order
排序升序时的第一个类别 - 每当它可能发生变化时,主要类别将指向不同的子类别......
我没有测试这两种方式,但我相信它们会100%工作。