将过滤后的账单 phone 保存到 WooCommerce 中的管理员用户配置文件

Save filtered billing phone to admin user profile in WooCommerce

过去几天我读到的所有内容都表明,如果我想在用户 edit.php(用户管理后端)中保存字段,我应该使用 edit_user_profile_update & personal_options_update 钩子(我不包括这个问题中的验证钩子...)

在抄本中,他们声明:

Consider an example:

update_user_meta($user_id, 'custom_meta_key', $_POST['custom_meta_key']);

Make doubly sure that you give a different key name for the $_POST data key and the actual user meta key. If you use the same key for both, Wordpress for some reason, empties the value posted under that key.
So you'll always get an empty value in $_POST['custom_meta_key'] so change it in the html input element's name attribute and append a suffix. Changing it to $_POST['custom_meta_key_data'] and it'll pass the data properly.

但是,鉴于我想向现有 billing_phone 字段添加验证,我不知道如何创建所说的“custom_meta_key_data”(例如:'_billing_phone',或 'prefix_billing_phone') 然后将值输入所述 prefix_billing_phone 然后通过 update_user_meta().

转换为 'billing_phone'

我在下面提供了我的基本代码,我已经在互联网上搜索了两天的解决方案,但我找不到解决方法。

此外,str_replace() 没有动作,这让我查看了 user-edit.php 并且评论支持上面引用的注释,在点击更新和重新加载配置文件之间,它将每个变量保存为 _billing_(前缀为“_”)并记录原始值(pre if 语句/下面我的代码)并保存 - 而不是我要验证的 conditioned/attempted。我不知道如何复制它以便我可以简单地验证我的字段...

add_action( 'personal_options_update', 'audp_save_user_account_fields' );
add_action( 'edit_user_profile_update', 'audp_save_user_account_fields' ); 

function audp_save_user_account_fields( $user_id ) {
    
    /* Input Value: "0412 345 678"
     * SHOULD Become: "0412345678"
     */
    $billing_phone = str_replace( array( ' ', '(', ')' ), '', $_POST['billing_phone'] );
    
    if ( !empty( $_POST['billing_phone'] ) && preg_match( '/^04[0-9]{8}$/D', $billing_phone ) ) {
        
        $billing_phone_query = get_users( array( 
            'meta_key' => 'billing_phone',
            'meta_value' => $billing_phone, 
        ) );
        
        foreach ( $billing_phone_query as $query ) {
            
            if ( $user_id == $query->ID ) {
                
                /* This value ($billing_phone) should be eg: "0412345678"
                 * but remains "0412 345 678" 
                 */
                update_user_meta( $user_id, 'billing_phone', $billing_phone );
            
            }
        
        }
    
    }

}

添加/编辑

personal_options_updateedit_user_profile_update 下,字段仍未根据条件更新。例如,如果我输入“0411 111 111”(空格),则不会删除空格。我试图在下面条件的每个阶段执行 var_dump( $_POST ); die(); 并且值 ('billing_phone') 通过更改(例如:0411111111),但是在 update_user_meta() 和页面重新加载时恢复到“0411 111 111”?我的代码是:

function audp_save_user_account_fields( $user_id ) {
    
    $billing_phone_key = 'billing_phone';
    
    if ( isset( $_POST[$billing_phone_key] ) ) {
        
        // var_dump( $_POST['billing_phone_key] ) = '0411 111 111'
        $billing_phone = preg_replace( '/[^0-9]/i', '', sanitize_text_field($_POST[$billing_phone_key] ) );
        // var_dump on $billing_phone = '0411111111'
        
        if ( !empty( $_POST[$billing_phone_key] ) ) {
            
            // var_dump( $billing_phone ) = '0411111111'
            update_user_meta( $user_id, 'billing_phone', $billing_phone );
            // page reloads with value '0411 111 111'.
        }
        
    }

}
add_action( 'edit_user_profile_update', 'audp_save_user_account_fields' );

我在某处读到,您可能必须以某种方式传递旧变量和新变量 and/or 删除 user_meta_data 然后添加新变量,但我还没有尝试过... ??如有任何进一步的建议,我们将不胜感激。

已更新

Now to filter a phone number string keeping only numbers, you can better use preg_replace() than str_replace().

在下文中,帐单 phone 号码将在保存之前被过滤:

  • Wordpress 管理员用户仪表板
  • 我的账户地址编辑账单地址
  • 下订单后(保存数据之前)

代码:

// In Wordpress user profile (increased hook priority)
add_action( 'personal_options_update', 'save_user_billing_phone_fields', 999999 );
add_action( 'edit_user_profile_update', 'save_user_billing_phone_fields', 999999 );
function save_user_billing_phone_fields( $user_id ) {
    $field_key = 'billing_phone';

    if( isset($_POST[$field_key]) && ! empty($_POST[$field_key]) ) {
        // Filtering billing phone (removing everything else than numbers)
        $billing_phone = preg_replace( '/[^0-9]/', '', sanitize_text_field($_POST[$field_key]) );

        // Update the billing phone
        update_user_meta( $user_id, 'billing_phone', $billing_phone );
    }
}

// On My account > edit addresses > Billing
add_action( 'woocommerce_process_myaccount_field_billing_phone', 'filter_my_account_billing_phone_fields' );
function filter_my_account_billing_phone_fields( $value ) {
    return preg_replace( '/[^0-9]/', '', $value );
}

// On order submission
add_action( 'woocommerce_checkout_posted_data', 'wc_checkout_posted_data_filter_callback' );
function  wc_checkout_posted_data_filter_callback( $data ) {
    $field_key = 'billing_phone';

    if( isset($data[$field_key]) ) {
        // Filtering billing phone (removing everything else than numbers)
        $data[$field_key] = preg_replace( '/[^0-9]/', '', $data[$field_key] );
    }
    return $data;
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。

相关:Extract numbers from a string