Вывести на главную категории с картинкой - не такая уж редкая задача. Как-то раньше я использовал модуль XDCategoryGroups, но надо настраивать - подгружать картинки и пр. А хочется - чтобы все работало проще - создал категорию и она тут же отобразилась. Также, как отображаются дочерние подкатегории. При этом еще нужно чтобы обычный модуль "категории" остался в системе.
Стоит отметить, что этот способ позволит вывести полный список категорий на любой странице сайта.
План работы:
- Настроим вывод модуля "категории"
- Создадим дополнительный шаблон для вывода категорий.
- Слегка подредактируем контроллер модуля "Категории"
catalog/controller/module/category.php
1. Настроим вывод модуля "категории"

2. Создадим дополнительный шаблон для вывода категорий.
Я его назвал category_top и поместил в папке рядом с оригинальным шаблоном catalog/view/theme/default/template/module/category.tpl
3. Слегка подредактируем контроллер
catalog/controller/module/category.php (Подсвечены участки, которые надо добавить/изменить)
language->load('module/category');
$this->data['heading_title'] = $this->language->get('heading_title');
if (isset($this->request->get['path'])) {
$parts = explode('_', (string)$this->request->get['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->load->model('tool/image');
$this->data['categories'] = array();
$categories = $this->model_catalog_category->getCategories(0);
//Показывать или нет количество товаров
$show_product_count = $this->config->get('config_product_count');
foreach ($categories as $category) {
//Будем вычислять кол-во товаров в категориях только если это кол-во надо показывать
$PIDs=array();
if ($show_product_count) {
$res = $this->model_catalog_product->getTotalProductsID(array('filter_category_id' => $category['category_id']));
foreach ($res as $key=>$value) {
$PIDs[$value['product_id']]=$value['product_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
);
$res = $this->model_catalog_product->getTotalProductsID($data);
$product_total=count($res);
foreach ($res as $key=>$value) {
$PIDs[$value['product_id']]=$value['product_id'];
}
// $total += count($PIDs);
}
$children_data[] = array(
'category_id' => $child['category_id'],
'name' => $child['name'] . ($show_product_count ? ' (' . $product_total . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']),
'thumb' => $this->model_tool_image->resize(($child['image']=='' ? 'no_image.jpg' : $child['image']), $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height')),
);
}
$total = count($PIDs);
$this->data['categories'][] = array(
'category_id' => $category['category_id'],
'name' => $category['name'] . ($show_product_count ? ' (' . $total . ')' : ''),
'children' => $children_data,
'href' => $this->url->link('product/category', 'path=' . $category['category_id']),
'thumb' => $this->model_tool_image->resize(($category['image']=='' ? 'no_image.jpg' : $category['image']), $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height')),
);
}
// Вообще тут еще было условие по поводу кастомного шаблона, но мне оно было не нужно и я его снес. Вы действуйте по своей ситуации.
if($setting['position'] == 'content_top'){
$this->template = 'default/template/module/category_top.tpl';
}else{
$this->template = 'default/template/module/category.tpl';
}
$this->render();
}
}
?>
Комментарии (4)
Не писать ответ