Site logo

Archived topic

GP simple mega menu with dynamic woo subcategories?

5 replies · Started by Axel on October 28, 2021

Viewing posts 1–6 of 6

hi there,

actually trying to resolve a 'problem' with the simple mega menu we buildt with the GP css.
works great, BUT:
the customer now often changes the woo subcategories which causes a lot of work to update the menu every time ...

so it would be ideal to have the menu grandchilds for the subcategories loaded dynamically for appropriate auto update.
menu is buildt like so with five childs in the simple mega:

menu

the child level is defined manually for the demanded main categories - this is o.k.
but the grandchilds should reflect all the subcategories of the parent level on an automatic basis if possible in any way.

tried a few approaches including mega menu plugins and code but nothing seems to work anyway near so far.

so - any idea or hint to get this done in a proper and sleak way???

thanks for hints and help in advance.

hi david,

thanks for the hint (link) - although it looks somewhat similar to an already tested approach...
i'll try to dig into it.

yes - theres a few plugins that are eighter seemingly outdatet, dont work very well or are bloating the code awfully.
surprised anyway that theres no proper buildt in way to accomplish this.

thanks - i'll be back on occasion :)

Glad to be of help.

... o.k. drives me nuts, sadly.

davids link above seems to be not bad at all. replacing category with product_cat gives back "no categories" in the expected places within the menu - so it basically works ...

but cant bypass this. eighter it gives the "no categories" text or nothing at all.
assuming theres a call for the wrong woo terms somewhere. but my php is far too limited to get over it.

anybody out there who has more ideas than me? or solved this problem somehow?

this is the actual code that gives the "no categories" line:

add_filter( 'walker_nav_menu_start_el', 'xx_add_subcategories_under_parent_category', 10, 4 );
function xx_add_subcategories_under_parent_category( $item_output, $item, $depth, $args ) {
	if( $args->theme_location == 'primary' && $item->object === 'product_cat' && $item->type === 'taxonomy' ) {
		$sub_cats = wp_list_categories( [ 'echo' => 0, 'hide_empty' => 1, 'child_of' => $item->object_id, 'title_li' => '' ] );
		if( ! empty( $sub_cats ) ) {
			$class = "menu-item menu-item-object-{$item->object} menu-item-type-{$item->type}";
			$sub_cats = preg_replace( '#<ul\sclass=([^>]*)>#', '<ul class="sub-menu">', $sub_cats );
			$sub_cats = preg_replace( '#<li\sclass="([^>]*)">#', '<li class="' . $class . ' $1">', $sub_cats );
			$item_output .= '<ul class="sub-menu">' . "\n";
			$item_output .= $sub_cats;
			$item_output .= '</ul>' . "\n";
		}
	}

	return $item_output;
}
This archived topic is closed to new replies.