• Resolved strozzapr

    (@strozzapr)


    Hi!

    I’m looking to create a function where we would be able to hide a tab if it’s empty (I’ve automatically imported my products and it created a tab for every single one of them). However some products don’t use certain tabs yet are still displayed (empty).

    Here’s one of my tabs name and ID : “Version Numérique”, ID : 2

    Perhaps there’s also a function to only display tab when there’s content instead?

    Thank you kindly!

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Hi @strozzapr!

    Some people programmatically add content to tabs so we need to allow empty tabs to show.

    Here’s a function that will remove any empty tabs:

    // Remove Empty Tabs
    add_filter( 'woocommerce_product_tabs', 'yikes_woo_remove_empty_tabs', 20, 1 );
    
    function yikes_woo_remove_empty_tabs( $tabs ) {
    
    	if ( ! empty( $tabs ) ) {
    		foreach ( $tabs as $title => $tab ) {
    			if ( empty( $tab['content'] ) ) {
    				unset( $tabs[ $title ] );
    			}
    		}
    	}
    	return $tabs;
    }

    Cheers,
    Kevin.

    Edit: note that this function will remove tabs when the product page is displayed. It won’t remove the tabs from the DB/back-end.

    Thread Starter strozzapr

    (@strozzapr)

    Works like a charm!

    Thank you Kevin ! ??

    Jon

    (@freshyjon)

    That function seems to also remove the Description tab, even when there is info in that tab…

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Hey @freshyjon,

    Unless the description tab’s content is being populated dynamically, I don’t see how that function could be removing it. The description tab has the same content field that other tabs have and all we’re looking at is if that field is empty.

    Do you know if your description tab is being populated dynamically or if there is any other sort of customization going on?

    Jon

    (@freshyjon)

    Not sure. Pretty sure it’s using regular functionality. I’m using Divi + WooCommerce. As soon as I add that function, it removes the Description tab.

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Hm, let’s try to explicitly keep the description tab just to see if that works.

    // Remove Empty Tabs
    add_filter( 'woocommerce_product_tabs', 'yikes_woo_remove_empty_tabs', 20, 1 );
    
    function yikes_woo_remove_empty_tabs( $tabs ) {
    
    	if ( ! empty( $tabs ) ) {
    		foreach ( $tabs as $title => $tab ) {
    			if ( empty( $tab['content'] ) && strtolower( $tab['title'] ) !== 'description' ) {
    				unset( $tabs[ $title ] );
    			}
    		}
    	}
    	return $tabs;
    }
    Jon

    (@freshyjon)

    That works!

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Awesome! Not sure why the empty check is failing for you but you should be good with that version of the snippet.

    deeoon

    (@deeoon)

    Hi,

    How can I add this function on my website ?
    I want to Hide the description tab.
    Thanks !

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Hi @deeoon,

    This is not the snippet you should use if you want to hide the description tab. There are dozens of other support tickets in this forum related to hiding WooCommerce’s default tabs. This is the snippet you want for that:

    // Remove Tabs.
    add_filter( 'woocommerce_product_tabs', 'yikes_woo_remove_tabs', 20, 1 );
    
    function yikes_woo_remove_tabs( $tabs ) {
    
    	// Remove the description tab.
        if ( isset( $tabs['description'] ) ) {
        	unset( $tabs['description'] );
        }
    
        return $tabs;
    }

    The best place for this code is your child theme’s functions.php file. If you’re not using a child theme, you can use your theme’s functions.php file or a plugin like ?My Custom Functions.

    If you’re not familiar with adding code to your site, I think using the plugin is probably the best idea because it provides some safety measures.

    All the best,
    Kevin.

    Hi, I tested your 1st code but it does’nt work, would there be an update to do to code?

    thank you

    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Hi @xavier0623,

    What tab are you trying to hide?

    Let me know.

    Thank you,
    Kevin.

    hi, thank you for your answer, I would like to hide the woocommerce tabs if they are empty. I have other tabs, not generated by woocommerce, is it possible to hide them too?

    if you wish we can exchange by mail this may be easier

    my mail: [email protected]

    thank you advance

    • This reply was modified 5 years, 10 months ago by xavier0623.
    Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Hi @xavier0623,

    Which tabs are you referring to? WooCommerce won’t show their default tabs (Description, Additional Information) if they’re empty. Also, you can control the Reviews tab on a per-product (or per-site, I believe) basis.

    Are the tabs that you’re trying to hide (that could be empty) coming from our plugin?

    Let me know.

    Thank you,
    Kevin.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Hide tabs when empty’ is closed to new replies.