1

I'm trying to figure out how to hide a custom tab that was created in the theme customizer settings by the theme when the tab is empty.

Here is the code that displays the custom tab on my product page:

<a class="rey-summaryAcc-accItem --active" href="#acctab-custom_tab_0"> <span>Shipment Parts</span>

and

<div class="rey-summaryAcc-item rey-summaryAcc-item--custom_tab_0 " id="acctab-custom_tab_0" role="tabpanel" aria-labelledby="acctab-title-custom_tab_0" style="display: block;"><div class="__inner"> <input type="hidden" id="show_price" value="yes">

I tried using this code from this post but my tab is still showing.

    // Add a custom product tab on single product pages
add_filter( 'woocommerce_product_tabs', 'woo_new_tab' );
function woo_new_tab( $tabs ) { 
    $values = get_field('acctab-custom_tab_0');

    // Adds the new tab 
    if ( ! empty($values) ) {
        $tabs['acctab-custom_tab_0'] = array(
            'title'     => __( 'Test', 'woocommerce' ),
            'priority'  => 20,
            'callback'  => 'woo_new_tab_content'
        );
    }
    return $tabs;
}

// Displays the tab content 
function woo_new_tab_content() {
    $values = (array) get_field('acctab-custom_tab_0');

    echo '<ul>';

    foreach ( $values as $value ) {
        echo '<li>' . $value . '</li>';
    }
    echo '</ul>';
}

What am I doing wrong?

dyz
  • 31
  • 4

1 Answers1

1

To hide the custom tab when it is empty, you can modify your code like this:

// Add a custom product tab on single product pages
add_filter( 'woocommerce_product_tabs', 'woo_new_tab' );
function woo_new_tab( $tabs ) { 
    $show_price = get_field('show_price');

    // Adds the new tab 
    if ( ! empty($show_price) ) {
        $tabs['acctab-custom_tab_0'] = array(
            'title'     => __( 'Test', 'woocommerce' ),
            'priority'  => 20,
            'callback'  => 'woo_new_tab_content'
        );
    }
    return $tabs;
}

// Displays the tab content 
function woo_new_tab_content() {
    $values = (array) get_field('acctab-custom_tab_0');

    echo '<ul>';

    foreach ( $values as $value ) {
        echo '<li>' . $value . '</li>';
    }
    echo '</ul>';
}

Just be sure you have the ACF (Advanced Custom Fields) plugin installed and configured correctly, and that the field show_price is correctly set and returns the desired value.

Mike D.
  • 116
  • 6