@DeniSidorenko

Как вывести все дочерние категории в header.tpl?

есть структура такая

Каталог -> И категории что идут дочерние, вложенные и.т.п

При выводе $categories у меня показываются лишь дочерние для 1 уровня( т.е для каталога) А дочерние для текущих дочерних - нет. Каким образом можно исправить и вывести дочерние для дочерних каталога( 3 уровень )
  • Вопрос задан
  • 45 просмотров
Решения вопроса 1
@Asokr
Модуль входит в состав Opencart.pro (бесплатной версии;) ) - можете скачать - и посмотреть более подробно, но в общем - как -то так:
Контролер
$data['categories'] = array();

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

		foreach ($categories as $category) {

			$children_data = array();

			$children = $this->model_catalog_category->getCategories($category['category_id']);

			foreach ($children as $child) {
			
			$children2_data = array();
			$children2 = $this->model_catalog_category->getCategories($child['category_id']);
			
				foreach ($children2 as $child2) {
				
					$children3_data = array();
					$children3 = $this->model_catalog_category->getCategories($child2['category_id']);
					
						foreach ($children3 as $child3) {
							
							$filter_data3 = array(
								'filter_category_id'  => $child3['category_id'],
							);				

							$children3_data[] = array(
								'category_id' => $child3['category_id'],
								'name'        => $child3['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data3) . ')' : ''),
								'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']. '_' . $child2['category_id']. '_' . $child3['category_id'])	
							);		
						
						
						}
				
					$filter_data2 = array(
						'filter_category_id'  => $child2['category_id'],
					);				

					$children2_data[] = array(
						'category_id' => $child2['category_id'],
						'name'        => $child2['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data2) . ')' : ''),
						'children3'    => $children3_data,
						'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']. '_' . $child2['category_id'])	
					);		
				
				
				}
				
				$filter_data1 = array(
						'filter_category_id'  => $child['category_id'],
					);	

				$children_data[] = array(
					'category_id' => $child['category_id'],
					'name'        => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data1) . ')' : ''),
					'children2'    => $children2_data,
					'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])	
				);		
			}
			
			$filter_data = array(
				'filter_category_id'  => $category['category_id'],
			);

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

Код шаблона
<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 } else { ?>
        <a href="<?php echo $category['href']; ?>" class="list-group-item"><?php echo $category['name']; ?></a>
        <?php } ?>
        <?php if (($category['children']) && ($category['category_id'] == $category_id)) { ?>
          <?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;- <?php echo $child['name']; ?></a>
            <?php } else { ?>
            <a href="<?php echo $child['href']; ?>" class="list-group-item">&nbsp;- <?php echo $child['name']; ?></a>
            <?php } ?>
			        <?php if (($child['children2']) &&  ($category['category_id'] == $category_id)) { ?>
						  <?php foreach ($child['children2'] as $child2) { ?>
							<?php if ($child2['category_id'] == $child_id2) { ?>
							<a href="<?php echo $child2['href']; ?>" class="list-group-item active">&nbsp;&nbsp;&nbsp;- <?php echo $child2['name']; ?></a>
							<?php } else { ?>
							<a href="<?php echo $child2['href']; ?>" class="list-group-item">&nbsp;&nbsp;&nbsp;- <?php echo $child2['name']; ?></a>
							<?php } ?>
								<?php if (($child2['children3']) &&  ($category['category_id'] == $category_id)) { ?>
									  <?php foreach ($child2['children3'] as $child3) { ?>
										<?php if ($child3['category_id'] == $child_id3) { ?>
										<a href="<?php echo $child3['href']; ?>" class="list-group-item active">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <?php echo $child3['name']; ?></a>
										<?php } else { ?>
										<a href="<?php echo $child3['href']; ?>" class="list-group-item">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <?php echo $child3['name']; ?></a>
										<?php } ?>
									  <?php } ?>
								 <?php } ?>
						  <?php } ?>
					 <?php } ?>
          <?php } ?>
        <?php } ?>
      <?php } ?>
</div>
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы