Доброй ночи. Проблема такова. на главной стоит модуль categoryhome
Нужно было добавить подкатегории, но при их выводе - выводяться все категории, а не для каждой родительской категории отдельно. В чем может быть проблема ? Прикладываю контроллер и tpl модуля.
Контроллер:
<?php
class ControllerModulecategoryhome extends Controller {
protected $category_id = 0;
protected $path = array();
protected function index($setting) {
$this->language->load('module/categoryhome');
$this->data['heading_title'] = $this->language->get('heading_title');
$this->load->model('catalog/category');
$this->load->model('tool/image');
$url = '';
$this->data['categoryhome'] = $this->getCategories($this->config->get('categoryhome_category'));
$this->id = 'categoryhome';
if (isset($this->request->get['path'])) {
$parts = explode('_', $this->request->get['path']);
$this->category_id = end($this->path);
} else {
$parts = array();
}
if (isset($parts[0])) {
$this->data['category_id'] = $parts[0];
} else {
$this->data['category_id'] = 0;
}
if (isset($parts[1])) {
$this->data['child_id'] = $parts[1];
} else {
$this->data['child_id'] = 0;
}
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$this->data['categories'] = array();
$categories = $this->model_catalog_category->getCategories(0);
//Показывать или нет количество товаров
//$show_product_count = $this->config->get('config_product_count');
foreach ($categories as $category) {
//Будем вычислять кол-во товаров в категориях только если это кол-во надо показывать
//if ($show_product_count) {
// $total = $this->model_catalog_product->getTotalProducts(array('filter_category_id' => $category['category_id']));
//}
$children_data = array();
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach ($children as $child) {
//Будем вычислять кол-во товаров в категориях только если это кол-во надо показывать
//if ($show_product_count) {
// $data = array(
// 'filter_category_id' => $child['category_id'],
// 'filter_sub_category' => true
// );
//
// $product_total = $this->model_catalog_product->getTotalProducts($data);
//
// $total += $product_total;
//}
$children_data[] = array(
'category_id' => $child['category_id'],
'name' => $child['name'],
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
);
}
$this->data['categories'][] = array(
'category_id' => $category['category_id'],
'name' => $category['name'],
'children' => $children_data,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
}
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/categoryhome.tpl')) {
$this->template = $this->config->get('config_template') . '/template/module/categoryhome.tpl';
} else {
$this->template = 'default/template/module/categoryhome.tpl';
}
$this->render();
}
protected function getCategories($parent_id, $current_path = '') {
$categoryhome = array();
$category_id = array_shift($this->path);
$results = $this->model_catalog_category->getCategories($parent_id);
$i=0;
foreach ($results as $result) {
if (!$current_path)
{
$new_path = $result['category_id'];
}
else
{
$new_path = $current_path . '_' . $result['category_id'];
}
if ($this->category_id == $result['category_id']) {
$categoryhome[$i]['href'] = $this->url->link('product/category', 'path=' . $new_path);
} else {
$categoryhome[$i]['href'] = $this->url->link('product/category', 'path=' . $new_path);
}
if ($result['image']) {
$image = $result['image'];
} else {
$image = 'no_image.jpg';
}
$categoryhome[$i]['thumb'] = $this->model_tool_image->resize($image, 150, 150);
$categoryhome[$i]['name'] = $result['name'];
$i++;
}
return $categoryhome;
}
}
?>
и tpl:
<div class="box">
<div class="box-content">
<div class="box-cat">
<?php foreach ($categoryhome as $categoryhome) { ?>
<div>
<div class="image"><a href="<?php echo $categoryhome['href']; ?>"><img src="<?php echo $categoryhome['thumb']; ?>" title="<?php echo $categoryhome['name']; ?>" alt="<?php echo $categoryhome['name']; ?>" /></a></div>
<div class="name"><a href="<?php echo $categoryhome['href']; ?>"><?php echo $categoryhome['name']; ?></a></div>
<?php foreach ($categories as $category) { ?>
<?php if ($category['children']) { ?>
<?php foreach ($category['children'] as $child) { ?>
<?php if ($child['category_id'] == $child_id) { ?>
<a href="<?php echo $child['href']; ?>"> - <?php echo $child['name']; ?></a>
<?php } else { ?>
<a href="<?php echo $child['href']; ?>"> - <?php echo $child['name']; ?></a>
<?php } ?>
<?php } ?>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
</div>
</div>
</div>