Reputation: 504
This may be a duplicate, but which ever answer I found, didn't solve my problem. I have category structure like this: Category->Subcategory->Sub sub category. In my category module, I managed to show the whole structure (Category->Subcategory->Subsub category). But what I need is only Subsub categories to show for each Category or subcategory. My category.php
module file looks like this:
<?php
class ControllerModuleCategory extends Controller {
public function index() {
$this->load->language('module/category');
$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])) {
$data['category_id'] = $parts[0];
} else {
$data['category_id'] = 0;
}
if (isset($parts[1])) {
$data['child_id'] = $parts[1];
} else {
$data['child_id'] = 0;
}
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$data['categories'] = array();
$categories = $this->model_catalog_category->getCategories(0);
foreach ($categories as $category) {
$children_data = array();
if ($category['category_id'] == $data['category_id']) {
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach($children as $child) {
$children_data_2 = array();
$children_2 = $this->model_catalog_category->getCategories($child['category_id']);
foreach ($children_2 as $child_2) {
$filter_data = array(
'filter_category_id' => $child_2['category_id'],
'filter_sub_category' => true
);
$children_data_2[] = array(
'category_id' => $child_2['category_id'],
'name' => $child_2['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'] . '_' . $child_2['category_id'])
);
}
$filter_data = array('filter_category_id' => $child['category_id'], 'filter_sub_category' => true);
$children_data[] = array(
'category_id' => $child['category_id'],
'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']),
'children' => $children_data_2 // insert this line
);
}
}
$filter_data = array(
'filter_category_id' => $category['category_id'],
'filter_sub_category' => true
);
$data['categories'][] = array(
'category_id' => $category['category_id'],
'name' => $category['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
'children' => $children_data,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
}
return $this->load->view('module/category', $data);
}
}
My category.tpl
module file looks like this:
<h3><?php echo "Potkategorije" ?></h3>
<div class="list-group">
<ul>
<?php foreach ($categories as $category) { ?>
<?php if ($category['category_id'] == $category_id) { ?>
<li class="cat-active">
<a href="<?php echo $category['href']; ?>" class="active"><?php echo $category['name']; ?></a>
<?php } else { ?>
<li>
<a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
<?php } ?>
<?php if ($category['children']) { ?>
<b class="cc"></b>
<ul class="col-subcat">
<?php foreach ($category['children'] as $child) { ?>
<li>
<?php if ($child['category_id'] == $child_id) { ?>
<a href="<?php echo $child['href']; ?>" class="active"><?php echo $child['name']; ?></a>
<?php } else { ?>
<a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a>
<?php } ?>
<?php if ($child['children']) { ?>
<b class="cc"></b>
<ul class="col-subcat">
<?php foreach ($child['children'] as $child_2) { ?>
<li>
<?php if ($child_2['category_id'] == $child_id) { ?>
<a href="<?php echo $child_2['href']; ?>" class="active"><?php echo $child_2['name']; ?></a>
<?php } else { ?>
<a href="<?php echo $child_2['href']; ?>"><?php echo $child_2['name']; ?></a>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>
How to modify files to make this work? I appreciate your time, thanx.
Upvotes: 1
Views: 555
Reputation: 504
Thanx to a good man @Ahmed Ginani, who tried to help me, I relized what was my problem. I never defined current subcategory. Instead, I went on showing the whole structure all at once. So, I had to modify both of my files (php and tpl) so that only subsubcategories for the current category/subcategory would display. I am showing the correct files here.
Category.php:
<?php
class ControllerModuleCategory extends Controller {
public function index() {
$this->load->language('module/category');
$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])) {
$data['category_id'] = $parts[0];
} else {
$data['category_id'] = 0;
}
if (isset($parts[1])) {
$data['child_id'] = $parts[1];
} else {
$data['child_id'] = 0;
}
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$data['categories'] = array();
$categories = $this->model_catalog_category->getCategories(0);
foreach ($categories as $category) {
$children_data = array();
if ($category['category_id'] == $data['category_id']) {
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach($children as $child) {
$children_data_2 = array();
$children_2 = $this->model_catalog_category->getCategories($child['category_id']);
foreach ($children_2 as $child_2) {
$filter_data = array(
'filter_category_id' => $child_2['category_id'],
'filter_sub_category' => true
);
$children_data_2[] = array(
'category_id' => $child_2['category_id'],
'name' => $child_2['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'] . '_' . $child_2['category_id']),
);
}
$filter_data = array('filter_category_id' => $child['category_id'], 'filter_sub_category' => true);
$children_data[] = array(
'category_id' => $child['category_id'],
'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']),
'children' => $children_data_2 // insert this line
);
}
}
$filter_data = array(
'filter_category_id' => $category['category_id'],
'filter_sub_category' => true
);
$data['categories'][] = array(
'category_id' => $category['category_id'],
'name' => $category['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
'children' => $children_data,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
}
return $this->load->view('module/category', $data);
}
}
Category.tpl:
<div class="list-group">
<ul id="menu">
<?php foreach ($categories as $category) :
if (!empty($category['children'])) :
echo '<h3 style="margin-top:54px;">Potkategorije</h3>';
echo '<ul>';
foreach ($category['children'] as $category_level2) :
if (!empty($category_level2['children']) && $category_level2['category_id'] == $child_id) :
echo '<ul>';
foreach ($category_level2['children'] as $category_level3) :
echo '<li style="list-style-type: none"><a href="'.$category_level3['href'].'">'.$category_level3['name'].'</a></li>';
endforeach;
echo '</ul>';
endif;
echo '</li>';
endforeach;
echo '</ul>';
endif;
echo '</li>';
endforeach;
echo '</ul>';
?>
</div>
I truly hope this helps someone, cheers!
Upvotes: 1
Reputation: 6650
I think you have a problem in variable comparison :
<?php foreach ($child['children'] as $child_2) { ?>
<li>
<?php if ($child_2['category_id'] == $child_id) { ?>
To
<?php if ($child_2['category_id'] == $child_2_id) { ?>
Upvotes: 0