1

В главном меню ocStore по умолчанию выведено два уровня категорий, но как вывести третий уровень категорий? Версия ocStore 3.0.2.0

2 ответа 2

1

controller/common/menu.php (вместо стардартного foreach ($categories as $category) {)

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

    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) {

                // Level 3
                $grandchildren_data = array();

                $grandchildren = $this->model_catalog_category->getCategories($child['category_id']);

                foreach ($grandchildren as $grandchild) {

                    $grandchild_filter_data = array(
                        'filter_category_id'  => $grandchild['category_id'],
                        'filter_sub_category' => true
                    );

                    $grandchildren_data[] = array(
                        'name'  => $grandchild['name'],
                        'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'] . '_' . $grandchild['category_id']),
                    );
                }


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

                $children_data[] = array(
                    'name'  => $child['name'],
                    'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']),
                    'children' => $grandchildren_data,
                );
            }

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

view/theme/default{or_your_theme_name}/template/common/menu.twig (вместо стардартного <ul class="nav navbar-nav list-unstyled">)

<ul class="nav navbar-nav list-unstyled">
                    {% for category in categories %}
                        {% if category.children %}
                            <li class="dropdown"><a href="{{ category.href }}">{{ category.name }}</a>
                                <div class="dropdown-menu lvl1">
                                    <div class="container">
                                        <div class="row">
                                            <div class="col-sm-12">
                                                {% for children in category.children|batch(category.children|length / category.column|round(1, 'ceil')) %}
                                                    <ul class="subcats list-unstyled">
                                                        {% for child in children %}
                                                            {% if child.children %}
                                                                <li class="have-subchild">
                                                                     <a href="{{ child.href }}">{{ child.name }}</a>
                                                                    <ul class="lvl2">
                                                                        {% for children in child.children %}
                                                                                <li><a href="{{ children.href }}">{{ children.name }}</a></li>
                                                                        {% endfor %}
                                                                    </ul>
                                                                </li>
                                                            {% else %}
                                                                    <li><a href="{{ child.href }}">{{ child.name }}</a></li>
                                                            {% endif %}
                                                        {% endfor %}
                                                    </ul>
                                                {% endfor %}
                                            </div>  
                                        </div>
                                    </div>
                                </div>
                            </li>
                        {% else %}
                            <li><a href="{{ category.href }}">{{ category.name }}</a></li>
                        {% endif %}
                    {% endfor %}
                </ul>
1
0

Я бы переделал twig файл ибо там слишком много некорректных моментов (ul в ul, div в li.....)

и тут более понятно что к чему

{% if categories %} <!--в случае если есть категории вообще -->
<ul><!--делаем список -->
{% for category in categories %}<!--перебираем массив пунктов -->
{% if category.children %}<!-- в случае если есть второй уровень меню-->
<li class="dropdown"><a href="{{ category.href }}">{{ category.name }}</a><!-- в таком пункте-->
<ul class="sub_level2"><!--выводим список -->
{% for children in category.children|batch(category.children|length / category.column|round(1, 'ceil')) %}<!--переираем массим 3-его уровня (детей детей) -->
{% for child in children %}<!--в случае если есть детё детей -->
{% if child.children %}
<li><a href="{{ child.href }}">{{ child.name }}</a><!--в его пункте -->
<ul class="sub_level3"><!--делаем 3-ий список -->
{% for children in child.children %}<!--берем самого дитё детища -->
<li><a href="{{ children.href }}">{{ children.name }}</a></li><!--выводим его (их) -->
{% endfor %}
</ul>
</li>
{% else %}
<li><a href="{{ child.href }}">{{ child.name }}</a></li><!-- а если у дитя нет детей то выводим как есть (только его)-->
{% endif %}
{% endfor %}
</ul>
</li>
{% endfor %}
{% else %}
<li><a href="{{ category.href }}">{{ category.name }}</a></li> <!--а если у 1-ого уровня нет вообще детей, то выводим только его (как есть)-->
{% endif %}
{% endfor %}
</ul>
{% endif %} 

PS: Также можно легко с помощью css/js цепляется за родителей и детей, и детей детей чтобы выполнять какие-то манипуляции

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.