HUSKY - WooCommerce Product Filter Professional

How to Collapse Subcategories in a WooCommerce Category Filter

A category filter that lists every subcategory turns into a wall of checkboxes. Most shops want a tree instead: parent categories visible, subcategories folded under a plus sign, sometimes parents shown as plain headings. To collapse subcategories in a WooCommerce category filter built with HUSKY you work with two separate mechanisms that are easy to mix up: the small +/- opener next to a category that has children, and the toggle in the block title that folds the whole filter block. They have different settings and keep their state in different ways, which explains most "it opens by itself" surprises.

Collapse subcategories in the WooCommerce category filter

On the plugin's Options tab set Hide childs in checkboxes and radio to Yes (new installations already have it on). Every list of subcategories is then printed hidden, and each term that has children gets an opener after its name: a small plus that turns into a minus when the branch is open. It works at every depth, so a subcategory with its own children gets its own opener.

  • It applies to the Checkbox and Radio filter types only. Drop-down types never fold subcategories (see the last section).
  • With No, all subcategories are always visible and there are no openers.
  • For a long list of top-level categories, open the taxonomy's additional options (the cog button next to it on the Structure tab) and fill in Not toggled terms count: only that many terms are shown, and a Show more button reveals the rest.

If some parents should not list their subcategories in the filter at all, the woof_terms_where_hidden_childs hook removes them. It works for checkboxes, radio buttons and drop-downs:

/*
 * Product filter: list these parent categories without their subcategories.
 * Child theme functions.php or a code snippets plugin.
 */
add_filter('woof_terms_where_hidden_childs', function ($term_id) {
    $parents = array(15, 27); // IDs of the parent categories
    return in_array((int) $term_id, $parents, true) ? 1 : 0;
});

Return 1 for every term to show only the top level.

Why opened subcategories stay open after a reload

When a visitor clicks an opener, the plugin saves the state of that branch in the browser's local storage. On every page load, and after every AJAX redraw of the filter, branches saved as opened are opened again. Local storage does not expire, so a branch you opened while testing last week is still open in your browser today, while a new visitor sees everything closed. Always check the default state in a private window.

Two side effects come from this design:

  • The saved state decides which branch is open, not the current selection. A visitor who arrives through a filtered link, for example from a menu item that points to a subcategory filter, can find the selected subcategory hidden inside a closed branch.
  • The state is stored under the class of the parent's list item. Top-level categories have their own class (woof_term_15 and so on), but items below the first level do not, so all branches below the first level share one stored state: open one second-level branch, and after the next page load every second-level branch is open.

There is no setting to switch the memory off, and the edits of js/front.js suggested for this in the past are lost with every update. The snippet below does it from your theme instead. The plugin fires the woof_init_search_form event each time it has built the filter, on page load and after AJAX redraws; the snippet listens to it and sets every branch again: open if it contains a selected term, closed otherwise.

/*
 * Product filter: ignore remembered branches. A branch of subcategories is
 * open only when it contains a selected term.
 * Child theme functions.php or a code snippets plugin.
 */
add_action('wp_footer', function () {
    ?>
    <script>
    document.addEventListener('woof_init_search_form', function () {
        jQuery('ul.woof_childs_list').each(function () {
            var list = jQuery(this);
            var sign = list.parent('li').children('.woof_childs_list_opener').find('span');
            if (!sign.length) {
                return; // this list has no +/- opener
            }
            var open = list.find('input:checked').length > 0;
            list.toggle(open);
            sign.toggleClass('woof_is_opened', open).toggleClass('woof_is_closed', !open);
        });
    });
    </script>
    <?php
}, 100);

To show every branch opened by default while keeping the openers, change the var open line to var open = true;.

Fold whole filter blocks under their titles

The second mechanism hides a whole block except its title. Open the taxonomy's additional options on the Structure tab and set the toggle option to Yes, show as closed or Yes, show as opened. In 3.4.6 this option is labelled Show toggle button for radio in the taxonomy popup, but it is the toggle of that taxonomy's block. The title must be visible (Show title label = Yes), because the toggle sits inside it. On the Design tab, Toggle block type chooses text or images, and the fields next to it set the texts (by default + and -) or two images of about 20×20 px.

Block state is not remembered in the browser. The server sets it on every page: a block set to show as closed opens when one of its terms is part of the current filter, so the visitor sees what is selected. To keep blocks closed even then, or to force them open, use the woof_block_toggle_state hook:

/*
 * Product filter: blocks with a toggle always start closed.
 * Child theme functions.php or a code snippets plugin.
 */
add_filter('woof_block_toggle_state', function ($block_is_closed) {
    return true; // true = closed, false = opened
});

The hook receives only the state, not the taxonomy, so it applies to every block that has the toggle switched on.

Make the whole title clickable

By default only the +/- opens a block. The FAQ on making the toggle include the title attaches the click on page load. The version below is delegated, so it keeps working after AJAX filtering redraws the form, and it ignores clicks on the +/- itself, which would otherwise toggle the block twice:

/*
 * Product filter: a click anywhere on a block title opens or closes the block.
 * Child theme functions.php or a code snippets plugin.
 */
