将过滤后的计费电话保存到 WooCommerce 中的管理员用户配置文件 [英] Save filtered billing phone to admin user profile in WooCommerce

查看:34
本文介绍了将过滤后的计费电话保存到 WooCommerce 中的管理员用户配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Everything I have read over the last couple of days has indicated that if I want to save fields in the user-edit.php (User Admin Backend), I should be using the edit_user_profile_update & personal_options_update hooks (and I'm not including the validation hook in this question...)

在法典中,他们声明:

举个例子:

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

确保为 $_POST 数据键和实际用户元键提供不同的键名.如果您对两者使用相同的键,Wordpress 出于某种原因会清空该键下发布的值.
所以你总是会在 $_POST['custom_meta_key'] 中得到一个空值,所以在 html 输入元素的 name 属性中更改它并附加一个后缀.将其更改为 $_POST['custom_meta_key_data'] 它将正确传递数据.

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 中,然后通过 'billing_phone' 转换为 'billing_phone'代码>update_user_meta().

However, given the fact that I want to add validations to the existing billing_phone field, I do not know how to create said "custom_meta_key_data" (eg: '_billing_phone', or 'prefix_billing_phone') to then enter the value into said prefix_billing_phone to then convert to 'billing_phone' via update_user_meta().

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

I have provided my base code below, and I have searched the internet for two days as to a solution, but I cannot find a workaround.

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

In addition, the str_replace() doesn't action, which made me look inside the user-edit.php and the comments support the quoted notes above, where between hitting update, and the profile reloading, it saves each variable as _billing_ (prefixed '_') and records the original value (pre if statements / my code below) and saves that - not what I have conditioned/attempted to validate. I don't know how to copy that so that I can simply validate my fields...

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'?我的代码是:

Under personal_options_update and edit_user_profile_update the fields still do not update as per the conditionals. For example, if I enter '0411 111 111' (spaces), the spaces are not removed. I have attempted to do var_dump( $_POST ); die(); at every stage of the conditional below and the value ('billing_phone') passes the changes (eg: 0411111111), but at the update_user_meta() and when the page reloads it reverts back to the '0411 111 111'? The code I have is:

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' );

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

I read somewhere that you might have to pass the old variable and the new variable somehow and/or delete the user_meta_data and then add the new one but I have not tried that yet...?? Any further suggestions would be appreciated.

推荐答案

更新

现在要过滤只保留数字的电话号码字符串,使用 preg_replace() 比使用 str_replace() 更好.

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

在下面,帐单电话号码将在保存之前进行过滤:

In the following, The billing phone number will be filtered before saving in:

  • 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 文件中.经测试有效.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

相关:从字符串中提取数字

这篇关于将过滤后的计费电话保存到 WooCommerce 中的管理员用户配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