The code below allows a user to hide 'empty' category and subcategory levels on a WooCommerce site when there are no products within a category level, but I have realised this is not the correct way to code this function as it uses the 'product id' as a reference, when really it needs to be referencing and checking itself against the 'stock levels' of any products that may be in said category/subcategory level instead.
Is anyone able to amend the following code to check against stock levels rather than product id's? I have tried, but without success.
function nav_remove_empty_category_menu_item ( $items, $menu, $args ) {
if ( ! is_admin() ) {
global $wpdb;
$nopost = $wpdb->get_col( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE count = 0" );
foreach ( $items as $key => $item ) {
if ( ( 'taxonomy' == $item->type ) && ( in_array( $item->object_id, $nopost ) ) ) {
unset( $items[$key] );
}
}
}
return $items;
}
add_filter( 'wp_get_nav_menu_items', 'nav_remove_empty_category_menu_item', 10, 3 );