我一直在研究你的问题,并在谷歌搜索后找到了解决方案。
Note: 将下面提到的代码添加到主题functions.php
或任何插件文件。
Code:
此筛选函数将向Products Data
代谢箱<?php
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\' => __( \'My Custom Tab\', \'my_text_domain\' ),
\'target\' => \'my_custom_product_data\',
);
return $product_data_tabs;
}
此操作将向下添加的自定义选项卡添加自定义字段
Products Data
代谢箱
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_checkbox( array(
\'id\' => \'_my_custom_field\',
\'wrapper_class\' => \'show_if_simple\',
\'label\' => __( \'My Custom Field Label\', \'my_text_domain\' ),
\'description\' => __( \'My Custom Field Description\', \'my_text_domain\' ),
\'default\' => \'0\',
\'desc_tip\' => false,
) );
?>
</div>
<?php
}
?>
保存产品的自定义字段数据选项卡:
add_action( \'woocommerce_process_product_meta\', \'woocommerce_process_product_meta_fields_save\' );
function woocommerce_process_product_meta_fields_save( $post_id ){
// This is the case to save custom field data of checkbox. You have to do it as per your custom fields
$woo_checkbox = isset( $_POST[\'_my_custom_field\'] ) ? \'yes\' : \'no\';
update_post_meta( $post_id, \'_my_custom_field\', $woo_checkbox );
}
希望这有帮助!