Overview:
In a previous post – How to reconfigure and rearrange WooCommerce checkout fields β Part 1, I explained the process of creating custom fields that can be used on the WooCommerce checkout page. In this tutorial I’ll explain how you can associate the custom fields to the user meta. By default simply saving the order meta does not associate the custom woocommerce fields with the user meta.
The user meta is the information associated with the WordPress user or the customer which you see on their profile. The default WooCommerce billing fields such as address, mobile etc. are automatically saved with the user. So let’s go ahead and also save our custom fields such as title of the person, spouse information, some option buttons to receive emails and newsletters etc. with the user meta.
I am adding all my woocommerce customizations to a file woocommerce-customizations.php. See this post on how I make my customizations to woocommerce without bloating functions.php.
Saving the custom WooCommerce checkout fields with the WordPress user meta
I have used the ‘woocommerce_checkout_update_order_meta‘ action hook to add instructions to save the custom fields to the user meta.
function nds_save_custom_field_usermeta($order_id) { $order = new WC_Order($order_id); $user_id = $order->customer_user; //get the custom fields $title = get_post_meta( $order_id, 'title', true ); $title_spouse = get_post_meta( $order_id, 'title_spouse', true ); $spouse = get_post_meta( $order_id, 'spouse', true ); $receive_newsletter = (get_post_meta( $order_id, 'newsletter', true ) == 1) ? __('Yes') : __('No') ; $receive_emails = (get_post_meta( $order_id, 'receive_emails', true ) == 1) ? __('Yes') : __('No') ; //and so on //save them with the user if ( ! empty( $_POST['title'] ) ) { update_user_meta($user_id, 'title', $title); } if ( ! empty( $_POST['title_spouse'] ) ) { update_user_meta($user_id, 'title_spouse', $title_spouse); } if ( ! empty( $_POST['spouse'] ) ) { update_user_meta($user_id, 'spouse', $spouse); } update_user_meta($user_id, 'newsletter', $receive_newsletter); update_user_meta($user_id, 'receive_emails', $receive_emails); //and so on } add_action( 'woocommerce_checkout_update_order_meta', 'nds_save_custom_field_usermeta', 99 );
There is also another action hook ‘woocommerce_checkout_update_user_meta’ that you could probably use to achieve the same result. The WooCommerce hooks API guide is the best place to hunt for these.
In another post, I’ve explained how one can also allow modifications to the custom checkout fields from the user’s profile pages.
Marijn says
Hi! How would this work for custom fields from admin only? My custom fields from checkout Woocommerce are already saving to user_meta but when I enter custom field value in backend it is not saved to user… Thanks in advance π
Karan NA Gupta says
Hey,
You’ll need to hook into ‘personal_options_update’ and ‘edit_user_profile_update’ and add your fields to save. Here’s an example:
add_action( 'personal_options_update', 'custom_save_custom_profile_fields' );
add_action( 'edit_user_profile_update', 'custom_save_custom_profile_fields' );
function custom_save_custom_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
if (isset($_POST['field_name'])) {
update_usermeta( $user_id, 'field_name', wc_clean($_POST['field_name']) );
}
//repeat for other custom fields
}