英文:
Add content to a custom tab in WooCommerce admin single product data options settings
问题
我创建了一个自定义选项卡在产品数据选项面板上,但我不知道如何在上面编写内容。我有一个代码可以打印出在库存选项卡上的选项(复选框、文本等),它使用了钩子 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'
并在我的自定义选项卡上编写选项?
英文:
I created a custom tab on the product data options panel, but i don't know how to write the content on it. I have yet a code that prints the options (checkboxes, texts etc) that was on the inventory tab using the hook add_action('woocommerce_product_options_sku','add_leadlovers_custom_fields' );
I used this code to generates this tab.
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' );
But, what hook can I use to replace 'woocommerce_product_options_sku' and wrtite the options on my own custom tab?
答案1
得分: 1
这是缺失的挂钩函数,用于显示内容(并保存字段值)以供您的额外产品设置选项卡“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;
}
// 显示内容
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">';
## ---- 内容开始 ---- ##
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'),
));
## ---- 内容结束 ---- ##
echo '</div></div>';
}
// 保存字段值
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 );
}
然后您会得到:
英文:
Here are the missing hooked function, to display the content (and save field values) for your additional product settings tab "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 );
}
Then you will get:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论