2015-09-07 3 views
3

Vous avez vraiment besoin de votre aide pour faire ce travail. J'utilise OpenCart 2.0.3.1 et je veux que le module de catégorie sidebar affiche par défaut toutes les sous-catégories de toutes les catégories. Actuellement, le module affiche les sous-catégories uniquement lorsque vous cliquez sur une catégorie et qu'elle ne montre que des sous-catégories de cette catégorie. Vous pouvez avoir un coup d'oeil dans l'action:OpenCart 2: Afficher toutes les sous-catégories par défaut dans la catégorie module (php)

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

(c'est le module sur la barre latérale gauche)

Je suis juste en utilisant le module par défaut. J'ai essayé de nombreuses façons d'obtenir ce travail et rien n'a aidé à atteindre cet objectif. Je sais que je dois modifier ces deux fichiers: catalogue/contrôleur/module/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 } 
 
}

Catalogue/view/thème/default/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>

Toute aide apprieciated, Cheers

+0

vous voulez qu'il les affiche développé et non réduit par défaut? – timothymarois

+0

Ouais, je veux que le module soit développé par défaut et montre toutes les sous-catégories de chaque catégorie. Votre solution ne semble pas fonctionner cependant. Vous pouvez jeter un oeil à mon site Web: http://test.vga.lt/ –

+0

Êtes-vous sûr de modifier les bons fichiers? Votre thème est modifié par défaut. mais en ce qui concerne mes codes ci-dessous, j'ai testé sur ma version locale, et il a juste énuméré toutes les catégories et sous-catégories. Ils l'empêchent de se produire dans le contrôleur et dans la vue. tant que vous notez ces lignes, il devrait s'afficher. – timothymarois

Répondre

5

Ici, si je reçois votre question correctement, vous voulez afficher toutes les sous-catégories automatiquement sur la page, au lieu d'afficher uniquement les sous-catégories de la catégorie de la page en cours.

Copiez ces codes à votre système

Le contrôleur: catalogue/contrôleur/module/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); 
     } 
    } 
} 

Le fichier Vue: Catalogue/view/thème/default/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> 

Comme vous pouvez le voir dans les fichiers, je noté les déclarations qui empêchent les sous-catégories d'afficher par défaut.

+0

Merci, c'était aussi la solution pour mon problème de sous-catégorie. –