Php 将自定义字段添加到shipping选项卡中的Woocomece产品设置页面

Php 将自定义字段添加到shipping选项卡中的Woocomece产品设置页面,php,wordpress,woocommerce,product,shipping,Php,Wordpress,Woocommerce,Product,Shipping,是否可以在后端的WooCommerce产品页面发货选项卡设置中添加一些额外字段,因为我需要添加12个自定义字段 我试图找到一些相关的钩子,但没有成功。我发现的唯一方法是通过属性,但这不是一个方便的解决方案 如何将自定义字段添加到shipping选项卡中的Woocomece产品设置页面 谢谢。这是可能的,您会得到这个(我在这里设置了自定义文本字段): 代码如下: // Add custom fields to product shipping tab add_action( 'woocommer

是否可以在后端的WooCommerce产品页面发货选项卡设置中添加一些额外字段,因为我需要添加12个自定义字段

我试图找到一些相关的钩子,但没有成功。我发现的唯一方法是通过属性,但这不是一个方便的解决方案

如何将自定义字段添加到shipping选项卡中的Woocomece产品设置页面


谢谢。

这是可能的,您会得到这个(我在这里设置了自定义文本字段):

代码如下:

// 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 ) );
}
//将自定义字段添加到“产品装运”选项卡
添加操作('woocommerce\u product\u options\u shipping'、'add\u custom\u shipping\u option\u to\u products');
函数将\自定义\发货\选项\添加到\产品(){
全球$post$product;
回显“”;//新选项组
woocommerce_wp_text_输入(数组(
'id'=>'\u自定义\u文本\u字段1',
'label'=>\('My Text Field one','woocommerce'),
“占位符”=>“某物”,
'desc_tip'=>'true',
“description'=>”(在此处输入自定义值),“woocommerce”),
'value'=>get_post_meta($post->ID,'u custom_meta_field1',true),
) );
woocommerce_wp_text_输入(数组(
'id'=>'\u自定义\u文本\u字段2',
'label'=>\('My Text Field two','woocommerce'),
“占位符”=>“某物”,
'desc_tip'=>'true',
“description'=>”(在此处输入自定义值),“woocommerce”),
'value'=>get_post_meta($post->ID,'u custom_meta_field2',true),
) );
}
//将自定义字段值另存为元数据
添加操作(“woocommerce\u process\u product\u meta”、“save\u custom\u shipping\u option\u to\u products”);
功能保存\自定义\发货\选项\至\产品($post\ id){
$custom_text_field1=$_POST[''custom_text_field1'];
if(isset($custom_text_field1))
更新发布元数据($post\u id,''u custom\u meta\u field1',esc\u attr($custom\u text\u field1));
$custom_text_field2=$_POST[''custom_text_field2'];
if(isset($custom_text_field2))
更新发布元数据($post\u id,''u custom\u meta\u field2',esc\u attr($custom\u text\u field2));
}
代码位于活动子主题(或主题)的function.php文件或任何插件文件中


这段代码在WooCommerce 3+上进行了测试,可以正常工作

Hi,谢谢,它也可以在compare?Hi之类的插件中自动工作,那么如何才能将一个字段划分为更像已经存在的维度呢?是否可以编辑现有字段名称的文本?如何设置数字以十进制形式存储?如何在产品页面的“附加信息”选项卡中以正确的单位显示它们?您好,我知道这部分可以工作,谢谢,我已经实现了。好的,那么我将提出一个新问题,谢谢,但请告诉我如何修改一个类似于现有维度字段的字段?