add_action('wp_footer', function () {
    ?>
    <style>.woof_container_inner > h4:has(> .woof_front_toggle) { cursor: pointer; }</style>
    <script>
    jQuery(document).on('click', '.woof_container_inner > h4', function (e) {
        if (jQuery(e.target).closest('.woof_front_toggle, .woof_tooltip_header').length) {
            return; // the +/- or the tooltip icon was clicked
        }
        jQuery(this).children('.woof_front_toggle').trigger('click');
    });
    </script>
    <?php
}, 100);

If you changed the title tag with the woof_title_tag hook, replace h4 with your tag. A text toggle is a link, so its colour is set with CSS such as .woof_container_inner h4 a.woof_front_toggle { color: #222; }.

To fold several blocks under one heading, for example all attributes under "More filters", use the Sections extension. Once it is active on the Extensions tab, its settings under Advanced → Sections wrap a range of filter elements into an open/close section; Sections behavior As checkbox lets several sections be open at once, As radio keeps only one open.

Show a parent category as a heading without a checkbox

When products are assigned to subcategories only, the parent works better as a heading. With a Radio and checkboxes skin selected on the Design tab, the real input is wrapped in a div whose class is the skin name with a hyphen: the skin square_blue gives icheckbox_square-blue (and iradio_square-blue for radio buttons). CSS written with icheckbox_square_grey, with an underscore, therefore matches nothing. The rules below work with any skin and with plain inputs. The plugin's script adds the class woof_childs_list_li to every item that has children, at every level. Put the CSS in Advanced → Code → Custom CSS code or in your theme:

/* Product categories filter: parents become headings without a box */
.woof_container_product_cat li.woof_childs_list_li > input,
.woof_container_product_cat li.woof_childs_list_li > div[class*="icheckbox_"],
.woof_container_product_cat li.woof_childs_list_li > div[class*="iradio_"] {
    display: none !important;
}
.woof_container_product_cat li.woof_childs_list_li > label {
    font-weight: bold;
    cursor: pointer;
}

For one top-level category only, use its term class, for example .woof_term_123 > div[class*="icheckbox_"]. Items below the first level have no term class; target them through the list of their parent: ul.woof_childs_list_123 > li.woof_childs_list_li > div[class*="icheckbox_"].

The name of a parent is still a label of its hidden input, so a click on it would select the parent. The next snippet turns that click into opening and closing the branch. It is also the answer when you keep the checkboxes but want a click on the category name to expand its subcategories:

/*
 * Product filter: a click on the name of a parent category opens or closes
 * its subcategories instead of selecting it.
 * Child theme functions.php or a code snippets plugin.
 */
add_action('wp_footer', function () {
    ?>
    <script>
    document.addEventListener('click', function (e) {
        var label = e.target.closest ? e.target.closest('.woof_container_product_cat li.woof_childs_list_li > label') : null;
        if (!label) {
            return;
        }
        e.preventDefault(); // do not select the parent
        e.stopPropagation();
        jQuery(label).siblings('.woof_childs_list_opener').find('span').trigger('click');
    }, true);
    </script>
    <?php
}, 100);

For another taxonomy replace product_cat with its slug, for example .woof_container_pa_brand.

Subcategories not indented, or on one line

The plugin indents each list of subcategories by 17 px. When subcategories line up under the parent as if they were parents themselves, a theme rule for lists in widgets or sidebars removes that padding with a stronger selector. Restore it:

.woof .woof_block_html_items .woof_childs_list {
    padding: 0 0 0 17px !important;
}

Subcategories side by side in one row usually come from the block option Display items in a row: it puts every term without children inline, subcategories included. Set it to No for a tree. If the option is off, look for a theme rule that gives lists display: flex or list items display: inline-block.

If the plus and minus of the openers are hard to see on your background, replace them on the Design tab with Image for subcategories open (the plus) and Image for subcategories close (the minus). They are displayed in a 13×13 px box, so use simple, high-contrast icons.

Drop-down category filters

The Drop-down and Multi drop-down types cannot fold subcategories. The single drop-down indents subcategories with spaces; the multi drop-down lists them flat. Every subcategory option carries a class with its depth, woof-padding-1, woof-padding-2 and so on, while top-level options have none. Browsers allow little styling of native options, but with Drop-down skin set to Chosen the classes reach the visible list, so you can mark the levels:

/* Category drop-downs with the Chosen skin: bold top level */
.woof_container_product_cat .chosen-results li.active-result:not([class*="woof-padding-"]) {
    font-weight: bold;
}
/* Multi drop-down: indent subcategories by level */
.woof_container_mselect.woof_container_product_cat .chosen-results li.woof-padding-1 { padding-left: 20px; }
.woof_container_mselect.woof_container_product_cat .chosen-results li.woof-padding-2 { padding-left: 34px; }

If visitors should choose a parent first and then its subcategory in a second drop-down, use the Hierarchy drop-down extension; the article on a year, make and model filter shows the setup.

Checklist

  • The category block uses the Checkbox or Radio type and Hide childs in checkboxes and radio is Yes.
  • The default state was checked in a private window, not in a browser that remembers opened branches.
  • The branch snippet is in place if branches must follow the selection instead of the visitor's earlier clicks.
  • Block toggles are set in the taxonomy's additional options, the title is shown, and woof_block_toggle_state is used only if the default open-on-selection is not wanted.
  • Custom CSS uses the hyphenated skin class or the [class*="icheckbox_"] form, and page and browser caches were cleared after each change.

If the tree still does not behave as described, open a topic on the HUSKY support forum with a link to the filter page and the settings of the category block.