only show custom tabs if not empty?
-
add_filter( ‘woocommerce_product_tabs’, ‘woo_new_product_tab_1’ );
function woo_new_product_tab_1( $tabs ) {$tabs[‘downloads’] = array(
‘title’ => __( ‘Downloads’, ‘woocommerce’ ),
‘priority’ => 40,
‘callback’ => ‘show_downloads’
);return $tabs;
}
function show_downloads() {
echo get_field( ‘downloads’ );
}
-
Sorry I missed the message to the post:
Hi,
I added a custom function to add custom product tabs for woocommerce, that grab content from “advanced custom fields” plugin. It ist working fine. Problem is, I want to hide the complete tab if empty.I found several tutorials that are hiding tabs if empty. Problem is my shop has a lot of products and all infos are already added in advanced custom fields, so i have to go with this solution.
how can I add this function to my code above?
No one?!
Hi @christianslater, you may try add below code to functions.php:
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab_1' ); function woo_new_product_tab_1( $tabs ) { if( !empty(get_field( 'downloads' )) ){ $tabs['downloads'] = array( 'title' => __( 'Downloads', 'woocommerce' ), 'priority' => 40, 'callback' => 'show_downloads' ); } return $tabs; } function show_downloads() { echo get_field( 'downloads' ); }
Hi, thanks for your reply!
I tried but it gives me a complete blank page on product page…
Any Idea how to solve this?! pease 🙁
“Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error.”
http://php.net/manual/en/function.empty.phpOK, as I am no PHP expert. Is there any other solution?
It looks like your issue is unique so it will need some custom php code.
If your php version is <5.5, then terrytsang’s solution may be failing with the empty() function, if so it needs rewriting like this:
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab_1' ); function woo_new_product_tab_1( $tabs ) { $var = get_field( 'downloads' ); if( !empty($var) ){ $tabs['downloads'] = array( 'title' => __( 'Downloads', 'woocommerce' ), 'priority' => 40, 'callback' => 'show_downloads' ); } return $tabs; } function show_downloads() { echo get_field( 'downloads' ); }
I’m sorry I can’t test it because I don’t have the other components that you have in your system.
Without php skills you may need to hire someone:
http://jobs.wordpress.net/thanks. works perfectly!
Hi lorro,
thanks again for your help. It works perfectly. I have another question. I want to add another custom field in the same tab. So If I input another Video it shows under the first one.How can I add the custom field “einbau_video_2” to that code?
If I just add the custom field to the echo section is is showing, even if there’s no video
function show_video() { echo '<div class="video-container">'; echo get_field( 'einbau_video' ); echo '</div>'; echo '<div class="video-container">'; echo get_field( 'einbau_video_2' ); echo '</div>';}
This is my actual code.
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab_2' ); function woo_new_product_tab_2( $tabs ) { $var = get_field( 'einbau_video' ); if( !empty($var) ){ $tabs['video'] = array( 'title' => __( 'Video', 'woocommerce' ), 'priority' => 20, 'callback' => 'show_video' ); } return $tabs; } function show_video() { echo '<div class="video-container">'; echo get_field( 'einbau_video' ); echo '</div>'; }
Any help is appreciated.
function show_video() { $field = get_field( 'einbau_video' ) if (!empty($field)) { echo '<div class="video-container">'; echo $field; echo '</div>'; } }
Again I am unable to test it. You can use var_dump($field); to find out exactly what is in the $field variable.
Thanks again,
I think thats the right way.
How can I add “show_video_2” to the callback
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab_2' ); function woo_new_product_tab_2( $tabs ) { $var = get_field( 'einbau_video' ); if( !empty($var) ){ $tabs['video'] = array( 'title' => __( 'Video', 'woocommerce' ), 'priority' => 20, 'callback' => 'show_video' ); } return $tabs; }
that’s what I got so far:
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab_2' ); function woo_new_product_tab_2( $tabs ) { $var = get_field( 'einbau_video' ); if( !empty($var) ){ $tabs['video'] = array( 'title' => __( 'Video', 'woocommerce' ), 'priority' => 20, 'callback' => 'show_video' ); } return $tabs; } function show_video() { echo '<div class="video-container">'; echo get_field( 'einbau_video' ); echo '</div>'; } function show_video_2() { $field = get_field( 'einbau_video_2' ); if (!empty($field)) { echo '<div class="video-container">'; echo $field; echo '</div>'; } }
$tabs['video']['callback'] = 'show_video_2';
mhm sorry I don’t understand. I want to show “einbau_video_2” in the same tab just below “einbau_video”. Where do I have to put this code?
Sorry for my dumb question…
Oh, forget my last then. A tab can have only one callback function, so everything you want the tab to show will have to be in show_video(). Putting the two together:
function show_video() { // video 1 echo '<div class="video-container">'; echo get_field( 'einbau_video' ); echo '</div>'; // video 2 $field = get_field( 'einbau_video_2' ); if (!empty($field)) { echo '<div class="video-container">'; echo $field; echo '</div>'; } }
- The topic ‘only show custom tabs if not empty?’ is closed to new replies.