Saving the value of a custom field phone number in WooCommerce My account > Account details

流过昼夜 提交于 2019-12-03 21:33:25

You need to add a custom function hooked in woocommerce_save_account_details action hook:

add_action( 'woocommerce_save_account_details', 'my_account_saving_billing_phone', 10, 1 );
function my_account_saving_billing_phone( $user_id ) {
    $billing_phone = $_POST['billing_phone'];
    if( ! empty( $billing_phone ) )
        update_user_meta( $user_id, 'billing_phone', sanitize_text_field( $billing_phone ) );
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested on WooCommerce version 3+ and works. The data will be updated and displayed correctly.


Your complete billing phone field code in form-edit-account.php should be something like:

 <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
    <label for="billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?> <span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--phone input-text" name="billing_phone" id="billing_phone" value="<?php echo esc_attr( $user->billing_phone ); ?>" />
</p>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!