Php 将自定义字段另存为用户元数据

Php 将自定义字段另存为用户元数据,php,wordpress,woocommerce,checkout,usermetadata,Php,Wordpress,Woocommerce,Checkout,Usermetadata,我想将我的自定义注册字段添加到我的签出页表单中 我正在使用此代码将自定义字段添加到我的注册区域 顺便说一句,我正在使用我的签出字段到我的注册字段中 add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' ); function custom_override_checkout_fields( $fields ) { $fields['billing']['shipping_tc'] =

我想将我的自定义注册字段添加到我的签出页表单中

我正在使用此代码将自定义字段添加到我的注册区域

顺便说一句,我正在使用我的签出字段到我的注册字段中

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {

    $fields['billing']['shipping_tc'] = array(
        'label' => __('TC Kimlik No', 'woocommerce'),
        'placeholder' => _x('Fatura İçin Gerekli', 'placeholder', 'woocommerce'),
        'required' => true,
        'class' => array('form-row-wide'),
        'clear' => true
    );
    
    return $fields;
}
我尝试了这段代码来更新用户meta

add_action( 'woocommerce_checkout_update_user_meta', 'reigel_woocommerce_checkout_update_user_meta', 10, 2 );
function reigel_woocommerce_checkout_update_user_meta( $customer_id, $posted ) {
    if (isset($posted['shipping_tc'])) {
        $dob = sanitize_text_field( $posted['shipping_tc'] );
        update_user_meta( $user_id, $dob, $_POST[$dob]);
    }
}
没有错误,但它不工作。。。有人能帮我吗

我正在使用此代码的帮助成功地更新其他默认签出值

// Custom function to save Usermeta or Billing Address of registered user
add_action('woocommerce_created_customer','zk_save_billing_address');
function zk_save_billing_address($user_id){
    $address = $_POST;
    foreach ($address as $key => $field){
        // Only billing fields values
        if( strpos( $key, 'billing_' ) !== false ){
            // Condition to add firstname and last name to user meta table
            if($key == 'billing_first_name' || $key == 'billing_last_name'){
                $new_key = str_replace( 'billing_', '', $key );
                update_user_meta( $user_id, $new_key, $_POST[$key] );
            }
            update_user_meta( $user_id, $key, $_POST[$key] );
        }
    }
}
如何通过注册更新自定义签出字段


这里是。

主要错误是使用了一个签出字段,该字段的键在“账单”部分以
shipping\uuu
开头

此外,您最好使用钩子组合钩子
woocommerce\u billing\u fields
,它将为您做所有事情(因此无需像woocommerce那样将字段保存为用户元数据或订单项元数据)

因此,唯一需要的代码替换将是(使用字段键
billing\u identifier
,而不是混淆
shipping\u tc
):

代码进入活动子主题(或活动主题)的functions.php文件。测试和工作


该字段将另外出现在“我的帐户>地址>编辑帐单地址”中。

非常感谢!!它是有效的…@enolÜstün我也在这里回答了你的第一个问题:
add_filter( 'woocommerce_billing_fields' , 'add_custom_billing_field' );
function add_custom_billing_field( $fields ) {
    $fields['billing_identifier'] = array(
        'label' => __('TC Kimlik No', 'woocommerce'),
        'placeholder' => _x('Fatura İçin Gerekli', 'placeholder', 'woocommerce'),
        'required' => true,
        'class' => array('form-row-wide'),
        'clear' => true
    );

    return $fields;
}