4

I am looking for a way to show one more category on a sitemap page (it's 3 by default).

I tried to modify sitemap.twig of the template with the following code:

<li><a href="{{ category_3.href }}">{{ category_3.name }}</a>
   {% if category_3.children %}
     <ul>
       {% for category_4 in category_3.children %}
        <li><a href="{{ category_4.href }}">{{ category_4.name }}</a></li>
       {% endfor %}
      </ul>
    {% endif %}
</li>

But it didn't work.

Then I also tried to change /catalog/controller/information/sitemap.php with one more foreach cycle:

foreach ($categories_3 as $category_3) {
    $level_4_data = array();

    $categories_4 = $this->model_catalog_category->getCategories($category_3['category_id']);

    foreach ($categories_4 as $category_4) {
        $level_4_data[] = array(
            'name' => $category_4['name'],
            'href' => $this->url->link('product/category', 'path=' . $category_1['category_id'] . '_' . $category_2['category_id'] . '_' . $category_3['category_id'] . '_' . $category_4['category_id'])
        );
    }

    $level_3_data[] = array(
        'name' => $category_3['name'],
        'children' => $level_4_data,
        'href' => $this->url->link('product/category', 'path=' . $category_1['category_id'] . '_' . $category_2['category_id'] . '_' . $category_3['category_id'])
    );
}

And nothing happened at all. So I am asking if there is any way to solve my problem?

Matias Kinnunen
  • 5,463
  • 3
  • 27
  • 30
val_ya
  • 99
  • 5

1 Answers1

1

You can show all categories and their sub-categories (category tree) this way:

Controller file:

catalog\controller\information\sitemap.php

Add these two functions at the end of this file, before closing }

protected $categoryTree = '';
protected function buildCategoryTree($parent_id = 0, $categories = null, $path = ''){
    $categories = $this->model_catalog_category->getCategories($parent_id);
    if($categories){
        $this->categoryTree .= '<ul>';
        foreach ($categories as $category) {
            $path = $path ? $path . $category['category_id'] : $category['category_id'];
            $href = $this->url->link('product/category', 'path=' . $path);
            $categories = $this->model_catalog_category->getCategories($category['category_id']);
            $this->categoryTree .= '<li><a href="' . $href . '">' . $category['name'] . '</a>';
            $this->buildCategoryTree($category['category_id'], $categories, $path . '_');
            $this->categoryTree .= '</li>';
        }
        $this->categoryTree .= '</ul>';
    }
}

Find:

$this->response->setOutput($this->load->view('information/sitemap', $data));

Add before it:

$this->buildCategoryTree();
$data['categories'] = $this->categoryTree;

View file:

catalog\view\theme\*\template\information\sitemap.twig

Find:

<ul>
{% for category_1 in categories %}
<li><a href="{{ category_1.href }}">{{ category_1.name }}</a>
  {% if category_1.children %}
  <ul>
    {% for category_2 in category_1.children %}
    <li><a href="{{ category_2.href }}">{{ category_2.name }}</a>
      {% if category_2.children %}
      <ul>
        {% for category_3 in category_2.children %}
        <li><a href="{{ category_3.href }}">{{ category_3.name }}</a></li>
        {% endfor %}
      </ul>
      {% endif %}
    </li>
    {% endfor %}
  </ul>
  {% endif %}
</li>
{% endfor %}
</ul>

Replace it with:

{{ categories }}

You can remove primary categories foreach loop in controller file.

DigitCart
  • 2,770
  • 2
  • 14
  • 27
  • If this works, nice job! But why are you building the HTML in `sitemap.php` instead of `sitemap.twig`? This way it's messy. – Matias Kinnunen Feb 08 '18 at 10:05
  • Thanks for your answer, but all things above only gave me an error **Notice: Array to string conversion in /var/www/u7895260/public_html/arlight.store/system/library/template/Twig/Environment.php(403) : eval()'d code on line 188Array** – val_ya Feb 08 '18 at 20:32
  • 1
    Please double check your edits and clear ocmod and twig caches. It's working on my side. – DigitCart Feb 08 '18 at 20:43