Skip to content

Instantly share code, notes, and snippets.

@DEBA1801
Last active March 26, 2024 08:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DEBA1801/cc34c3ae9f73de1b8497dd60ff16789e to your computer and use it in GitHub Desktop.
Save DEBA1801/cc34c3ae9f73de1b8497dd60ff16789e to your computer and use it in GitHub Desktop.
WooCommerce | Accordion style child categories with custom toggle in the sidebar widget | Useful for a large number of categories
/* - woo cat toggling elements, injected via jQuery - */
/* make list item be relative, to be able to position toggle within this item, if desired */
#secondary .widget_product_categories ul.product-categories > li.cat-parent {
position: relative;
}
/* the new toggle element wrapper, which is added via jQuery */
#secondary .widget_product_categories ul.product-categories > li.cat-parent .woo-cat-toggle {
cursor: pointer;
float:right;
display: inline-block;
text-align: center;
margin-left: 0.5em;
width: 1.5em;
line-height: 1em;
-webkit-transform: rotate(-90deg);
transform: rotate(-90deg);
transition: all 0.4s ease;
width: 20px;
height: 20px;
background: rgba(0,0,0,0.05);
text-align: center;
line-height: 18px;
border-radius: 50%;
}
/* when it's popped, style the toggle wrapper differently */
#secondary .widget_product_categories ul.product-categories > li.cat-parent .woo-cat-toggle.cat-popped {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
background: rgba(85,85,85,0.8);
color: white;
}
/* toggle icon */
#secondary .widget_product_categories ul.product-categories > li.cat-parent .woo-cat-toggle::before {
font-weight: normal;
font-style: normal;
font-size: 19px;
text-transform: none;
speak: none;
content: '+';
line-height: 20px;
width: 20px;
height: 20px;
text-align: center;
}
/* toggle icon when triggered */
#secondary .widget_product_categories ul.product-categories > li.cat-parent .woo-cat-toggle.cat-popped::before {
content: '\2013';
}
/* hide sub cats by default */
#secondary .widget_product_categories ul.product-categories > li.cat-parent .woo-cat-toggle ~ ul.children {
overflow: hidden;
max-height: 0;
transition: all 0.4s ease;
}
/* show sub cats when triggered via jQuery toggle */
#secondary .widget_product_categories ul.product-categories > li.cat-parent .woo-cat-toggle.cat-popped ~ ul.children {
max-height: 400px;
}
jQuery(function($){
// add a new toggle element after the parent category anchor
$( "<div class='woo-cat-toggle'></div>" ).insertAfter( "#secondary .widget_product_categories .product-categories > .cat-item.cat-parent > a" );
// when the new toggle element is clicked, add/remove the class that controls visibility of children
$( "#secondary .widget_product_categories .product-categories .woo-cat-toggle" ).click(function () {
$(this).toggleClass("cat-popped");
});
// if the parent category is the current category or a parent of an active child, then show the children categories too
$('#secondary .widget_product_categories .product-categories > .cat-item.cat-parent').each(function(){
if($(this).is('.current-cat, .current-cat-parent')) {
$(this).children('.woo-cat-toggle').toggleClass("cat-popped");
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment