I just add 4 custom tabs in woocommerce single product page. Now I want to hide these tabs if there is no content. Here is my custom tabs code. How can I do that? Automatically description tab is not showing if there is no content. Another question is for the review tab. Can I hide it also if there is no review or comments?
Thanks in advance.
// Add a custom metabox
add_action( 'add_meta_boxes', 'additional_product_tabs_metabox' );
function additional_product_tabs_metabox()
{
add_meta_box(
'add_product_metabox_additional_tabs',
__( 'Additional product Tabs', 'woocommerce' ),
'additional_product_tabs_metabox_content',
'product',
'normal',
'high'
);
}
// Add custom metabox content
function additional_product_tabs_metabox_content( $post )
{
// Indicated For
echo '<h4>' . __( 'Indicated for', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_indicated_for', true );
wp_editor( $value, '_indicated_for', array( 'editor_height' => 100 ) );
// Caution
echo '<br><hr><h4>' . __( 'Caution', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_caution', true );
wp_editor( $value, '_caution', array( 'editor_height' => 100 ) );
// How To
echo '<br><hr><h4>' . __( 'How To', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_how_to', true );
wp_editor( $value, '_how_to', array( 'editor_height' => 100 ) );
//Part of Cure Series
echo '<br><hr><h4>' . __( 'Part of Cure Series', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_part_of_cure_series', true );
wp_editor( $value, '_part_of_cure_series', array( 'editor_height' => 100 ) );
// Nonce field (for security)
echo '<input type="hidden" name="additional_product_tabs_nonce" value="' . wp_create_nonce() . '">';
}
// Save product data
add_action( 'save_post_product', 'save_additional_product_tabs', 10, 1 );
function save_additional_product_tabs( $post_id ) {
// Security check
if ( ! isset( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
return $post_id;
}
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( ! current_user_can( 'edit_product', $post_id ) ) {
return $post_id;
}
// Sanitize user input and save the post meta fields values.
if( isset($_POST[ '_indicated_for' ]) )
update_post_meta( $post_id, '_indicated_for', wp_kses_post($_POST[ '_indicated_for' ]) );
if( isset($_POST[ '_caution' ]) )
update_post_meta( $post_id, '_caution', wp_kses_post($_POST[ '_caution' ]) );
if( isset($_POST[ '_how_to' ]) )
update_post_meta( $post_id, '_how_to', wp_kses_post($_POST[ '_how_to' ]) );
if( isset($_POST[ '_part_of_cure_series' ]) )
update_post_meta( $post_id, '_part_of_cure_series', wp_kses_post($_POST[ '_part_of_cure_series' ]) );
}
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
// 1) Removing tabs
//unset( $tabs['description'] ); // Remove the description tab
//unset( $tabs['additional_information'] ); // Remove the additional information tab
//Attribute Indicated for
$tabs['intend_tab'] = array(
'title' => __( 'Intend For', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_indicated_for_tab_content'
);
// Adds the Caution tab
$tabs['caution_tab'] = array(
'title' => __( 'Caution', 'woocommerce' ),
'priority' => 60,
'callback' => 'woo_caution_tab_content'
);
// Adds the how to tab
$tabs['how_to'] = array(
'title' => __( 'How To', 'woocommerce' ),
'priority' => 70,
'callback' => 'woo_how_to_content'
);
// Adds the _part_of_cure_series
$tabs['part_of_cure_series'] = array(
'title' => __( 'Part of Cure Series', 'woocommerce' ),
'priority' => 80,
'callback' => 'woo_part_of_cure_series'
);
$tabs['reviews']['priority'] = 90;
return $tabs;
}
function woo_indicated_for_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_indicated_for' ) . '</p></div>';
}
function woo_caution_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_caution' ) . '</p></div>';
}
function woo_how_to_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_how_to' ) . '</p></div>';
}
function woo_part_of_cure_series() {
global $product;
echo'<div><p>'. $product->get_meta( '_part_of_cure_series' ) . '</p></div>';
}
2
Answers
Add the follows code snippets in your active theme’s functions.php to achieve the above –
I know this is very late answering but this is generic and can be useful for someone