2010-11-13 78 views
2

我在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; 
    }   
} 
?> 

我真的希望有人知道的解決方案。

回答

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>