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>";
}
}
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.
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>';
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With