HUSKY - WooCommerce Product Filter Professional

Different WooCommerce Filter for Mobile and Desktop: How to Set It Up

Most shops want the product filter to behave one way on a computer and another way on a phone: open blocks and instant results on a wide screen; folded blocks, less scrolling and a Filter button on a small one. HUSKY has no separate page of mobile settings, but a different WooCommerce filter for mobile and desktop takes only a few settings and short snippets. What usually goes wrong is not the snippet but the place where the device is detected, so start there.

Two ways to tell a phone from a computer

On the server. WordPress has wp_is_mobile(), which reads the browser identification sent with each request. HUSKY uses it for mobile mode, for the values Yes, but only for mobile devices and Yes, but only for desktop of Set filter automatically, and for the Mobile behavior of the slideout. The PHP snippets below use it too. A decision made on the server can change anything, including which form is in the page at all. It has two costs:

  • A page cache must keep separate copies for phones and computers. Otherwise every visitor gets the version built for whoever came first. The settings for common cache plugins are in the guide to the filter not showing on mobile.
  • It is about the device, not the screen. iPads and some Android tablets identify themselves as computers and get the desktop version, and a narrowed desktop browser window never becomes "mobile".

In the browser. CSS media queries and a few lines of JavaScript can check the screen width. That works with any cache and follows your theme's breakpoints, but it can only change how the forms already in the page look and behave. It cannot add or remove a form.

Rule of thumb: every snippet below that uses wp_is_mobile() needs a page cache with a separate mobile copy, or shop pages excluded from the cache. Where a browser-side variant exists, it is given as well.

Collapse filter blocks on mobile only

A filter block can fold under its title with a +/- toggle. Switch the toggle on first: on the Structure tab open the additional options of the taxonomy (the cog button) and set the toggle option to Yes, show as closed. In 3.4.6 that option is labelled Show toggle button for radio in the taxonomy popup, but it controls the block of that taxonomy whatever its type; the price filter has the same option as Show toggle button. The block title must be visible, because the toggle sits in it. Toggles and category trees are covered in detail in collapsible category tree in the filter.

On its own this closes the blocks on every device. HUSKY passes the state of each block through the woof_block_toggle_state hook, so one condition opens them on computers:

/*
 * HUSKY: filter blocks open on computers, closed on phones.
 * The block toggle must be set to "Yes, show as closed".
 * Child theme functions.php or a code snippets plugin.
 */
add_filter('woof_block_toggle_state', function ($block_is_closed) {
    if (!wp_is_mobile()) {
        return false; // computers: every block starts open
    }
    return $block_is_closed; // phones: the plugin's own state
});

Phones keep the plugin's own state: blocks are closed, except a block that contains a selected term, so after filtering the visitor still sees what is chosen. The example on the hook page forces every block closed on mobile, which also folds blocks that have a selection. The hook acts only on blocks whose toggle is switched on, and it receives only the state, not the block name, so it applies to all of them.

Fold the whole form behind one button on phones

Instead of single blocks, you can hide the whole form behind a show/hide button. The [woof] shortcode has the attribute autohide=1 for that. To switch it on for phones only, set it in the woof_filter_shortcode_args hook, which receives the attributes of every HUSKY form before it is drawn, widgets and front builder forms included:

/*
 * HUSKY: on phones the filter form starts folded behind a button.
 * Child theme functions.php or a code snippets plugin.
 */
add_filter('woof_filter_shortcode_args', function ($args) {
    if (wp_is_mobile()) {
        $args['autohide'] = 1;
    }
    return $args;
});

The button takes its look from the Design tab: Auto filter close/open image and Auto filter close/open text. Write none in the image field to show the text only. In AJAX mode the fold is outside the part of the form that is redrawn, so it stays open while the visitor filters; with page reloads every page starts folded. For a full-screen panel behind an Open filter button, use mobile mode instead, explained in the mobile filter guide.

Autosubmit on desktop, Filter button on mobile

Autosubmit, filtering on every change, is comfortable with a mouse and tiring on a phone, where each tap reloads the products and moves the page. The obvious idea, two forms with different Autosubmit settings shown by screen size, fails: the script reads the autosubmit setting once, from the first HUSKY form on the page, and uses it for all of them. One form that changes its behaviour is the reliable way.

By screen width, with any cache. Give the form Autosubmit = No, so its Filter button is always printed. That is the Autosubmit option on the Options tab for forms that use the default, the widget's own Autosubmit field, autosubmit=0 in the shortcode, or the Autosubmit switch of a front builder form. Then let wide screens filter on every change and hide the button there:

/*
 * HUSKY: autosubmit on wide screens, Filter button on narrow ones.
 * The form itself must have Autosubmit = No.
 * Use your theme's breakpoint instead of 769px (twice).
 * Child theme functions.php or a code snippets plugin.
 */
add_action('wp_footer', function () {
    ?>
    <style>
        @media (min-width: 769px) {
            .woof .woof_submit_search_form { display: none; }
        }
    </style>
    <script>
        document.addEventListener('woof_init_search_form', function () {
            if (typeof woof_autosubmit !== 'undefined') {
                woof_autosubmit = window.matchMedia('(min-width: 769px)').matches ? 1 : 0;
            }
        });
    </script>
    <?php
}, 99);

It works because HUSKY checks the woof_autosubmit variable at the moment of every change, and fires the woof_init_search_form event after reading its own settings and again after each AJAX redraw of the form. The Reset button stays visible. On phones the price slider and every other element wait for the Filter button.

