Skip to content
Advertisement

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

Trying to add a field for Woocommerce billing_phone to the my-account/edit-account/. It is present within the update address pages but when adding to the form-edit-account.php it does not update.

Currently adding by following the same code as the other fields, such as first_name:

<input type="text" class="form-control woocommerce-Input woocommerce-Input--phone input-text" name="billing_phone" id="billing_phone" value="<?php echo esc_attr( $user->billing_phone ); ?>" /> 

Image of form with billing_phone being shown as a value:

enter image description here

But upon updating with new number it is not changed

How can I make this being saved/updated?

Advertisement

Answer

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:

<?php function field_phone(){
$user = wp_get_current_user(); 
 ?>
 <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> <?php } ?>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement