在OpenCart中显示和计算类别

时间:2010-11-13 17:42:15

标签: php opencart

我在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 :(得分:0)

您无需自定义控制器。它已经有父类和子类列表,只需打开类别模块,然后过去下面的代码。

<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>