WooCommerce Accordion Style Expand/Collapse Product Category Menu

Here is a jQuery solution for a WooCommerce accordion style menu for the product category menu that can be assigned with a WooCommerce widget, the menu works best when all product categories are assigned to a parent category.

The Accordion menu will then expand and collapse on click with the first parent category appearing expanded, the count span also has a link to the child category archive page. I have come across the original code (which I have modified)  on a site with no credit/author reference.

The toggle icons have been marked up with FontAwesome5 regular free icons, so either have that loaded or change the markup for something else.

jQuery(document).ready(function($) {
* WooCommerce Product Category Accordion jQuery Menu
* @link https://wpbeaches.com/woocommerce-accordion-style-expand-collapse-product-category-menu/
if ($('ul.product-categories').length > 0) {
// Set variables
// pC = Parent Category
// fpC = First Parent Category
// cC = Current Category
// cCp = Currents Category's Parent
pC = $('.product-categories li.cat-parent'),
fpC = $('.product-categories li.cat-parent:first-child'), // Start this one open
cC = $('.product-categories li.current-cat'),
cCp = $('.product-categories li.current-cat-parent');
pC.prepend('<span class="toggle"><i class="far fa-minus-square fa-plus-square"></i></span>');
pC.parent('ul').addClass('has-toggle'); pC.children('ul').hide();
if (pC.hasClass("current-cat-parent")) {
cCp.addClass('open'); cCp.children('ul').show(); cCp.children().children('i.far').removeClass('fa-plus-square');
else if (pC.hasClass("current-cat")) {
cC.addClass('open'); cC.children('ul').show(); cC.children().children('i.far').removeClass('fa-plus-square');
else {
fpC.addClass('open'); fpC.children('ul').show(); fpC.children().children('i.far').removeClass('fa-plus-square');
$('.has-toggle span.toggle').on('click', function() {
$t = $(this);
if ($t.parent().hasClass("open")){
$t.parent().removeClass('open'); $t.parent().children('ul').slideUp(); $t.children('i.far').addClass('fa-plus-square');
else {
// Link the count number
$('.count').css('cursor', 'pointer');
$('.count').on('click', function(event) {
view raw pc-menu.js hosted with ❤ by GitHub

1 Comment

  1. D on February 28, 2019 at 4:35 pm

    Sweet, works lika a charm.
    -How to keep them all closed by default?

Leave a Comment