javascript opencart的类别菜单

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript opencart的类别菜单相关的知识,希望对你有一定的参考价值。

<div class="box">
  <div class="box-heading"><?php echo $heading_title; ?><span class="box-heading__icon"></span></div>
  <div class="box-content">
    <ul class="box-category categoryMenu">
      <?php foreach ($categories as $category) { ?>
      <li class="categoryMenu__item">

        <?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 class="categorySubmenu">
            <?php foreach ($category['children'] as $child) { ?>
              <li class="categorySubmenu__item categorySubmenu__item_level1">
                <?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 } ?>



                <?php if ($child['babies']) { ?>
                  <ul class="categorySubmenu">
                    <?php foreach ($child['babies'] as $baby) { ?>
                      <li class="categorySubmenu__item categorySubmenu__item_level2">
                        <?php if ($baby['category_id'] == $baby_id) { ?>
                        <a href="<?php echo $baby['href']; ?>" class="active"><?php echo $baby['name']; ?></a>
                        <?php } else { ?>
                        <a href="<?php echo $baby['href']; ?>"><?php echo $baby['name']; ?></a>
                        <?php } ?>
                      </li>
                    <?php }  // end foreach babies ?>
                  </ul>

                  <?php if ($child['category_id'] == $child_id) { ?>
                  <span class="toggleMenu toggleMenuLevel2 open"></span>
                  <?php } else { ?>
                  <span class="toggleMenu toggleMenuLevel2"></span>
                  <?php } ?>

                <?php } // end if babies ?>

              </li>
            <?php }  // end foreach children ?>
          </ul>

          <?php if ($category['category_id'] == $category_id) { ?>
          <span class="toggleMenu toggleMenuLevel1 open"></span>
          <?php } else { ?>
          <span class="toggleMenu toggleMenuLevel1"></span>
          <?php } ?>

        <?php } // end if children ?>
      </li>
      <?php } // end foreach category ?>
    </ul>
  </div>
</div>
<?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['baby_id'] = $parts[2];
        } else {
            $this->data['baby_id'] = 0;
        }

        $this->load->model('catalog/category');
        $this->load->model('catalog/product');
        $this->data['categories'] = array();
        $categories = $this->model_catalog_category->getCategories(0);

        //Показывать или нет количество товаров
        $show_product_count = $this->config->get('config_product_count');
        foreach ($categories as $category) {
            //Будем вычислять кол-во товаров в категориях только если это кол-во надо показывать
            $PIDs=array();
            if ($show_product_count) {
                $res = $this->model_catalog_product->getTotalProductsID(array('filter_category_id' => $category['category_id']));
                foreach ($res as $key=>$value) {
                    $PIDs[$value['product_id']]=$value['product_id'];
                }
            }
            // 2nd level
            $children_data = array();
            $children = $this->model_catalog_category->getCategories($category['category_id']);
            foreach ($children as $child) {
                //Будем вычислять кол-во товаров в категориях только если это кол-во надо показывать
                if ($show_product_count) {
                    $data = array(
                        'filter_category_id'  => $child['category_id'],
                        'filter_sub_category' => true
                    );
                    $res = $this->model_catalog_product->getTotalProductsID($data);
                    $product_total=count($res);
                    foreach ($res as $key=>$value) {
                        $PIDs[$value['product_id']]=$value['product_id'];
                    }
//					$total += count($PIDs);
                }
                // 3rd level
                $babies_data = array();
                $babies = $this->model_catalog_category->getCategories($child['category_id']);
                if ($babies) {
                    foreach ($babies as $baby) {
                        //Будем вычислять кол-во товаров в категориях только если это кол-во надо показывать
                        if ($show_product_count) {
                            $data = array(
                                'filter_category_id'  => $baby['category_id'],
                                'filter_sub_category' => true
                            );
                            $res = $this->model_catalog_product->getTotalProductsID($data);
                            $product_total=count($res);
                            foreach ($res as $key=>$value) {
                                $PIDs[$value['product_id']]=$value['product_id'];
                            }
//					        $total += count($PIDs);
                        }
                        $babies_data[] = array(
                            'category_id' => $baby['category_id'],
                            'name'        => $baby['name'] . ($show_product_count ? ' (' . $product_total . ')' : ''),
                            'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'] . '_' . $baby['category_id'])
                        );
                    }
                }
                $children_data[] = array(
                    'category_id' => $child['category_id'],
                    'name'        => $child['name'] . ($show_product_count ? ' (' . $product_total . ')' : ''),
                    'babies'      => $babies_data,
                    'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
                );
            }
            $total = count($PIDs);
            $this->data['categories'][] = array(
                'category_id' => $category['category_id'],
                'name'        => $category['name'] . ($show_product_count ? ' (' . $total . ')' : ''),
                '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')) {
            $this->template = $this->config->get('config_template') . '/template/module/category.tpl';
        } else {
            $this->template = 'default/template/module/category.tpl';
        }

        $this->render();
    }
}
?>
$(".toggleMenu").on("click", function() {
        	// Level 2
			if ( $(this).hasClass("toggleMenuLevel2") ) {
                var ul = $(this).prev();
                var a = $(this).prev().prev();
                var parent = $(this).parent();

				if ( $(this).hasClass("open") ) {
					ul.stop().slideUp(200);
					a.removeClass("active");
					$(this).removeClass("open");
				} else {
					var li = parent.siblings("li");
					$.each(li, function(i, el) {
                        $(el).find("a").removeClass("active");
                        $(el).find("ul").stop().slideUp(200);
                        $(el).find("span").removeClass("open");
					});
                    ul.stop().slideDown(200);
                    a.addClass("active");
                    $(this).addClass("open");
				}
			}

			// Level 1
            if ( $(this).hasClass("toggleMenuLevel1") ) {
                var ul = $(this).prev();
                var a = $(this).prev().prev();

                if ( $(this).hasClass("open") ) {
                	if ( ul.has(".categorySubmenu").length > 0 ) {
                		ul.find(".active").removeClass("active");
                		ul.find(".categorySubmenu").stop().slideUp(200);
                		ul.find(".toggleMenu").removeClass("open");
					}
                    ul.stop().slideUp(200);
                    a.removeClass("active");
                    $(this).removeClass("open");
                } else {
                	$(".active").removeClass("active");
                	$(".toggleMenu").removeClass("open");
                	$(".categorySubmenu").stop().slideUp(200);
                    ul.stop().slideDown(200);
                    a.addClass("active");
                    $(this).addClass("open");
                }
            }
        });

以上是关于javascript opencart的类别菜单的主要内容,如果未能解决你的问题,请参考以下文章

Opencart 全类 ul li 树

Foreach 并继续创建子菜单列

xml 缓存Opencart 1.5的类别数据,使其更快。 Opencart安装有很多类别真的很慢。 HTTP://forum.opencart

Opencart SEO打破类别直接

PHP OpenCart主要类别

OpenCart主类别