Reputation: 52
I am having a hard time to make this possible. what i am about to do is that i want category images in my menu as thumbs in Opencart 2.0.x.
I have put 'thumb' => $thumb, in catalog/controller/common/header.tpl
Like this:
// Menu
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$data['categories'] = array();
$categories = $this->model_catalog_category->getCategories(0);
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) {
$filter_data = array(
'filter_category_id' => $child['category_id'],
'filter_sub_category' => true
);
$children_data[] = array(
'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'])
);
}
// Level 1
$this->load->model('tool/image');
$image = empty($category['image']) ? 'no_image.jpg' : $category['image'];
$thumb = $this->model_tool_image->resize($image, 100, 100);
$data['categories'][] = array(
'name' => $category['name'],
'children' => $children_data,
'thumb' => $thumb,
'column' => $category['column'] ? $category['column'] : 1,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
}
}
And i also put the $category['thumb'] in catalog/view/theme/default/template/common/header.tpl
Like this:
<?php if ($categories) { ?>
<nav id="navigation">
<div class="boxed bg-navigation">
<div class="container">
<ul data-breakpoint="800" class="flexnav with-js lg-screen clearfix">
<?php foreach ($categories as $category) { ?>
<?php if ($category['children']) { ?>
<li><a href="<?php echo $category['href']; ?>" ><?php echo $category['name']; ?></a>
<ul class="list-unstyled ul-col-<?php echo $category['column']; ?>">
<?php foreach (array_chunk($category['children'], ceil(count($category['children']) / $category['column'])) as $children) { ?>
<?php if ($category['column'] == 1) { ?>
<?php foreach ($children as $child) { ?>
<li><a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a></li>
<?php } ?>
<?php } else { ?>
<li class="li-inline">
<div>
<?php foreach ($children as $child) { ?>
<?php $category['thumb']?>
<a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a>
<?php } ?>
</div>
</li>
<?php } ?>
<?php } ?>
</ul>
</li>
<?php } else { ?>
<li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a></li>
<?php } ?>
<?php } ?>
</ul>
</div>
</div>
</nav>
<?php } ?>
What am i doing wrong here? i found this solution on Stack Overflow Solution
But i can't seem to fix this properly, it's probably a simple thing that i can't really see. I have done this before with another framework in OpenCart 1.5.6.4. But that also didn't helped me alot hehe. If you want i can paste the other code to.
Upvotes: 0
Views: 1475
Reputation: 17
I had the same problem and got this working on Opencart 2.1.0.1 Here is the catalog/controller/common/header.php file:
(line 113)
// Level 2
$children_data = array();
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach ($children as $child) {
$filter_data = array(
'filter_category_id' => $child['category_id'],
'filter_sub_category' => true
);
$this->load->model('tool/image');
$image = empty($child['image']) ? 'no_image.png' : $child['image'];
$thumb = $this->model_tool_image->resize($image, 100, 100);
$children_data[] = array(
'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
'thumb' => $thumb,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
);
}
And here is my catalog/view/theme/default/template/common/header.tpl file:
(line 108)
<li><a href="<?php echo $child['href']; ?>"><img src="<?php echo $child['thumb']?>" alt="<?php echo $child['name']; ?>"/><br /><?php echo $child['name']; ?></a></li>
Hope this helps.
Upvotes: 2
Reputation: 1194
It is because $category['thumb']
contains only image source so in template file
Replace <?php $category['thumb']?>
With <img src="<?php echo $category['thumb']?>" alt="<?php echo $child['name']; ?>"/>
In addition in controller you are setting thumb of parent category and in template printing on child category so it will print same parent image again and again.
the no_image.jpg
is replaced as no_image.png
in latest version.
Upvotes: 1