在 OpenCart 中显示和计数类别

发布于 2024-10-02 11:05:42 字数 3843 浏览 3 评论 0原文

我在使用 OpenCart 时遇到了这个问题,我想以自定义方式显示我的商店类别并计算父类别。

到目前为止,我修改了代码,得到了以下输出

<ul id="catOpContainer"> 
<li id="switchCatOp1">Parent Cat 1 
  <ul id="catOp1"> 
   <li>Child cat 1</li> 
   Child Cat 2</li> 
  </ul>
</li> 
Parent Cat 2 
  <ul id="catOp1"> 
   <li>Child cat 1</li> 
   Child cat 2</li> 
  </ul>
</li> 
Parent Cat 3</li> 
</ul>
</ul>

,而不是所需的输出。

<ul id="catOpContainer"> 
<li id="switchCatOp1">Parent Cat 1
  <ul id="catOp1"> 
   <li>Child Cat 1</li> 
   <li>Child Cat 2</li> 
  </ul> 
  </li> 
   <li id="switchCatOp2">Parent Cat 2
    <ul id="catOp2"> 
    <li>Child Cat 1</li> 
    <li>Child Cat 2</li> 
   <li>Child Cat 3</li> 
  </ul> 
</li> 
</ul>

显然,存在一些缺失的元素,但我不知道可能的解决方案。我也不知道如何计算父类别,以便我可以切换子类别。

我目前有以下代码片段:

<?php  
class ControllerModuleCategory extends Controller {
    protected $category_id = 0;
    protected $path = array();

    protected function index() {
        $this->language->load('module/category');

        $this->data['heading_title'] = $this->language->get('heading_title');

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

        if (isset($this->request->get['path'])) {
            $this->path = explode('_', $this->request->get['path']);

            $this->category_id = end($this->path);
        }

        $this->data['category'] = $this->getCategories(0);

        $this->id = 'category';

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

    protected function getCategories($parent_id, $current_path = '') {
        $category_id = array_shift($this->path);

        $output = '';

        $results = $this->model_catalog_category->getCategories($parent_id);

        if ($results) {
            if ($parent_id == 0) {
                $output .= '&lt;li id="switchCatOp1">';
            } else {
                $output .= '&lt;ul id="catOp1">&lt;li>';
            }
        }

        foreach ($results as $result) {    
            if (!$current_path) {
                $new_path = $result['category_id'];
            } else {
                $new_path = $current_path . '_' . $result['category_id'];
            }

            $output .= '';

            $children = '';

            // if ($category_id == $result['category_id']) {
                $children = $this->getCategories($result['category_id'], $new_path);
            // }

            if ($this->category_id == $result['category_id']) {
                $output .= '&lt;a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $new_path)  . '">' . $result['name'] . '&lt;/a>';
            } else {
                $output .= '&lt;a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $new_path)  . '">' . $result['name'] . '&lt;/a>';
            }

            $output .= $children;

            $output .= '&lt;/li>'; 
        }

        if ($results) {
            $output .= '&lt;/ul>';
        }

        return $output;
    }        
}
?>

我真的希望有人知道解决方案。

I have this issue with OpenCart where I want to display my shop categories in a custom way and count the parent categories.

I currently modified the code so far that I get the following output

<ul id="catOpContainer"> 
<li id="switchCatOp1">Parent Cat 1 
  <ul id="catOp1"> 
   <li>Child cat 1</li> 
   Child Cat 2</li> 
  </ul>
</li> 
Parent Cat 2 
  <ul id="catOp1"> 
   <li>Child cat 1</li> 
   Child cat 2</li> 
  </ul>
</li> 
Parent Cat 3</li> 
</ul>
</ul>

instead of the desired

<ul id="catOpContainer"> 
<li id="switchCatOp1">Parent Cat 1
  <ul id="catOp1"> 
   <li>Child Cat 1</li> 
   <li>Child Cat 2</li> 
  </ul> 
  </li> 
   <li id="switchCatOp2">Parent Cat 2
    <ul id="catOp2"> 
    <li>Child Cat 1</li> 
    <li>Child Cat 2</li> 
   <li>Child Cat 3</li> 
  </ul> 
</li> 
</ul>

It's obvious that there are some missing elements, but I have no clue about a possible solution. I also don't have a clue how to count the parent categories, so that I can toggle the subcategories.

I currently have the following code snippet:

<?php  
class ControllerModuleCategory extends Controller {
    protected $category_id = 0;
    protected $path = array();

    protected function index() {
        $this->language->load('module/category');

        $this->data['heading_title'] = $this->language->get('heading_title');

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

        if (isset($this->request->get['path'])) {
            $this->path = explode('_', $this->request->get['path']);

            $this->category_id = end($this->path);
        }

        $this->data['category'] = $this->getCategories(0);

        $this->id = 'category';

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

    protected function getCategories($parent_id, $current_path = '') {
        $category_id = array_shift($this->path);

        $output = '';

        $results = $this->model_catalog_category->getCategories($parent_id);

        if ($results) {
            if ($parent_id == 0) {
                $output .= '<li id="switchCatOp1">';
            } else {
                $output .= '<ul id="catOp1"><li>';
            }
        }

        foreach ($results as $result) {    
            if (!$current_path) {
                $new_path = $result['category_id'];
            } else {
                $new_path = $current_path . '_' . $result['category_id'];
            }

            $output .= '';

            $children = '';

            // if ($category_id == $result['category_id']) {
                $children = $this->getCategories($result['category_id'], $new_path);
            // }

            if ($this->category_id == $result['category_id']) {
                $output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $new_path)  . '">' . $result['name'] . '</a>';
            } else {
                $output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $new_path)  . '">' . $result['name'] . '</a>';
            }

            $output .= $children;

            $output .= '</li>'; 
        }

        if ($results) {
            $output .= '</ul>';
        }

        return $output;
    }        
}
?>

I really hope someone knows a solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深空失忆 2024-10-09 11:05:42

您不需要自定义控制器。它已经有父和子类别列表,只需打开类别模块,然后输入以下代码即可。

<div class="box-category">
  <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>
      <?php } ?>
      <?php if ($category['children']) { ?>
      <ul>
        <?php foreach ($category['children'] as $child) { ?>
        <li>
          <?php if ($child['category_id'] == $child_id) { ?>
          <a href="<?php echo $child['href']; ?>" class="active"><?php echo $child['name']; ?></a>
          <?php } else { ?>
          <a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a>
          <?php } ?>
        </li>
        <?php } ?>
      </ul>
      <?php } ?>
    </li>
    <?php } ?>
  </ul>
</div>

You do not need to customize controller. It has already Parent and child category listing, Just open the category module, and past below code.

<div class="box-category">
  <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>
      <?php } ?>
      <?php if ($category['children']) { ?>
      <ul>
        <?php foreach ($category['children'] as $child) { ?>
        <li>
          <?php if ($child['category_id'] == $child_id) { ?>
          <a href="<?php echo $child['href']; ?>" class="active"><?php echo $child['name']; ?></a>
          <?php } else { ?>
          <a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a>
          <?php } ?>
        </li>
        <?php } ?>
      </ul>
      <?php } ?>
    </li>
    <?php } ?>
  </ul>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文