我在产品数据选项面板上创建了一个自定义选项卡,但我不知道如何在上面写入内容。我还有一个代码可以使用钩子
add_action('woocommerce_product_options_sku','add_leadlovers_custom_fields' );
打印库存选项卡上的选项(复选框、文本等)
function adicionar_guia_leadlovers($tabs) {
$tabs['guia_leadlovers'] = array(
'label' => __( 'LeadLovers', 'text-domain' ),
'target' => 'leadlovers_product_data',
'class' => array( 'show_if_simple', 'show_if_variable' ),
);
return $tabs;
}
add_filter( 'woocommerce_product_data_tabs', 'adicionar_guia_leadlovers' );
但是,我可以使用什么钩子来替换“woocommerce_product_options_sku”并将选项写入我自己的自定义选项卡上?
这里是缺少的挂钩函数,用于显示附加产品设置选项卡“LeadLovers”的内容(并保存字段值):
add_filter( 'woocommerce_product_data_tabs', 'add_leadlovers_guide_product_tab' );
function add_leadlovers_guide_product_tab($tabs) {
$tabs['leadlovers_guide'] = array(
'label' => __( 'LeadLovers', 'text-domain' ),
'target' => 'leadlovers_product_data',
'class' => array( 'show_if_simple', 'show_if_variable' ),
);
return $tabs;
}
// Display the content
add_action( 'woocommerce_product_data_panels', 'display_readlovers_guide_product_data_tab_content' );
function display_readlovers_guide_product_data_tab_content() {
global $product_object;
echo '<div id="leadlovers_product_data" class="panel woocommerce_options_panel">
<div class="options_group">';
## ---- Content Start ---- ##
echo '<p>This is your LeadLovers content for the product "<strong>'.$product_object->get_name().'</strong>"…</p>';
woocommerce_wp_text_input( array(
'id' => '_leadlovers',
'value' => $product_object->get_meta('_leadlovers'),
'label' => __('LeadLovers field', 'woocommerce'),
'placeholder' => '',
'description' => __('LeadLovers description text.', 'woocommerce'),
));
## ---- Content End ---- ##
echo '</div></div>';
}
// Save field values
add_action( 'woocommerce_admin_process_product_object', 'save_leadlovers_guide_fields_values' );
function save_leadlovers_guide_fields_values( $product ) {
$leadlovers = isset( $_POST['_leadlovers'] ) ? sanitize_text_field($_POST['_leadlovers']) : '';
$product->update_meta_data( '_leadlovers', $leadlovers );
}
然后你会得到:
Tnx 获取您的代码,但是当我将您的代码添加到我的插件中时 使所有其他选项卡像照片一样消失
@LoicTheAztec
private function add_hooks(){
add_action( 'plugin_action_links_' . RTLFINALINCOME_PLUGIN_BASE, array( $this, 'add_plugin_action_link' ), 20 );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_backend_scripts_and_styles' ), 20 );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts_and_styles' ), 20 );
add_action( 'wp_ajax_nopriv_my_demo_ajax_call', array( $this, 'my_demo_ajax_call_callback' ), 20 );
add_action( 'wp_ajax_my_demo_ajax_call', array( $this, 'my_demo_ajax_call_callback' ), 20 );
add_action( 'admin_bar_menu', array( $this, 'add_admin_bar_menu_items' ), 100, 1 );
add_action( 'plugins_loaded', array( $this, 'add_wp_webhooks_integrations' ), 9 );
add_filter( 'woocommerce_product_data_tabs', 'add_leadlovers_guide_product_tab' );
add_action( 'woocommerce_admin_process_product_object', 'save_leadlovers_guide_fields_values' );
add_action( 'woocommerce_product_data_panels', 'display_readlovers_guide_product_data_tab_content' );
}
function add_leadlovers_guide_product_tab($tabs) {
$tabs['leadlovers_guide'] = array(
'label' => __( 'LeadLovers', 'text-domain' ),
'target' => 'leadlovers_product_data',
'class' => array( 'show_if_simple', 'show_if_variable' ),
);
return $tabs;}
function display_readlovers_guide_product_data_tab_content() {
global $product_object;
echo '<div id="leadlovers_product_data" class="panel woocommerce_options_panel">
<div class="options_group">';
## ---- Content Start ---- ##
echo '<p>This is your LeadLovers content for the product "<strong>'.$product_object->get_name().'</strong>"…</p>';
woocommerce_wp_text_input( array(
'id' => '_leadlovers',
'value' => $product_object->get_meta('_leadlovers'),
'label' => __('LeadLovers field', 'woocommerce'),
'placeholder' => '',
'description' => __('LeadLovers description text.', 'woocommerce'),
));
## ---- Content End ---- ##
echo '</div></div>';}
function save_leadlovers_guide_fields_values( $product ) {
$leadlovers = isset( $_POST['_leadlovers'] ) ? sanitize_text_field($_POST['_leadlovers']) : '';
$product->update_meta_data( '_leadlovers', $leadlovers );}