0

我是opencart的新手。我正在使用类别模块在左侧边栏中显示类别。我正在尝试添加与ebay.in左侧类别相同的子类别限制(例如一列中的 5 个)。

我的.../module/category.php文件是

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

    $this->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])) {
        $this->data['category_id'] = $parts[0];
    } else {
        $this->data['category_id'] = 0;
    }

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

     if (isset($parts[2])) {
     $this->data['sisters_id'] = $parts[2];
  } else {
     $this->data['sisters_id'] = 0;
  }

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

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

    $this->data['categories'] = array();

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

    foreach ($categories as $category) {


     $children_data = array();

     $children = $this->model_catalog_category->getCategories($category['category_id']);

     foreach ($children as $child) {            
        $sister_data = array();
        $sisters = $this->model_catalog_category->getCategories($child['category_id']);
        if(count($sisters) > 1) {
           foreach ($sisters as $sisterMember) {
              $sister_data[] = array(
                 'category_id' =>$sisterMember['category_id'],
                 'name'        => $sisterMember['name'],
                 'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']. '_' . $sisterMember['category_id'])   
              );                     

           }
           $children_data[] = array(
                 'category_id' => $child['category_id'],
                 'sister_id'   => $sister_data,
                 'name'        => $child['name'],
                 'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])   
              );   
        }else{                     
           $children_data[] = array(
              'category_id' => $child['category_id'],
              'sister_id'    =>'',
              'name'        => $child['name'],
              'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])   
           );   
        }
     }         
     $data = array(
        'filter_category_id'  => $category['category_id'],
        'filter_sub_category' => true   
     );      

     $product_total = $this->model_catalog_product->getTotalProducts($data);
     $this->load->model('tool/image'); 
     $this->data['categories'][] = array(
        'category_id' => $category['category_id'],
        'name'        => $category['name'],
        'children'    => $children_data,
        'sister'    => $sister_data,
        'category_image' => $this->model_tool_image->resize($category['image'], 20, 20),
        'href'        => $this->url->link('product/category', 'path=' . $category['category_id'])
     );
  }

    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();
}}?>

我的...module/category.tpl文件是

<div id="menu_box">
  <div class="box-heading"><?php echo $heading_title; ?></div>
      <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>
          <span style="float:right; font-weight:bold;">»</span>
          <a class="menuimg" ><img src="<?php echo $category['category_image']; ?> " /></a>
          <?php } ?>
          <?php if ($category['children']) { ?>
          <ul class="second-level" style="position: absolute;left: 166px;top:-2px;padding: 5px;margin: 0px;width:350px; background-color: whitesmoke; border:1px solid#ececec; z-index:10;">
            <?php foreach ($category['children'] as $child) { ?>
            <li id="second-li">
              <?php if ($child['category_id'] == $child_id) { ?>
              <a style="border-bottom:1px solid #5d5d5d;" href="<?php echo $child['href']; ?>" class="active"> <?php echo $child['name']; ?></a>
              <?php } else { ?>
              <a style="border-bottom:1px solid #5d5d5d;" href="<?php echo $child['href']; ?>"> <?php echo $child['name']; ?></a>
              <?php } ?>              
                <?php if($child['sister_id']){ ?>
                     <ul>
                    <?php foreach($child['sister_id'] as $sisters) { ?>
                        <li>
         <?php if ($sisters['category_id'] == $sisters_id) { ?>   


<a href="<?php echo $sisters['href']; ?>" class="active"><?php echo $sisters['name']; ?></a>
<?php } else { ?>
<a href="<?php echo $sisters['href']; ?>"><?php echo $sisters['name']; ?></a>
<?php } ?>
</li>
                    <?php } ?>
                     </ul>
                 <?php } ?>
            </li>
            <?php } ?>
          </ul>
          <?php } ?>
        </li>

        <?php } ?>
      </ul>
    </div>

非常感谢任何建议这样做。

4

1 回答 1

3

对于主要类别

丑陋/快速修复

您可以添加此行

$categories = array_slice($categories, 0, 5);

略低于

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

catalog/controller/module/category.php

更好的解决方案

catalog/model/catalog/category.php复制该函数getCategories($parent_id = 0)并向其添加第二个参数limit。在查询中使用这个参数来限制返回的结果

限制子类别/子类别中的结果

丑陋的修复

添加这一行

$children = array_slice($children, 0, 5);

略低于

$children = $this->model_catalog_category->getCategories($category['category_id']);

catalog/controller/module/category.php

更好的解决方案

复制 中的函数getCategoriesByParentId($category_id),为其catalog/model/catalog/category.php添加第二个参数limit,在 SQL 查询中使用此参数。

希望有帮助!

于 2012-11-20T05:16:19.953 回答