亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將過濾后的計費電話保存到 WooCommerce 中的管理員用戶配置文件

將過濾后的計費電話保存到 WooCommerce 中的管理員用戶配置文件

PHP
呼喚遠方 2023-07-21 15:50:41
我在過去幾天讀到的所有內容都表明,如果我想在 user-edit.php (用戶管理后端)中保存字段,我應該使用 & 鉤子(并且我在這個問題中不包括驗證鉤子edit_user_profile_update...... personal_options_update)在法典中,他們指出:考慮一個例子: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'],它將正確傳遞數據。但是,考慮到我想向現有billing_phone字段添加驗證,我不知道如何創建所述“ custom_meta_key_data”(例如:'_billing_phone'、 或'prefix_billing_phone'),然后將值輸入到所述 prefix_billing_phone 中,然后轉換為'billing_phone'via update_user_meta()。我在下面提供了我的基本代碼,我已經在互聯網上搜索了兩天的解決方案,但我找不到解決方法。此外,str_replace()不執行操作,這讓我查看內部,user-edit.php并且注釋支持上面引用的注釋,在點擊更新和配置文件重新加載之間,它將每個變量保存為_billing_(前綴“_”)并記錄原始值(前面的 if 語句/下面的代碼)并保存該值 - 不是我條件/嘗試驗證的內容。我不知道如何復制它,以便我可以簡單地驗證我的字段......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 );                        }                }        }}
查看完整描述

1 回答

?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

更新

現在要過濾僅保留數字的電話號碼字符串,您可以更好地preg_replace()使用str_replace().

下面,帳單電話號碼在保存之前將被過濾:

  • 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 文件中。經過測試并有效。


查看完整回答
反對 回復 2023-07-21
  • 1 回答
  • 0 關注
  • 146 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號