Add a custom field in Woocommerce Edit Account page Add a custom field in Woocommerce Edit Account page wordpress wordpress

Add a custom field in Woocommerce Edit Account page


This can be done without overriding template files, just using available hooks this way:

// Add the custom field "favorite_color"add_action( 'woocommerce_edit_account_form', 'add_favorite_color_to_edit_account_form' );function add_favorite_color_to_edit_account_form() {    $user = wp_get_current_user();    ?>        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">        <label for="favorite_color"><?php _e( 'Favorite color', 'woocommerce' ); ?></label>        <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="favorite_color" id="favorite_color" value="<?php echo esc_attr( $user->favorite_color ); ?>" />    </p>    <?php}// Save the custom field 'favorite_color' add_action( 'woocommerce_save_account_details', 'save_favorite_color_account_details', 12, 1 );function save_favorite_color_account_details( $user_id ) {    // For Favorite color    if( isset( $_POST['favorite_color'] ) )        update_user_meta( $user_id, 'favorite_color', sanitize_text_field( $_POST['favorite_color'] ) );    // For Billing email (added related to your comment)    if( isset( $_POST['account_email'] ) )        update_user_meta( $user_id, 'billing_email', sanitize_text_field( $_POST['account_email'] ) );}

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

Tested and works.

If you want to override the template to display the custom field, you just have to keep the 2nd hooked function (the one who saves the data when edited).