2015-09-07 83 views
3

真的需要您的幫助來完成此項工作。我使用的是OpenCart 2.0.3.1,我希望側邊欄類別模塊默認顯示所有類別的所有子類別。目前,只有當您點擊某個類別並且僅顯示該類別的子類別時,該模塊纔會顯示子類別。您可以在動作看看它:OpenCart 2:默認顯示類別模塊中的所有子類別(php)

http://demo.opencart.com/index.php?route=product/category&path=20

(它是在左側邊欄的模塊)

我只使用默認的模塊。我嘗試了許多不同的方式來完成這項工作,並沒有什麼幫助實現這一點。我知道我需要編輯這兩個文件: 目錄/控制器/模塊/ category.php

<?php 
 
class ControllerModuleCategory extends Controller { 
 
\t public function index() { 
 
\t \t $this->load->language('module/category'); 
 

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

 
\t \t if (isset($this->request->get['path'])) { 
 
\t \t \t $parts = explode('_', (string)$this->request->get['path']); 
 
\t \t } else { 
 
\t \t \t $parts = array(); 
 
\t \t } 
 

 
\t \t if (isset($parts[0])) { 
 
\t \t \t $data['category_id'] = $parts[0]; 
 
\t \t } else { 
 
\t \t \t $data['category_id'] = 0; 
 
\t \t } 
 

 
\t \t if (isset($parts[1])) { 
 
\t \t \t $data['child_id'] = $parts[1]; 
 
\t \t } else { 
 
\t \t \t $data['child_id'] = 0; 
 
\t \t } 
 

 
\t \t $this->load->model('catalog/category'); 
 

 
\t \t $this->load->model('catalog/product'); 
 

 
\t \t $data['categories'] = array(); 
 

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

 
\t \t foreach ($categories as $category) { 
 
\t \t \t $children_data = array(); 
 

 
\t \t \t if ($category['category_id'] == $data['category_id']) { 
 
\t \t \t \t $children = $this->model_catalog_category->getCategories($category['category_id']); 
 

 
\t \t \t \t foreach($children as $child) { 
 
\t \t \t \t \t $filter_data = array('filter_category_id' => $child['category_id'], 'filter_sub_category' => true); 
 

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

 
\t \t \t $filter_data = array(
 
\t \t \t \t 'filter_category_id' => $category['category_id'], 
 
\t \t \t \t 'filter_sub_category' => true 
 
\t \t \t); 
 

 
\t \t \t $data['categories'][] = array(
 
\t \t \t \t 'category_id' => $category['category_id'], 
 
\t \t \t \t 'name'  => $category['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''), 
 
\t \t \t \t 'children' => $children_data, 
 
\t \t \t \t 'href'  => $this->url->link('product/category', 'path=' . $category['category_id']) 
 
\t \t \t); 
 
\t \t } 
 

 
\t \t if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/category.tpl')) { 
 
\t \t \t return $this->load->view($this->config->get('config_template') . '/template/module/category.tpl', $data); 
 
\t \t } else { 
 
\t \t \t return $this->load->view('default/template/module/category.tpl', $data); 
 
\t \t } 
 
\t } 
 
}

目錄/視圖/主題/默認/模板/模塊/ category.tpl

<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 if ($category['children']) { ?> 
 
    <?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;&nbsp;&nbsp;- <?php echo $child['name']; ?></a> 
 
    <?php } else { ?> 
 
    <a href="<?php echo $child['href']; ?>" class="list-group-item">&nbsp;&nbsp;&nbsp;- <?php echo $child['name']; ?></a> 
 
    <?php } ?> 
 
    <?php } ?> 
 
    <?php } ?> 
 
    <?php } else { ?> 
 
    <a href="<?php echo $category['href']; ?>" class="list-group-item"><?php echo $category['name']; ?></a> 
 
    <?php } ?> 
 
    <?php } ?> 
 
</div>

任何幫助apprieciated, 乾杯

+0

你希望它顯示它們展開並且默認情況下不折疊? – timothymarois

+0

是的,我希望默認擴展模塊並顯示每個類別的所有子類別。雖然你的解決方案似乎不起作用。 你可以看看我的網站: http://test.vga.lt/ –

+0

你確定你修改了正確的文件嗎?您的主題已從默認設置修改。但就我的代碼而言,我測試了我的本地版本,並且只列出了所有類別和子類別。它們防止它在控制器和視圖中發生。只要你注意到這些線,它應該顯示。 – timothymarois

回答

5

在這裏,如果我正確地得到您的問題,您希望在頁面上自動顯示所有子類別,而不是僅顯示當前頁面類別的子類別。

複製這些代碼到系統

控制器: 目錄/控制器/模塊/ category.php

<?php 
class ControllerModuleCategory extends Controller { 
    public function index() { 
     $this->load->language('module/category'); 

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

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

     if (isset($parts[0])) { 
      $data['category_id'] = $parts[0]; 
     } else { 
      $data['category_id'] = 0; 
     } 

     if (isset($parts[1])) { 
      $data['child_id'] = $parts[1]; 
     } else { 
      $data['child_id'] = 0; 
     } 

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

     $this->load->model('catalog/product'); 

     $data['categories'] = array(); 

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

     foreach ($categories as $category) { 
      $children_data = array(); 

      //if ($category['category_id'] == $data['category_id']) { 
       $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(
         'category_id' => $child['category_id'], 
         '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']) 
        ); 
       } 
      //} 

      $filter_data = array(
       'filter_category_id' => $category['category_id'], 
       'filter_sub_category' => true 
      ); 

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

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

視圖文件: 目錄/視圖/主題/默認/ template/module/category.tpl

<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 if ($category['children']) { ?> 
    <?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;&nbsp;&nbsp;- <?php echo $child['name']; ?></a> 
    <?php } else { ?> 
    <a href="<?php echo $child['href']; ?>" class="list-group-item">&nbsp;&nbsp;&nbsp;- <?php echo $child['name']; ?></a> 
    <?php } ?> 
    <?php } ?> 
    <?php } ?> 
    <?php /*} else { ?> 
    <a href="<?php echo $category['href']; ?>" class="list-group-item"><?php echo $category['name']; ?></a> 
    <?php }*/ ?> 
    <?php } ?> 
</div> 

正如您在文件中看到的那樣,我在中指出了這些語句會阻止子類別在默認情況下顯示。

+0

謝謝,這也是我的子類別問題的解決方案。 –

相關問題