Añadir pestaña y campos personalizados a datos de productos WC

Añadir pestaña y campos personalizados a datos de productos WC

Crear una pestaña a la ficha de productos de Woocommerce, para incluir campos personalizados del producto.

/**
* Crea la pestaña
*/
function custom_product_tabs( $tabs) {
$tabs["ebshipping"] = array(
"label" => __( "EB Shipping", "woocommerce" ),
"target" => "ebshipping_options",
);
return $tabs;
}
add_filter( "woocommerce_product_data_tabs", "custom_product_tabs" );




/**
* Campos de la pestaña
*/
function ebshipping_options_product_tab_content() {
global $post;

// Note the "id" attribute needs to match the "target" parameter set above
?><div id="ebshipping_options" class="panel woocommerce_options_panel"><?php
?><div class="options_group"><?php
woocommerce_wp_text_input( array(
"id" => "_ebshipping_peninsula",
"label" => __( "Península", "woocommerce" ),
"desc_tip" => "true",
"description" => __( "Costo de envío para península", "woocommerce" ),
) );
woocommerce_wp_text_input( array(
"id" => "_ebshipping_baleares",
"label" => __( "Baleares", "woocommerce" ),
"desc_tip" => "true",
"description" => __( "Costo de envío para baleares", "woocommerce" ),
) );
?></div>

</div><?php
}
add_filter( "woocommerce_product_data_panels", "ebshipping_options_product_tab_content" ); // WC 2.6 and up



/**
* Guarda los campos
*/
function save_ebshipping_option_fields( $post_id ) {

if ( isset( $_POST["_ebshipping_peninsula"] ) ) :
update_post_meta( $post_id, "_ebshipping_peninsula", floatval( $_POST["_ebshipping_peninsula"] ) );
endif;

if ( isset( $_POST["_ebshipping_baleares"] ) ) :
update_post_meta( $post_id, "_ebshipping_baleares", floatval( $_POST["_ebshipping_baleares"] ) );
endif;
}
add_action( "woocommerce_process_product_meta_simple", "save_ebshipping_option_fields" );
add_action( "woocommerce_process_product_meta_variable", "save_ebshipping_option_fields" );