class ControllerCommonMenu extends Controller { public function index() { $this->load->language('common/menu'); // 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']) { // Check if the category has any products $filter_data = array('filter_category_id' => $category['category_id']); $total_products = $this->model_catalog_product->getTotalProducts($filter_data); // If no products in the category, skip it if ($total_products == 0) { continue; } // 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 ); // Check if the child has products $total_child_products = $this->model_catalog_product->getTotalProducts($filter_data); // Skip this child if no products if ($total_child_products == 0) { continue; } $children_data[] = array( 'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $total_child_products . ')' : ''), 'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']) ); } // Level 1: Add the category to the menu only if it has products or children with products if (count($children_data) > 0 || $total_products > 0) { $data['categories'][] = array( 'name' => $category['name'], 'children' => $children_data, 'column' => $category['column'] ? $category['column'] : 1, 'href' => $this->url->link('product/category', 'path=' . $category['category_id']) ); } } } return $this->load->view('common/menu', $data); } }