• Resolved mayberrysoapcompany

    (@mayberrysoapcompany)


    I have my settings set to hide items that are sold out, but the “empty” categories are still visible. I cannot find how to fix this where the categories automatically hides when they are sold out. I am fully up to date with woocommerce and all of my plugins, I have a purchased theme from Theme Forest that is up to date.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter mayberrysoapcompany

    (@mayberrysoapcompany)

    Found code that fixed it at least for now.

    hrn1995 (woo-hc)

    (@aguinaldodarla)

    Hi @mayberrysoapcompany

    That’s great to know. We’d appreciate it if you could also share how you solved the issue, to also help other members having similar concern.

    Thread Starter mayberrysoapcompany

    (@mayberrysoapcompany)

    I cannot remember the website I found the code on so I cannot give credit (I went to so many and found this around midnight the other night, but you take this code and add it to the functions.php in the theme editor at the end –

    function nav_remove_empty_category_menu_item ( $items, $menu) {
    
        if ( ! is_admin() ) {
            
            $args = array(
            'hide_empty' => false,
            'hierarchical' => true,
        );
    
        $product_categories = get_terms( 'product_cat', $args );
    
        $exclude = array();
        foreach ( $product_categories as $category ) {
    
            $posts         = get_posts( array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => $category->slug, 'fields' => 'ids' ) );
            $show_category = false;
    
            foreach ( $posts as $post ) {
    
                $product         = new wC_Product( $post );
                $visible_product = $product->is_visible();
    
                if ( true === $visible_product ) {
                    $show_category = true;
                    break;
                }
    
            }
    
            if ( false === $show_category ) {
                $exclude[] = $category->term_id;
            }
    
        }
    
            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, $exclude ) ) ) {
    
                    unset( $items[$key] );
    
                }
    
            }
    
        }
    
        return $items;
    
    }
    
    add_filter( 'wp_get_nav_menu_items', 'nav_remove_empty_category_menu_item', 10, 3 );
     
    hrn1995 (woo-hc)

    (@aguinaldodarla)

    Thank you, @ebphillip

    This will help others with similar concerns. Please don’t hesitate to start a new topic if you have any more questions down the line.

    Have a fantastic day!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Need help hiding categories that are sold out’ is closed to new replies.