Hey guys,
Here is the solution that I came up with and it has been working fine for me, hope it helps you as well.
I created a few new fields for the billing form using this plugin but let’s just use one as an example:
-Resale License # ( billing_resale_license_ )
Now, the plugin will add the field to the form for you, in my case, I end up with:
<input type="text" class="input-text " name="billing_resale_license_" id="billing_resale_license_" placeholder="" value="" display="text">
Now, we need to save the value of that field to the user_meta table, like so:
add_action( 'woocommerce_checkout_process', 'ws_billing_fields_save', 10, 1 );
function ws_billing_fields_save( $user_id ){
if ( isset( $_POST['billing_resale_license_'] ) ) {
update_user_meta($user_id, 'billing_resale_license_', $_POST['billing_resale_license_']);
}
}
Now that data is stored in user_meta, we need to hook in to the profile area to display it and allow it to be edited by the user or admins.
add_action( 'show_user_profile', 'ws_update_user_profile' );
add_action( 'edit_user_profile', 'ws_update_user_profile' );
function ws_update_user_profile( $user ){ ?>
<h3>Additional Fields</h3>
<table class="form-table">
<tr>
<th><label for="billing_resale_license_">Resale #</label></th>
<td><input type="text" name="billing_resale_license_" value="<?php echo esc_attr(get_the_author_meta( 'billing_resale_license_', $user->ID )); ?>" class="regular-text" /></td>
</tr>
</table>
<?php
}
add_action( 'personal_options_update', 'save_extra_fields' );
add_action( 'edit_user_profile_update', 'save_extra_fields' );
function save_extra_fields( $user_id ){
update_user_meta( $user_id,'billing_resale_license_', sanitize_text_field( $_POST['billing_resale_license_'] ) );
}
I chose to break my additional fields into their own table just above the default Woocommerce fields as I have many of them and they are more specific to the user rather than the order processing itself, so I keep them organized in their own table titled “Additional Fields”.