Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide custom tab when no content is present in woocommerce products

Is there a way I can hide the custom tabs if there is no content present in the field box. I am implementing this with advanced custom fields plugin. So far the tab is still present even if there is no content placed

Here is the code that I have placed in my functions.php

add_filter( 'woocommerce_product_tabs', 'woo_new_direction_tab' );
function woo_new_direction_tab( $tabs ) {

// Adds the new tab

    $tabs['direction_tab'] = array(
        'title'     => __( 'Direction', 'woocommerce' ),
        'priority'  => 60,
        'callback'  => 'woo_new_direction_tab_content'
    );

    return $tabs;

}


function woo_new_direction_tab_content() {

    // The new tab content

    echo the_field('directions');

}

UPDATE

//Direction Tab
add_filter( 'woocommerce_product_tabs', 'woo_new_direction_tab' );
function woo_new_direction_tab( $tabs ) {

// Adds the new tab

    $tabs['direction_tab'] = array(
        'title'     => __( 'Direction', 'woocommerce' ),
        'priority'  => 60,
        'callback'  => 'woo_new_direction_tab_content'
    );


    return $tabs;





}


function woo_new_direction_tab_content() {

    if( get_field('directions') )
    {
        echo the_field('directions');
    }

    else
    {
        echo "<style>li.direction_tab_tab{ display:none !important; }</style>";
    }

}
like image 890
clestcruz Avatar asked Nov 12 '14 10:11

clestcruz


People also ask

How do I add a custom tab to a WooCommerce product page?

Add a tab to the product data metabox We have set the priority to 100 in order for our new tab to appear after the default Advanced tab. Next up we need to add content to the tab, the tab's content will be the two custom fields mentioned earlier, one for the custom tab's title and one for the selected page's ID.


1 Answers

This code works for me in 2021. It only adds a custom tab when the desired field is full. If the field is empty, it hides the tab without a trace.

    //Add a custom product data tab

add_filter( 'woocommerce_product_tabs', 'woo_new_custom_tab' );
function woo_new_custom_tab( $tabs ) {
    global $post, $product;
    
    // Adds the new tab

    if( get_field('field_123') ) {
        $tabs['custom_tab'] = array(
            'title'     => __( 'Custom Tab', 'woocommerce' ),
            'priority'  => 25,
            'callback'  => 'woo_new_custom_tab_content'
        );
    }
    
    return $tabs;

}

   //Add content to a tab and hide it if it is empty

function woo_new_custom_tab_content() {
    global $post, $product;
    
    if( get_field('field_123') ) {
        echo '<h2>Custom Tab</h2>';
        echo '<p>'.get_field('field_123',$post->ID).'</p>';
    }
}
like image 51
Р. Р. Avatar answered Oct 10 '22 00:10

Р. Р.