Skip to content

Instantly share code, notes, and snippets.

Last active Aug 20, 2021
What would you like to do?
WooCommerce Accordion Style Expand/Collapse Product Category Menu
jQuery(document).ready(function($) {
* WooCommerce Product Category Accordion jQuery Menu
* @link
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'),
fpC = $('.product-categories'), // 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) {
Copy link

deeman commented Feb 4, 2019

Works perfect, may I just ask you; -How to keep it all closed by default? —Thanks.

Copy link

stephkmcg commented Apr 10, 2019

This is exactly the code that I need, but I was just wondering where I paste this in order for it to work.
Sorry for the newbie question!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment