Показать и посчитать категории в OpenCart - PullRequest
2 голосов
/ 13 ноября 2010

У меня есть эта проблема с OpenCart, где я хочу отображать категории своего магазина в индивидуальном порядке и подсчитывать родительские категории.

В настоящее время я изменил код до такой степени, что получаю следующий вывод

<ul id="catOpContainer"> 
<li id="switchCatOp1">Parent Cat 1 
  <ul id="catOp1"> 
   <li>Child cat 1</li> 
   Child Cat 2</li> 
  </ul>
</li> 
Parent Cat 2 
  <ul id="catOp1"> 
   <li>Child cat 1</li> 
   Child cat 2</li> 
  </ul>
</li> 
Parent Cat 3</li> 
</ul>
</ul>

вместо желаемого

<ul id="catOpContainer"> 
<li id="switchCatOp1">Parent Cat 1
  <ul id="catOp1"> 
   <li>Child Cat 1</li> 
   <li>Child Cat 2</li> 
  </ul> 
  </li> 
   <li id="switchCatOp2">Parent Cat 2
    <ul id="catOp2"> 
    <li>Child Cat 1</li> 
    <li>Child Cat 2</li> 
   <li>Child Cat 3</li> 
  </ul> 
</li> 
</ul>

Очевидно, что некоторые элементы отсутствуют, но я понятия не имею о возможном решении. Я также не знаю, как считать родительские категории, чтобы я мог переключать подкатегории.

В настоящее время у меня есть следующий фрагмент кода:

<?php  
class ControllerModuleCategory extends Controller {
    protected $category_id = 0;
    protected $path = array();

    protected function index() {
        $this->language->load('module/category');

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->load->model('catalog/category');
        $this->load->model('tool/seo_url');

        if (isset($this->request->get['path'])) {
            $this->path = explode('_', $this->request->get['path']);

            $this->category_id = end($this->path);
        }

        $this->data['category'] = $this->getCategories(0);

        $this->id = 'category';

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

        $this->render();
      }

    protected function getCategories($parent_id, $current_path = '') {
        $category_id = array_shift($this->path);

        $output = '';

        $results = $this->model_catalog_category->getCategories($parent_id);

        if ($results) {
            if ($parent_id == 0) {
                $output .= '&lt;li id="switchCatOp1">';
            } else {
                $output .= '&lt;ul id="catOp1">&lt;li>';
            }
        }

        foreach ($results as $result) {    
            if (!$current_path) {
                $new_path = $result['category_id'];
            } else {
                $new_path = $current_path . '_' . $result['category_id'];
            }

            $output .= '';

            $children = '';

            // if ($category_id == $result['category_id']) {
                $children = $this->getCategories($result['category_id'], $new_path);
            // }

            if ($this->category_id == $result['category_id']) {
                $output .= '&lt;a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $new_path)  . '">' . $result['name'] . '&lt;/a>';
            } else {
                $output .= '&lt;a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $new_path)  . '">' . $result['name'] . '&lt;/a>';
            }

            $output .= $children;

            $output .= '&lt;/li>'; 
        }

        if ($results) {
            $output .= '&lt;/ul>';
        }

        return $output;
    }        
}
?>

Я действительно надеюсь, что кто-то знает решение.

1 Ответ

0 голосов
/ 26 апреля 2012

Вам не нужно настраивать контроллер.В нем уже есть список родительских и дочерних категорий, просто откройте модуль категории и пройдите приведенный ниже код.

<div class="box-category">
  <ul>
    <?php foreach ($categories as $category) { ?>
    <li>
      <?php if ($category['category_id'] == $category_id) { ?>
      <a href="<?php echo $category['href']; ?>" class="active"><?php echo $category['name']; ?></a>
      <?php } else { ?>
      <a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
      <?php } ?>
      <?php if ($category['children']) { ?>
      <ul>
        <?php foreach ($category['children'] as $child) { ?>
        <li>
          <?php if ($child['category_id'] == $child_id) { ?>
          <a href="<?php echo $child['href']; ?>" class="active"><?php echo $child['name']; ?></a>
          <?php } else { ?>
          <a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a>
          <?php } ?>
        </li>
        <?php } ?>
      </ul>
      <?php } ?>
    </li>
    <?php } ?>
  </ul>
</div>
...