Inserire tab personalizzato nel singolo prodotto in admin

Pubblicato da: Roberto Commenti: 0

Senza l’utilizzo di plugin, è possibile creare delle funzioni che permettono di creare tab personalizzati per il singolo prodotto, dove il suo contenuto puo essere inserito in admin della scheda prodotto.

Vi riporto l’esempio di 6 tab , dove se non viene inserito del contenuto non viene mostrato, ma mostrato solo se realmente si necessità di farlo vedere.

Ecco a voi lo snippet, dove potete modificarlo a vostro piacimento

 


// 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',
__( 'Tab Aggiuntivi', 'woocommerce' ),
'additional_product_tabs_metabox_content',
'product',
'normal',
'high'
);
}

// Add custom metabox content
function additional_product_tabs_metabox_content( $post )
{
// Tab number 1
echo '<h4>' . __( 'Tab Numero 1', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_tab_number_1', true );
wp_editor( $value, '_tab_number_1', array( 'editor_height' => 100 ) );

// Tab number 2
echo '<br><hr><h4>' . __( 'Tab Numero 2', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_tab_number_2', true );
wp_editor( $value, '_tab_number_2', array( 'editor_height' => 100 ) );

// Tab number 3
echo '<br><hr><h4>' . __( 'Tab Numero 3', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_tab_number_3', true );
wp_editor( $value, '_tab_number_3', array( 'editor_height' => 100 ) );

// Tab number 4
echo '<br><hr><h4>' . __( 'Tab Numero 4', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_tab_number_4', true );
wp_editor( $value, '_tab_number_4', array( 'editor_height' => 100 ) );

// Tab number 5
echo '<br><hr><h4>' . __( 'Tab Numero 5', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_tab_number_5', true );
wp_editor( $value, '_tab_number_5', array( 'editor_height' => 100 ) );

// Tab Number 6
echo '<br><hr><h4>' . __( 'Tab Numero 6', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_tab_number_6', true );
wp_editor( $value, '_tab_number_6', 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[ '_tab_number_1' ]) )
update_post_meta( $post_id, '_tab_number_1', wp_kses_post($_POST[ '_tab_number_1' ]) );

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

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

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

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

if( isset($_POST[ '_tab_number_6' ]) )
update_post_meta( $post_id, '_tab_number_6', wp_kses_post($_POST[ '_tab_number_6' ]) );
}

add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
global $product;

// 1) If you want to Removing native tabs

//unset( $tabs['description'] ); // Remove the description tab
//unset( $tabs['additional_information'] ); // Remove the additional information tab

// 2 Adding new tabs and set the right order

//Adds tab 1
if ( !empty( $product->get_meta( '_tab_number_1' ))){
$tabs['tab_number_1_tab'] = array(
'title' => __( 'Tab Numero 1', 'woocommerce' ),
'priority' => 10,
'callback' => 'woo_tab_number_1_tab_content'
);
}

// Adds tab 2
if ( !empty( $product->get_meta( '_tab_number_2' ))){
$tabs['tab_number_2_tab'] = array(
'title' => __( 'Tab Numero 2', 'woocommerce' ),
'priority' => 20,
'callback' => 'woo_tab_number_2_tab_content'
);
}
// Adds tab 3
if ( !empty( $product->get_meta( '_tab_number_3' ))){
$tabs['tab_number_3_tab'] = array(
'title' => __( 'Tab Numero 3', 'woocommerce' ),
'priority' => 30,
'callback' => 'woo_tab_number_3_tab_content'
);
}
// Adds tab 4
if ( !empty( $product->get_meta( '_tab_number_4' ))){
$tabs['tab_number_4_tab'] = array(
'title' => __( 'Tab Numero 4', 'woocommerce' ),
'priority' => 40,
'callback' => 'woo_tab_number_4_tab_content'
);
}
// Adds tab 5
if ( !empty( $product->get_meta( '_tab_number_5' ))){
$tabs['tab_number_5_tab'] = array(
'title' => __( 'Tab Numero 5', 'woocommerce' ),
'priority' => 60,
'callback' => 'woo_tab_number_5_tab_content'
);
}
// Adds tab 6
if ( !empty( $product->get_meta( '_tab_number_6' ))){
$tabs['tab_number_6_tab'] = array(
'title' => __( 'Tab Numero 6', 'woocommerce' ),
'priority' => 70,
'callback' => 'woo_tab_number_6_tab_content'
);
}
$tabs['reviews']['priority'] = 80;

return $tabs;
}

function woo_tab_number_1_tab_content() {
global $product;

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

function woo_tab_number_2_tab_content() {
global $product;

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

function woo_tab_number_3_tab_content() {
global $product;

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

function woo_tab_number_4_tab_content() {
global $product;

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

function woo_tab_number_5_tab_content() {
global $product;

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

function woo_tab_number_6_tab_content() {
global $product;

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

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *