将自定义字段添加到发货选项卡中的 WooComerce 产品设置页面 [英] Add custom fields to WooComerce product setting pages in the shipping tab
问题描述
是否可以在 WooCommerce 产品页面中添加一些额外的字段在后端的运输标签设置中,因为我需要添加类似 12 个自定义字段的内容.
Is it possible to do add some extra fields in WooCommerce products pages shipping tab settings in backend, as I need to add something like 12 custom fields.
我试图找到一些相关的钩子,但没有成功.我发现的唯一方法是在属性上,但这不是一个方便的解决方案......
I have tried to find some related hooks without success. The only way that I have found was over attributes, but it was not a convenient solution…
如何将自定义字段添加到发货选项卡中的 WooComerce 产品设置页面?
How can I add custom fields to WooComerce product setting pages in the shipping tab?
谢谢.
推荐答案
这是可能的,你会得到这个(这里我设置了自定义文本字段):
This is possible and you will get this (here I have set to custom text fields):
代码如下:
// Add custom fields to product shipping tab
add_action( 'woocommerce_product_options_shipping', 'add_custom_shipping_option_to_products');
function add_custom_shipping_option_to_products(){
global $post, $product;
echo '</div><div class="options_group">'; // New option group
woocommerce_wp_text_input( array(
'id' => '_custom_text_field1',
'label' => __( 'My Text Field one', 'woocommerce' ),
'placeholder' => 'something',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $post->ID, '_custom_meta_field1', true ),
) );
woocommerce_wp_text_input( array(
'id' => '_custom_text_field2',
'label' => __( 'My Text Field two', 'woocommerce' ),
'placeholder' => 'something',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $post->ID, '_custom_meta_field2', true ),
) );
}
// Save the custom fields values as meta data
add_action( 'woocommerce_process_product_meta', 'save_custom_shipping_option_to_products' );
function save_custom_shipping_option_to_products( $post_id ){
$custom_text_field1 = $_POST['_custom_text_field1'];
if( isset( $custom_text_field1 ) )
update_post_meta( $post_id, '_custom_meta_field1', esc_attr( $custom_text_field1 ) );
$custom_text_field2 = $_POST['_custom_text_field2'];
if( isset( $custom_text_field2 ) )
update_post_meta( $post_id, '_custom_meta_field2', esc_attr( $custom_text_field2 ) );
}
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
此代码已在 WooCommerce 3+ 上测试并有效
This code is tested on WooCommerce 3+ and works
这篇关于将自定义字段添加到发货选项卡中的 WooComerce 产品设置页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!