php – 以编程方式添加自定义设置选项卡以管理WooCommerce中的产品数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 以编程方式添加自定义设置选项卡以管理WooCommerce中的产品数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含8276字,纯文字阅读大概需要12分钟。
内容图文
![php – 以编程方式添加自定义设置选项卡以管理WooCommerce中的产品数据](/upload/InfoBanner/zyjiaocheng/810/5c9abf06c09b4e939c6363499310249d.jpg)
我想以编程方式将设置选项卡添加到产品数据元变量,如下所示:
“Verzendkosten”选项卡添加了firebug(意思是“运费”).
如何在woocommerce编辑产品页面设置中以编程方式添加“Verzendkosten”自定义选项卡?
(以及我如何能够用数据填充它?)
解决方法:
Updated on November 2017:
- Corrected some mistakes, cleaned and added available options
- Added ‘Usage’ and ‘naming conventions’ for custom fields slugs, at the end.
1)您在自定义帖子类型Metabox(此处为“产品”)中创建自定义选项卡,2)然后您可以添加字段以填充此选项卡,使用不同类型的字段(您将找到每种类型中的一个,因此这是一个非常完整的例子).
最后,您将找到一个在提交时保存数据的函数.
在这里,您可以直观地看到它(对于6种不同的自定义字段类型):
这是相关的代码:
// Step 1 - Adding a custom tab to the Products Metabox
add_filter( 'woocommerce_product_data_tabs', 'add_shipping_costs_product_data_tab', 99 , 1 );
function add_shipping_costs_product_data_tab( $product_data_tabs ) {
$product_data_tabs['shipping-costs'] = array(
'label' => __( 'Shipping costs', 'my_theme_domain' ), // translatable
'target' => 'shipping_costs_product_data', // translatable
);
return $product_data_tabs;
}
// Step 2 - Adding and POPULATING (with data) custom fields in custom tab for Product Metabox
add_action( 'woocommerce_product_data_panels', 'add_shipping_costs_product_data_fields' );
function add_shipping_costs_product_data_fields() {
global $post;
$post_id = $post->ID;
echo '<div id="shipping_costs_product_data" class="panel woocommerce_options_panel">';
## THE 6 DIFFERENT FIELD TYPES
# 1. Text imput field
woocommerce_wp_text_input( array(
'id' => '_imput_text',
// 'name' => '_imput_text', // (optional) for different ID attribute than name attribute
// 'class' => 'some-class', // (optional)
// 'wrapper_class' => 'show_if_simple', // (optional) example here for simple products type only
'placeholder' => __( 'Enter some data', 'theme_domain' ), // (optional)
'label' => __( 'Imput text Label', 'theme_domain' ), // (optional)
'description' => __( 'Imput text Description', 'theme_domain' ), // (optional)
'desc_tip' => true, // (optional) To show the description as a tip
// 'data_type' => '', // (optional formatting options) can be 'price', 'decimal', 'stock' or 'url'
// 'type' => '', // (optional additional custom attribute)
// 'value' => $value, // (optional) for a static value (can be conditionally set for $value variable)
) );
// 2. Textarea imput field
woocommerce_wp_textarea_input( array(
'id' => '_input_textarea',
// 'name' => 'input_textarea', // (optional) for different ID attribute than name attribute
'class' => 'widefat', // (optional)
// 'style' => '' // (optional)
// 'wrapper_class' => 'show_if_simple', // (optional) example here for simple products type only
'placeholder' => __( 'Enter some data', 'theme_domain' ), // (optional)
'label' => __( 'Imput textarea Label', 'theme_domain' ),
'description' => __( 'Imput textarea Description', 'theme_domain' ),
'desc_tip' => true, // (optional) To show the description as a tip
// 'rows' => 2, // (optional) defining number of rows
// 'cols' => 20, // (optional) defining number of columns
// 'value' => $value, // (optional) for a static value (can be conditionally set for $value variable)
) );
// 3. Checkbox field
woocommerce_wp_checkbox( array(
'id' => '_input_checkbox',
// 'name' => 'input_checkbox', // (optional) for different ID attribute than name attribute
// 'class' => 'some-class', // (optional)
// 'wrapper_class' => 'show_if_simple', // (optional) example here for simple products type only
'label' => __( 'Imput checkbox Label', 'theme_domain' ),
'description' => __( 'Imput checkbox Description', 'theme_domain' ),
'desc_tip' => true, // (optional) To show the description as a tip
// 'cbvalue' => 'yes', // to make it selected by default
// 'value' => $value, // (optional) for a static value (can be conditionally set for $value variable)
) );
// 4. Radio Buttons field
woocommerce_wp_radio( array(
'id' => '_imput_radio',
// 'name' => 'input_radio', // (optional) for different ID attribute than name attribute
// 'class' => 'some-class', // (optional)
// 'wrapper_class' => 'show_if_simple', // (optional) example here for simple products type only
'label' => __(' ', 'my_theme_domain'),
'description' => __( 'Imput Radio Description', 'my_theme_domain' ),
'desc_tip' => true,
'options' => array(
'option_value_1' => __('Displayed option 1'),
'option_value_2' => __('Displayed option 2'),
'option_value_3' => __('Displayed option 3'),
),
// 'value' => $value, // (optional) for a static value (can be conditionally set for $value variable)
) );
// 5. Select field
woocommerce_wp_select( array(
'id' => '_select_field',
// 'name' => '_select_field', // (optional) for different ID attribute than name attribute
// 'wrapper_class' => 'show_if_simple', // (optional) example here for simple products type only
'label' => __(' ', 'my_theme_domain'),
'description' => __( 'Imput Radio Description', 'my_theme_domain' ),
'desc_tip' => true,
'options' => array(
'' => __('Chose an option'), // Default empty value
'option_value_1' => __('Displayed option 1'),
'option_value_2' => __('Displayed option 2'),
'option_value_3' => __('Displayed option 3')
),
// 'value' => $value, // (optional) for a static value (can be conditionally set for $value variable)
) );
// 6. Hidden imput field
woocommerce_wp_hidden_input( array(
'id' => '_hidden_input',
// 'name' => '_hidden_input', // (optional) for different ID attribute than name attribute
'class' => 'some_class',
// 'value' => $value, // (optional) for a static value (can be conditionally set for $value variable)
) );
echo '</div>';
}
// Step 3 - Saving custom fields data of custom products tab metabox
add_action( 'woocommerce_process_product_meta', 'shipping_costs_process_product_meta_fields_save' );
function shipping_costs_process_product_meta_fields_save( $post_id ){
// save the text field data
if( isset( $_POST['_imput_text'] ) )
update_post_meta( $post_id, '_imput_text', esc_attr( $_POST['_imput_text'] ) );
// save the textarea field data
if( isset( $_POST['_imput_textarea'] ) )
update_post_meta( $post_id, '_imput_textarea', esc_attr( $_POST['_imput_textarea'] ) );
// save the checkbox field data
if( isset( $_POST['_imput_checkbox'] ) )
update_post_meta( $post_id, '_imput_checkbox', esc_attr( $_POST['_imput_checkbox'] ) );
// save the radio button field data
if( isset( $_POST['_input_radio'] ) )
update_post_meta( $post_id, '_input_radio', esc_attr( $_POST['_input_radio'] ) );
// save the selector field data
if( isset( $_POST['_select_field'] ) )
update_post_meta( $post_id, '_select_field', esc_attr( $_POST['_select_field'] ) );
// save the hidden imput data
if( isset( $_POST['_hidden_input'] ) )
update_post_meta( $post_id, '_hidden_input', esc_attr( $_POST['_hidden_input'] ) );
}
当然,这会在您的活动子主题(或主题)的function.php文件中或任何插件文件中.
You have to use the same custom field ID (slug names) in Step 2 and 3.
此代码经过测试且功能齐全
You can add custom options with ANY DATA, using custom code, custom variables, or any kind of functions in Step 2.
Usage
To get or retrieve the data you will use 07001 function for a defined Post ID:
06001
Where:
$post_id
is the current post ID (from product, order, coupon… post-types).custom_field_slug
is the ID (the slug) of your custom field.true
orfalse
: Whether to return a single value (data string or arrays)It’s the same process each kind of fields
Advice – Custom field slug names (Custom field ID)
If you don’t use an underscore character (
_slug_name
) at the beginning of the slug names of your custom fields, they will appear and be accessible to authorized users in the custom fields Metabox, after submitting the data (Update button).See this screen shot (here we get
input_text
custom field slug):07002
参考文献:
> Add tab which is visible only in admin side of product in woocommerce
> Package WooCommerce > Admin > Functions
> WooCommerce Multi Select for Single Product Field
> WP Code Reference – get_post_meta()
> WP Code Reference – update_post_meta()
内容总结
以上是互联网集市为您收集整理的php – 以编程方式添加自定义设置选项卡以管理WooCommerce中的产品数据全部内容,希望文章能够帮你解决php – 以编程方式添加自定义设置选项卡以管理WooCommerce中的产品数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。