phpwordpresswoocommercemetadatameta-boxes

Product metabox fields enabling additional tabs on WooCommerce single product pages


In continuation of this question: Custom metabox content displayed in single product additional tabs on Woocommerce

When I use the following code from this answer:

And by this method, when I want to not show empty tabs, I get an error. I use the following code:

// 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 )
{
    // Key Features
    echo '<h4>' . __( 'Key Features', 'woocommerce' ) . '</h4>';
    $value = get_post_meta( $post->ID, '_key_features', true );
    wp_editor( $value, '_key_features', array( 'editor_height' => 100 ) );


    // What News?
    echo '<br><hr><h4>' . __( 'What News?', 'woocommerce' ) . '</h4>';
    $value = get_post_meta( $post->ID, '_what_news', true );
    wp_editor( $value, '_what_news', array( 'editor_height' => 100 ) );


    // FAQ
    echo '<br><hr><h4>' . __( 'FAQ', 'woocommerce' ) . '</h4>';
    $value = get_post_meta( $post->ID, '_faq', true );
    wp_editor( $value, '_faq', 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[ '_key_features' ]) )
        update_post_meta( $post_id, '_key_features', wp_kses_post($_POST[ '_key_features' ]) );

    if( isset($_POST[ '_what_news' ]) )
        update_post_meta( $post_id, '_what_news', wp_kses_post($_POST[ '_what_news' ]) );
    
    if( isset($_POST[ '_faq' ]) )
        update_post_meta( $post_id, '_faq', wp_kses_post($_POST[ '_faq' ]) );

}

add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
    
    $values = get_field('key_features_tab');
    if ( ! empty($values) ) { 
        $tabs['key_features_tab'] = array(
            'title'     => __( 'Key Features', 'woocommerce' ),
            'priority'  => 10,
            'callback'  => 'woo_key_features_tab_content'
        );
    }
    
    $what_news_values = get_field('what_news_tab');
    if ( ! empty($what_news_values) ) {
        $tabs['what_news_tab'] = array(
            'title'     => __( 'What News?', 'woocommerce' ),
            'priority'  => 20,
            'callback'  => 'woo_what_news_tab_content'
        );
    }
    
    $faq_values = get_field('faq_tab');
    if ( ! empty($faq_values) ) {
        $tabs['faq_tab'] = array(
            'title'     => __( 'FAQ', 'woocommerce' ),
            'priority'  => 30,
            'callback'  => 'woo_faq_tab_content'
        );
    }
    
    $tabs['reviews']['priority'] = 40;

    return $tabs;
}


function woo_key_features_tab_content()  {
    global $product;

    echo'<div><p>'. $product->get_meta( '_key_features' ) . '</p></div>';
}

function woo_what_news_tab_content()  {
    global $product;

    echo'<div><p>'. $product->get_meta( '_what_news' ) . '</p></div>';
}

function woo_faq_tab_content()  {
    global $product;

    echo'<div><p>'. $product->get_meta( '_faq' ) . '</p></div>';
}

Where is the problem in this code?

Another question is that I know that additional info is displayed when the product has weight, height, etc. Is it possible to delete the additional info tab forever because my products are virtual and I don't have a physical product. Also, can I create a tab called additional info that has nothing to do with WooCommerce's additional info tab?


Solution

  • The problem is that you are using ACF get_field() function instead of the WooCommerce method get_meta() inside woo_custom_product_tabs() hooked function. Also, you are using the wrong meta keys.

    So change it for:

    add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
    function woo_custom_product_tabs( $tabs ) {
        global $product;
    
        $key_features = $product->get_meta('_key_features');
        if ( ! empty($key_features) ) { 
            $tabs['key_features_tab'] = array(
                'title'     => __( 'Key Features', 'woocommerce' ),
                'priority'  => 10,
                'callback'  => 'woo_key_features_tab_content'
            );
        }
        
        $what_news = $product->get_meta('_what_news');;
        if ( ! empty($what_news) ) {
            $tabs['what_news_tab'] = array(
                'title'     => __( 'What News?', 'woocommerce' ),
                'priority'  => 20,
                'callback'  => 'woo_what_news_tab_content'
            );
        }
        
        $faq_values = $product->get_meta('_faq');
        if ( ! empty($faq_values) ) {
            $tabs['faq_tab'] = array(
                'title'     => __( 'FAQ', 'woocommerce' ),
                'priority'  => 30,
                'callback'  => 'woo_faq_tab_content'
            );
        }
        
        $tabs['reviews']['priority'] = 40;
    
        return $tabs;
    }
    

    Now it should work as expected.