Skip to content
Advertisement

WordPress – add woocommerce billing_phone field into user-new.php and save it into database and my account

I’m facing a problem. I need to create some woocommerce customer role accounts manualy from the wp-admin. What I need to do is to add a woocommerce billing_phone number into the form which will be saved into the database and added to woocommerce my account phone field automatically. I’ve tried to do it with the code bellow but it did not help. Any ideas how could I make it?

I need to add it to user-new.php …I know there is possibility to add the billing_phone after I create the user account, but I want to save time by searching for the account and doing things twice so I want to save time by adding this field directly into the account creation process from wp-admin.

Thank you in advance!

Code I used but did not help:

add_action( 'user_new_form', function( $type ){
    if( 'add-new-user' !== $type )
        return;
    echo '<input type="text" name="billing_phone" id="billing_phone" value=""/>';
});

enter image description here

Advertisement

Answer

This is actually very easy to do. So the way WooCommerce autofills those fields is if by fetching the actually billing and shipping addresses for the user. If we manage so save the users contact number on regster hook we can make this work.

We will first create a field to save mobile data.

add_action( 'user_new_form', 'bks_add_mobile_field' );
add_action( 'edit_user_profile', 'bks_add_mobile_field' );
add_action( 'show_user_profile', 'bks_add_mobile_field' );

function bks_add_mobile_field( $user ) { ?>
    <h3><?php _e("Extra profile information", "blank"); ?></h3>

    <table class="form-table">
        <tr>
            <th><label for="mobile"><?php _e("Mobile Number"); ?></label></th>
            <td>
                <input type="text" name="mobile" id="mobile" value="<?php echo esc_attr( get_the_author_meta( 'mobile', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description"><?php _e("Please enter mobile."); ?></span>
            </td>
        </tr>
    </table>
<?php }

Now we would save this mobile in both the fields mobile and billing_mobile.

add_action( 'user_register', 'bks_save_mobile_field' );

function bks_save_mobile_field($user_id) {
    // You can maybe add checks here whch would determine if the users role is customer 
    // or not or maybe validate the number. 
    if ( isset( $_POST['mobile'] ) ) {
        update_user_meta($user_id, 'mobile', $_POST['mobile']);
        update_user_meta($user_id, 'billing_phone', $_POST['mobile']);
    }
}

So to put it all together add the following code in your functions.php file.

add_action( 'user_new_form', 'bks_add_mobile_field' );
add_action( 'show_user_profile', 'bks_add_mobile_field' );
add_action( 'edit_user_profile', 'bks_add_mobile_field' );

function bks_add_mobile_field( $user ) { ?>
    <h3><?php _e("Extra profile information", "blank"); ?></h3>

    <table class="form-table">
        <tr>
            <th><label for="mobile"><?php _e("Mobile Number"); ?></label></th>
            <td>
                <input type="text" name="mobile" id="mobile" value="<?php echo esc_attr( get_the_author_meta( 'mobile', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description"><?php _e("Please enter mobile."); ?></span>
            </td>
        </tr>
    </table>
<?php }


add_action( 'user_register', 'bks_save_mobile_field' );

function bks_save_mobile_field($user_id) {
    // You can maybe add checks here whch would determine if the users role is customer 
    // or not or maybe validate the number. 
    if ( isset( $_POST['mobile'] ) ) {
        update_user_meta($user_id, 'mobile', $_POST['mobile']);
        update_user_meta($user_id, 'billing_phone', $_POST['mobile']);
    }
}

This code is tested and it WORKS.

Order Screenshot

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement