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

For main category

Ugly/Quick fix

You can add this line

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

just below

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

in catalog/controller/module/category.php

Better Solution

In catalog/model/catalog/category.php replicate the function getCategories($parent_id = 0) and add a second parameter limit to it. Use this parameter inside the query to limit the results returned

To limit the results in children/sub-category

Ugly-Fix

Add this line

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

Just below

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

in catalog/controller/module/category.php

Better Solution

Replicate the function getCategoriesByParentId($category_id) in catalog/model/catalog/category.php, add a second paramter limit to it, use this parameter in the SQL query.

Hope that helps!

于 2012-11-20T05:16:19.953 に答える