Support » Plugin: Custom Product Tabs for WooCommerce » i need this php code

  • Resolved danna556

    (@danna556)


    How do I conditionally hide custom tabs if there is no advanced custom field data present for a product?

    For example, if a product does not have data for any Advanced Custom Fields in a field group, I would like the tab to be hidden. Currently, empty tabs are still showing, even if there is no product data to display.
    * can you give me the same code *

Viewing 1 replies (of 1 total)
  • Plugin Contributor yikesitskevin

    (@yikesitskevin)

    Hi @danna556,

    You’ll need to update this function in a few ways to match your use case.

    1. Replace ‘your_post_meta_key’ with the meta key of your ACF field.

    2. Replace ‘YOUR-TAB-SLUG’ with the name of the tab you want to hide if the ACF field is empty.

    You can check as many ACF fields and unset as many tabs as you’d like.

    add_filter( 'woocommerce_product_tabs', 'yikes_woo_maybe_hide_tabs', 20, 1 );
    
    function yikes_woo_maybe_hide_tabs( $tabs ) {
    	global $post;
    
    	if ( empty( $post ) || empty( $post->ID ) ) {
    		return $tabs;
    	}
    
    	// Get your ACF post meta here:
    	$acf_field = get_post_meta( $post->ID, 'your_post_meta_key', true );
    
    	if ( empty( $acf_field ) ) {
    		unset( $tabs['YOUR-TAB-SLUG'] ); // e.g. additional_information, description, etc.
    	}
    
    	return $tabs;
    }

    Hope that makes sense.

    Let me know.

    Cheers,
    Kevin.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.