真的需要您的幫助來完成此項工作。我使用的是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"> - <?php echo $child['name']; ?></a>
<?php } else { ?>
<a href="<?php echo $child['href']; ?>" class="list-group-item"> - <?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, 乾杯
你希望它顯示它們展開並且默認情況下不折疊? – timothymarois
是的,我希望默認擴展模塊並顯示每個類別的所有子類別。雖然你的解決方案似乎不起作用。 你可以看看我的網站: http://test.vga.lt/ –
你確定你修改了正確的文件嗎?您的主題已從默認設置修改。但就我的代碼而言,我測試了我的本地版本,並且只列出了所有類別和子類別。它們防止它在控制器和視圖中發生。只要你注意到這些線,它應該顯示。 – timothymarois