extend woocommerce user fields edit account

Solutions on MaxInterview for extend woocommerce user fields edit account by the best coders in the world

showing results for - "extend woocommerce user fields edit account"
Domenico
05 Feb 2020
1// Add the custom field "favorite_color"
2add_action( 'woocommerce_edit_account_form', 'add_favorite_color_to_edit_account_form' );
3function add_favorite_color_to_edit_account_form() {
4    $user = wp_get_current_user();
5    ?>
6        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
7        <label for="favorite_color"><?php _e( 'Favorite color', 'woocommerce' ); ?></label>
8        <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 ); ?>" />
9    </p>
10    <?php
11}
12
13// Save the custom field 'favorite_color' 
14add_action( 'woocommerce_save_account_details', 'save_favorite_color_account_details', 12, 1 );
15function save_favorite_color_account_details( $user_id ) {
16    // For Favorite color
17    if( isset( $_POST['favorite_color'] ) )
18        update_user_meta( $user_id, 'favorite_color', sanitize_text_field( $_POST['favorite_color'] ) );
19
20    // For Billing email (added related to your comment)
21    if( isset( $_POST['account_email'] ) )
22        update_user_meta( $user_id, 'billing_email', sanitize_text_field( $_POST['account_email'] ) );
23}
24