P
P
Pavel Zuev2020-02-25 14:09:59
opencart
Pavel Zuev, 2020-02-25 14:09:59

How to display subcategories on Home opencart 3x?

How to display subcategories on Home opencart 3x?

<div class="list-group">
        {% for category in categories %}
        {% if category.category_id == category_id %} 
        <a href="{{ category.href }}" class="list-group-item active">{{ category.name }}</a> 
        {% if category.children %}
        {% for child in category.children %}
        {% if child.category_id == child_id %}
        <a href="{{ child.href }}" class="list-group-item active">&nbsp;&nbsp;&nbsp;- {{ child.name }}</a> 
        {% else %} 
        <a href="{{ child.href }}" class="list-group-item">&nbsp;&nbsp;&nbsp;- {{ child.name }}</a>
        {% endif %}
        {% endfor %}
        {% endif %}
        {% else %} <a href="{{ category.href }}" class="list-group-item">{{ category.name }}</a>
        {% endif %}
        {% endfor %}
      </div>

This only outputs the categories. What to change where to dig?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lazuren, 2020-03-08
@lazuren

The controller has only 2 levels of nesting, so this should be a similar question.

P
Pavel Zuev, 2020-03-18
@idpabloo

The issue has been resolved
, an additional module has been created in the controller.

<?php
class ControllerExtensionModuleCategoryWall extends Controller {

    public function index() {
        $this->load->language('extension/module/category_wall');

        $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();

        $data['categories'] = array();

        $categories = $this->model_catalog_category->getCategories(0);

        $this->load->model('tool/image');

        foreach ($categories as $category) {
            $children_data = array();
            if ($category['top']) {
                $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);
                    if ($child['image']) {
                        $image = $this->model_tool_image->resize($child['image'], $this->config->get('theme_' . $this->config->get('config_theme') . '_image_category_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_category_height'));
                     } else {
                        $image = $this->model_tool_image->resize('placeholder.png', $this->config->get('theme_' . $this->config->get('config_theme') . '_image_category_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_category_height'));
                    }
                    $children_data[] = array(
                        'category_id' => $child['category_id'],
                        'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
                        'image' => $image,
                        'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
                    );
                }    
            if ($category['image']) {
                $image = $this->model_tool_image->resize($category['image'], $this->config->get('theme_' . $this->config->get('config_theme') . '_image_category_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_category_height'));
            } else {
                $image = $this->model_tool_image->resize('placeholder.png', $this->config->get('theme_' . $this->config->get('config_theme') . '_image_category_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_category_height'));
            }

            $data['categories'][] = array(
                'category_id' => $category['category_id'],
                'description' => $category['description'],
                'name' => $category['name'],
                'children'    => $children_data,
                'image' => $image,
                'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
                );
            }
        }
    return $this->load->view('extension/module/category_wall', $data);
    
        
    }
}

wrote in twig
<section class="category_table">
  <div class="container">
  {% for category in categories %}
     {% if category.category_id == 62 %}  
    <h3>{{ category.name }}</h3>
    <div class="row">
         
 
              {% if category.children %}
               {% for child in category.children  %}
                {% if child.category_id == child_id %}
                  <div class="col-sm-12 col-12">
                <div class="category_child">
                <a href="{{ child.href }}"><img src="{{ child.image }}" alt="{{ child.name }}" title="{{ child.name }}" class="img-responsive" /></a>
                <div class="child_name"><a href="{{ child.href }}">{{ child.name }}</a></div>
                </div>
                </div>
                {% else %}
                <div class="col-sm-4 col-12">
                <div class="category_child">
                <a href="{{ child.href }}"><img src="{{ child.image }}" alt="{{ child.name }}" title="{{ child.name }}" class="img-responsive" /></a>
                <div class="child_name"><a href="{{ child.href }}">{{ child.name }}</a></div>
                </div>
                </div>
                {% endif %}
               {% endfor %}
              {% endif %}
          </div> 
        </div>
          {% endif %}
        {% endfor %}

    </div>    
</div>
</section>

All for worked subcategories of a certain category are displayed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question