我目前正在寻找链接标题菜单中的主要类别链接直接到他们的第一个子类别的帮助。
换句话说:
单击类别链接时,我想在末尾添加第一个子类别的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>
欢迎使用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)
// >>> >>> >>> >>> >>> >>> >>> ^^^^^^^^^^^^^^^^^^^^^^^^
);
}
}
应该是这样。请注意,这种更简洁的方式需要每个类别多一个DB查询,因此在处理大量类别时可能会减慢站点速度(尽管只是一点点)。
更简单的方法 -只需要在header控制器中进行更改(不向模型添加新功能):
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%有效。