By device, on the server. If your cache keeps separate mobile copies, keep Autosubmit = Yes and turn it off for phones. Computers then get no button at all:

/*
 * HUSKY: Filter button on phones, autosubmit everywhere else.
 * Child theme functions.php or a code snippets plugin.
 */
add_filter('woof_filter_shortcode_args', function ($args) {
    if (wp_is_mobile()) {
        $args['autosubmit'] = 0;
    }
    return $args;
});

A different filter form for mobile and desktop

Sometimes the phone needs other filters, not only other behaviour: fewer blocks, no price slider, a drop-down instead of a long list. Then build two forms, but make sure only one of them is in the page. Two forms hidden with CSS, or with a page builder's responsive visibility such as "hide on mobile", are both still in the HTML, and they interfere:

  • the autosubmit setting of the first form applies to both, as described above;
  • radio buttons of the same taxonomy in both forms share one name, so the browser lets only one of them be checked: selections jump, or a category tree folds up after a tap;
  • if both forms use mobile mode, two Open filter buttons appear and one of them opens the form you cannot see.

One shortcode that picks the form on the server. Register a small shortcode and put it where the filter goes: a Shortcode block in a sidebar, a Shortcode widget in a page builder, or a theme template.

/*
 * [device_filter]: one HUSKY form for phones, another for computers.
 * Child theme functions.php or a code snippets plugin.
 */
add_shortcode('device_filter', function () {
    if (wp_is_mobile()) {
        // phones: categories and colour only (no price or other items), folded, Filter button
        return do_shortcode('[woof sid="phone" tax_only="product_cat,pa_color" by_only="none" autohide=1 autosubmit=0]');
    }
    // computers: the full form, filtering on every change
    return do_shortcode('[woof sid="desktop" autosubmit=1]');
});

Use your own attributes; all of them are listed in the [woof] shortcode reference. Forms made with the front builder work the same way: return [woof_front_builder name="..."] with a different form name for each device. If a form uses mobile mode, keep its sid to one word.

Widgets. The same rule applies to two HUSKY widgets in one sidebar: a widget visibility plugin must leave the widget out of the page on the server, not add a CSS class that hides it. To check, open the page in the browser's device emulation, reload it and search the page source for woof_redraw_zone: every HUSKY form has exactly one.

Above the products on phones, in the sidebar on computers

Many themes move the sidebar below the products on phones. HUSKY can place a form above the products for phones only: Options → Set filter automatically → Yes, but only for mobile devices, and on the Design tab Hide auto filter by default → Yes to fold it behind a button. This form is printed through the standard WooCommerce shop loop, so it appears on shop, category and other product archives of themes that use that loop. The sidebar widget still prints its own form on phones, below the products, so leave it out there with a visibility plugin or with this snippet:

/*
 * HUSKY: do not print HUSKY widgets on phones
 * (phones get the automatic filter or a slideout instead).
 * Child theme functions.php or a code snippets plugin.
 */
add_filter('widget_display_callback', function ($instance, $widget) {
    if ($widget instanceof WOOF_Widget && wp_is_mobile()) {
        return false;
    }
    return $instance;
}, 10, 2);

For a panel that slides in from the screen edge, the Slideout extension has the same split built in: its Mobile behavior option offers Show only on mobile and Show only on desktop.

Close the theme's popup or off-canvas panel after filtering

HUSKY's own mobile panel closes by itself after AJAX filtering, so the visitor sees the new products. A drawer, popup or off-canvas sidebar made by the theme or a page builder is not part of HUSKY and does not know that filtering has finished. Without AJAX this does not matter, because the page reloads. In AJAX mode the products change behind the drawer, which stays open.

After every AJAX filtering HUSKY fires the JavaScript event woof_ajax_done. Close the drawer from there, usually by clicking its own close button:

/*
 * HUSKY: close the theme's off-canvas filter panel after AJAX filtering.
 * Replace .my-offcanvas-close with the close button of your theme's panel.
 * Child theme functions.php or a code snippets plugin.
 */
add_action('wp_footer', function () {
    ?>
    <script>
        jQuery(document).on('woof_ajax_done', function () {
            jQuery('.my-offcanvas-close').trigger('click');
        });
    </script>
    <?php
}, 99);

Themes that open the sidebar with the Magnific Popup library, Flatsome among them, close it with jQuery.magnificPopup.close(); instead of the click. The line inside the handler can also go into Advanced → Code → JavaScript code after AJAX is done; since 3.4.4 only users allowed to post unfiltered HTML can change that field. Combine this with the Filter button on phones from the section above: with autosubmit, the first tap already filters and closes the drawer before the visitor can choose a second option.

Checklist

  • You decided where the device is detected: wp_is_mobile() on the server, with separate mobile copies in the page cache, or the screen width in the browser.
  • Blocks that should fold on phones have the toggle set to Yes, show as closed, and the woof_block_toggle_state snippet opens them on computers.
  • A form that should wait for a button is printed with Autosubmit = No, and the width snippet turns autosubmit on for wide screens.
  • Each device gets one HUSKY form: one woof_redraw_zone per form in the page source, no second form hidden with CSS.
  • Theme drawers and popups are closed from the woof_ajax_done event.
  • Tested on a real phone, or in device emulation with a reload, logged out, after clearing every cache.

If the filter still behaves the same on both devices, open a topic on the HUSKY support forum with the page link, your widget or shortcode settings and the snippets you added.