// Create tab
add_filter( \'woocommerce_product_data_tabs\', \'add_my_custom_product_data_tab\' , 99 , 1 );
function add_my_custom_product_data_tab( $product_data_tabs ) {
$product_data_tabs[\'my-custom-tab\'] = array
(
\'label\' => __( \'XColor\', \'my_text_domain\' ),
\'target\' => \'my_custom_product_data\',
);
return $product_data_tabs;
}
// Create field
add_action( \'woocommerce_product_data_panels\', \'add_my_custom_product_data_fields\' );
function add_my_custom_product_data_fields() {
global $woocommerce, $post;
?>
<!-- id below must match target registered in above add_my_custom_product_data_tab function -->
<div id="my_custom_product_data" class="panel woocommerce_options_panel">
<?php
woocommerce_wp_text_input(
array(
\'id\' => \'xcolor_html\',
\'label\' => __( \'HTML\', \'woocommerce\' )
));
?>
</div>
<?php
}
// Save field content - this don\'t work =(( white page
function woo_add_custom_general_fields_save( $post_id )
{
// Text Field
$woocommerce_text_field = $_POST[\'xcolor_html\'];
if (!empty($woocommerce_text_field))
update_post_meta($post_id, \'xcolor_html\', esc_attr($woocommerce_text_field));
}
// Show field content on product page
add_action( \'woocommerce_single_product_summary\', \'woocommerce_xcolor\', 15 );
function woocommerce_xcolor(){
echo get_post_meta( get_the_ID(), \'xcolor_html\', true );
}
最合适的回答,由SO网友:kirillrocks 整理而成
救命钩呢?woocommerce_process_product_meta
add_action( \'woocommerce_process_product_meta\', \'woo_add_custom_general_fields_save\' );