I am trying to display the categories menu on the rubric page using the code:

<?php var_dump($categories); ?> <?php foreach ($categories as $category): ?> <?php if ($category['children']): ?> <li class="nav-item <?php if ($category['category_id'] == $category_id) echo 'active'; ?>"> <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="<?= $category['href'] ?>"><?= $category['name'] ?></a> <div class="dropdown-menu"> <?php foreach ($category['children'] as $child): ?> <a class="dropdown-item <?php if ($child['category_id'] == $child_id) echo 'active'; ?>" href="<?= $child['href'] ?>"><?= $child['name'] ?></a> <?php endforeach; ?> </div> </li> <?php else: ?> <li class="nav-item"> <a class="nav-link" href="<?= $category['href'] ?>"> <?= $category['name'] ?></a> </li> <?php endif; ?> <?php endforeach; ?> 

And I get:

 <b>Notice</b>: Undefined variable: categories in <b>/home/m/matrixyc/site.com/public_html/catalog/view/theme/SnowWhite/template/common/column_left.tpl</b> on line <b>4</b>NULL <b>Notice</b>: Undefined variable: categories in <b>/home/m/matrixyc/site.com/public_html/catalog/view/theme/SnowWhite/template/common/column_left.tpl</b> on line <b>5</b><b>Warning</b>: Invalid argument supplied for foreach() in <b>/home/m/matrixyc/site.com/public_html/catalog/view/theme/SnowWhite/template/common/column_left.tpl</b> on line <b>5</b> 

In the same code, the code executes correctly.

Version ocStore 2.3.0.2.3.

    1 answer 1

    In the header, the code is processed because the header.php controller passes the $ categories array to the header.tpl file /catalog/controller/common/header.php

     // 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']) { // 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']) ); } // Level 1 $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/header', $data); 

    Accordingly, you need either to add the necessary information in the /catalog/controller/common/column_left.php controller, or use the standard "Categories" module, which displays the category tree in the desired positions (left, right column, bottom of the page, top of the page), or something else come up.