Category Images in Main Menu on Opencart 2.0.x

Date: 02/25/2016, Author: Lostwake, Category: PHP Quick Reference

Ok so here is a hack for Opencart to get category images in the sub-categories of the main menu.

I was using Opencart 2.1.0.1 when I did this.

We will need to edit two files to make this happen the first is: catalog/controller/common/header.php

We are going to start around line 113 and change this code:

 

// 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'])
                );

To:

// 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'])
                );
            }

Next we are going to edit this file: catalog/view/theme/default/template/common/header.tpl

We are only going to cahnge line 108 where the list

a href="?php echo $child['href']; ?">"/>

Let me know in the comments if you have any questions.

 

Comments: