1

我目前正在寻求帮助,将标题菜单中的主要类别链接直接链接到它们的第一个子类别。

换句话说:

单击类别链接时,我想在末尾添加第一个子类别的 ID,即

route=product/category&path=18_59 

代替

route=product/category&path=18

有没有人建议如何做到这一点?

<div id="menu">
  <ul>
    <?php foreach ($categories as $category) { ?>
    <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
      <?php if ($category['children']) { ?>
      <div>
        <?php for ($i = 0; $i < count($category['children']);) { ?>
        <ul>
          <?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
          <?php for (; $i < $j; $i++) { ?>
          <?php if (isset($category['children'][$i])) { ?>
          <li><a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?></a></li>
          <?php } ?>
          <?php } ?>
        </ul>
        <?php } ?>
      </div>
      <?php } ?>
    </li>
    <?php } ?>
  </ul>
</div>
4

1 回答 1

1

欢迎来到StackOverflow

这可以通过获取主类别的第一个孩子来实现,我们有两个选择,一个更干净,一个更容易。

清洁工之一。这将需要更改catalog/controller/common/header.php和 模型catalog/model/catalog/category.php,让我们一步一步来。首先,将新的必要函数添加到类别模型中:

public function getCategoryFirstChildId($category_id) {
    $query = $this->db->query('SELECT category_id FROM '. DB_PREFIX .'category WHERE parent_id = '. (int)$category_id .' ORDER BY category_id ASC LIMIT 1');

    return $query->row['category_id'];
}

此函数将获取作为参数给定的所有类别,parent_id$category_id升序对它们进行排序category_id(您可以将其更改为您想要的任何排序)并仅返回第一个。

现在让我们转到控制器 - 我们将编辑加载类别的部分:

    foreach ($categories as $category) {
        if ($category['top']) {
            // Level 2
            $children_data = array();

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

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

                $product_total = $this->model_catalog_product->getTotalProducts($data);

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

/*NEW =>*/  $first_child_id = $this->model_catalog_category->getCategoryFirstChildId($category['category_id']);

            // Level 1
            $this->data['categories'][] = array(
                'name'     => $category['name'],
                'children' => $children_data,
                'column'   => $category['column'] ? $category['column'] : 1,
                'href'     => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $first_child_id)
//     >>>          >>>          >>>           >>>          >>>           >>>          >>>           ^^^^^^^^^^^^^^^^^^^^^^^^
            );
        }
    }

这应该是它。明智的是,这种虽然更清洁的方式需要每个类别多一个数据库查询,因此在处理大量类别时可能会减慢(尽管一点点)站点。

更简单的方法- 只需要在标头控制器中进行更改(不向模型添加新功能):

    foreach ($categories as $category) {
        if ($category['top']) {
            // Level 2
            $children_data = array();
/*NEW =>*/  $first_child_id = 0;
/*NEW =>*/  $first_child = true;

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

            foreach ($children as $child) {
/*NEW =>*/      if($first_child) {
/*NEW =>*/          $first_child = false;
/*NEW =>*/          $first_child_id = $child['category_id'];
/*NEW =>*/      }

                $data = array(
                    'filter_category_id'  => $child['category_id'],
                    'filter_sub_category' => true
                );

                $product_total = $this->model_catalog_product->getTotalProducts($data);

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

            // Level 1
            $this->data['categories'][] = array(
                'name'     => $category['name'],
                'children' => $children_data,
                'column'   => $category['column'] ? $category['column'] : 1,
                'href'     => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $first_child_id)
//     >>>          >>>          >>>           >>>          >>>           >>>          >>>           ^^^^^^^^^^^^^^^^^^^^^^^^
            );
        }
    }

更简单的方法也可以很好地工作并且会更快一些,但是它将指向的第一个类别将始终是排序升序时出现的第一个类别sort_order- 每当它可能发生变化时,主类别将指向不同的孩子类别...

我没有测试这两种方法,但我相信它们会 100% 起作用。

于 2013-06-14T11:39:27.387 回答