4

Really need your help to make this work. I'm using OpenCart 2.0.3.1 and I want the sidebar category module to show all subcategories of all categories by default. Currently the module shows subcategories only when you click on a category and it shows subcategories only of that category. You can have a look at it in action:

http://demo.opencart.com/index.php?route=product/category&path=20

(it's the module on left sidebar)

I'm just using the default module. I tried many different ways to get this work and nothing helped to achieve this. I know that I need to edit these two files: catalog/controller/module/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) {
     $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'])
     );
    }
   }

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

  if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/category.tpl')) {
   return $this->load->view($this->config->get('config_template') . '/template/module/category.tpl', $data);
  } else {
   return $this->load->view('default/template/module/category.tpl', $data);
  }
 }
}

catalog/view/theme/default/template/module/category.tpl

<div class="list-group">
  <?php foreach ($categories as $category) { ?>
  <?php if ($category['category_id'] == $category_id) { ?>
  <a href="<?php echo $category['href']; ?>" class="list-group-item active"><?php echo $category['name']; ?></a>
  <?php if ($category['children']) { ?>
  <?php foreach ($category['children'] as $child) { ?>
  <?php if ($child['category_id'] == $child_id) { ?>
  <a href="<?php echo $child['href']; ?>" class="list-group-item active">&nbsp;&nbsp;&nbsp;- <?php echo $child['name']; ?></a>
  <?php } else { ?>
  <a href="<?php echo $child['href']; ?>" class="list-group-item">&nbsp;&nbsp;&nbsp;- <?php echo $child['name']; ?></a>
  <?php } ?>
  <?php } ?>
  <?php } ?>
  <?php } else { ?>
  <a href="<?php echo $category['href']; ?>" class="list-group-item"><?php echo $category['name']; ?></a>
  <?php } ?>
  <?php } ?>
</div>

Any help apprieciated, Cheers

  • you want it to show them expanded and not collapsed by default? – tmarois Sep 07 '15 at 17:00
  • Yeah, I want the module to be expanded by default and show all subcategories of each category. Your solution doesn't seem to work though. You can have a look at my website: http://test.vga.lt/ – Andrius Petravicius Sep 07 '15 at 18:50
  • are you sure you modifying the correct files? Your theme is modified from the default. but as for my codes below, I tested on my local version, and it just listed all the categories and subcategories. They prevent it from happening in the controller and in the view. as long as you note those lines out, it should display. – tmarois Sep 07 '15 at 18:52
  • You're correct, something else is preventing all subcategories from displaying. I just tried your solution on my other test website, with clean opencart install and default template, it works there. I'll try to find what's the issue. Thanks! – Andrius Petravicius Sep 07 '15 at 18:57
  • awesome! glad to help. – tmarois Sep 07 '15 at 18:59

1 Answers1

6

Here, if I get your question correctly, You want to display all the subcategories automatically on the page, instead of only displaying subcategories of the category of the current page.

Copy these codes to your system

The Controller: catalog/controller/module/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) {
                    $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'])
                    );
                }
            //}

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

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/category.tpl')) {
            return $this->load->view($this->config->get('config_template') . '/template/module/category.tpl', $data);
        } else {
            return $this->load->view('default/template/module/category.tpl', $data);
        }
    }
}

The View file: catalog/view/theme/default/template/module/category.tpl

<div class="list-group">
  <?php foreach ($categories as $category) { ?>
  <?php //if ($category['category_id'] == $category_id) { ?>
  <a href="<?php echo $category['href']; ?>" class="list-group-item active"><?php echo $category['name']; ?></a>
  <?php if ($category['children']) { ?>
  <?php foreach ($category['children'] as $child) { ?>
  <?php if ($child['category_id'] == $child_id) { ?>
  <a href="<?php echo $child['href']; ?>" class="list-group-item active">&nbsp;&nbsp;&nbsp;- <?php echo $child['name']; ?></a>
  <?php } else { ?>
  <a href="<?php echo $child['href']; ?>" class="list-group-item">&nbsp;&nbsp;&nbsp;- <?php echo $child['name']; ?></a>
  <?php } ?>
  <?php } ?>
  <?php } ?>
  <?php /*} else { ?>
  <a href="<?php echo $category['href']; ?>" class="list-group-item"><?php echo $category['name']; ?></a>
  <?php }*/ ?>
  <?php } ?>
</div>

As you can see in the files, I noted out the statements that are preventing the subcategories from displaying by default.

tmarois
  • 2,424
  • 2
  • 31
  • 43