Saturday, January 1, 2022

[FIXED] Hide custom tab when no content is present in woocommerce products

Issue

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>";
    }

}

Solution

There is most likely a better way of doing this, but I've achieved this in the past with the following:

if( get_field('directions') )
{
    echo the_field('directions');
}
else
{
    echo "<style>.direction_tab_tab { display:none !important; }</style>";
}

This will print the contents of the "directions" field if there is text in it, if not it will print the css and hide the tab.



Answered By - Howli

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.