<script>
$( document ).ready(function() {

//Список вложенных разделов
let subsectionObj = {
    
"Еда": [
        "Фрукты",
        "Сухофрукты",
        "Пицца",
        "Мясо",
        "Бургер"
    ],

"Техника": [
        "Пилы торцевые",
        "Камеры",
        "Авто",
        "Коляска",
        "Погрузчик",
    ],    
        
    
};


let shopComplete = true;
function prodComplete(){
//При загрузке товаров
let tistore = setInterval(function() {
    setTimeout(function(){
        if ( $('.js-store-grid-cont div').length>0 || $('.t-store__prod-snippet__container').length>0 ){
            clearInterval(tistore) 
            if(shopComplete){
                shopComplete = false;
                setTimeout(function(){  makeSubsection(); }, 200); 
            };
        };
    }, 600);
    
}, 100);   
};
prodComplete();

//Фунцция формирования разделов
function  makeSubsection(){
    //определяем основные разделы
    $.each(subsectionObj, function( key, value ) {
        //Ищем основные разделы
        $('.js-store-parts-switcher').each(function(){
            let caption = $(this).text();
            if(key==caption) {
                $(this).addClass('main-chapter');
                $(this).after('<div class="subsection"></div>');
                let mChp = $(this).next('.subsection');
                
                //Ищем подразделы в этот разделе
                for(var i = 0; i < value.length; i++) {
                    $('.js-store-parts-switcher').each(function(){
                        let caption = $(this).text();
                        if(value[i]==caption) {
                            $(this).addClass('sub-chapter');
                            $(this).appendTo(mChp);
                        };
                    
                    });
                };
            };
        });
    });
    
    
    //Поиск активного раздела при открытии страницы
    $('.js-store-parts-switcher').each(function(){
    
        if( $(this).hasClass('sub-chapter') &&  $(this).hasClass('t-active') ) {
        
            $(this).closest('.subsection').addClass('show-subsection');
            $('.show-subsection').prev().addClass('open-chapter');
        
    };
    
     
     
});
    
    
};


//При открытии подраздела
$(document).on('click','.main-chapter',function(e){ 
    $(this).toggleClass('open-chapter');
    $(this).next('.subsection').toggleClass('show-subsection');
});


//$(document).on('click','.js-store-filter , .js-store-filter-chosen-item , .js-store-load-more-btn , .js-store-parts-switcher',function(e){  shopComplete = true; prodComplete(); });
//$(document).on('keydown','.js-store-filter',function(event){if(event.keyCode == 13) { shopComplete = true; prodComplete()}}); 
$(document).on('change','.t-store__sort-select',function(e){  shopComplete = true; prodComplete() });



});
</script>




<style>
.subsection {
    display: flex;
    flex-wrap: wrap;
    padding-left: 15px;
    display: none;
}

.subsection.show-subsection{
    display: block;
}


.sub-chapter {
    width: 100%;
}

.main-chapter {
    text-decoration: underline;
    display: flex;
    align-items: center;
}

.js-store-parts-switcher.t-store__parts-switch-btn.sub-chapter {
    font-size: 14px;
    opacity: 0.8;
}

.main-chapter:after {
    content: "";
    background-image: url(https://static.tildacdn.com/tild3837-6431-4465-b265-383663343966/arrow_down_icon_1842.svg);
    width: 19px;
    height: 19px;
    display: inline-block;
    background-size: contain;
    background-repeat: no-repeat;
    margin-left: 8px;
    transition: all 0.3s ease-in-out;
}   
.open-chapter.main-chapter:after {
    transform: rotate(180deg);
}


@media screen and (max-width: 960px){
.js-store-parts-switcher.t-store__parts-switch-btn{
    font-size: 16px;
    font-weight:300;
    display: flex;
    margin-left: 0;
}

.js-store-parts-switcher.t-store__parts-switch-btn.sub-chapter.t-active {
    font-weight: 600;
}

.t-store__parts-switch-wrapper {
    max-height: 200px;
    overflow-y: auto;
}

.t951__sidebar-wrapper ::-webkit-scrollbar-thumb {
        background: #e0e0e0;
        border-radius: 10px;
}
.t951__sidebar-wrapper ::-webkit-scrollbar {
        width: 5px;
        height: 5px;
        background: #000000;
}


}





</style>
Made on
Tilda