I need update WordPress account email with woocommerce billing email after successful checkouts. I used this code but it does not work :
/* Update account email based on woocommerce billing email */ add_filter( 'woocommerce_thankyou' , 'custom_update_checkout_fields', 10, 2 ); function custom_update_checkout_fields($user_id, $old_user_data ) { $current_user = wp_get_current_user(); // Updating Billing info if($current_user->user_email != $current_user->billing_email) update_user_meta($user_id, 'billing_email', $current_user->user_email); }
Am I used an outdated code?
Advertisement
Answer
There are some mistakes. Try the following instead:
add_filter( 'woocommerce_thankyou' , 'thankyou_update_wordpress_user_email' ); function thankyou_update_wordpress_user_email( $order_id ) { $order = wc_get_order( $order_id ); $user = $order->get_user(); $billing_email = $order->get_billing_email(); // Updating user account email if( is_a($user, 'WP_User' ) && $user->user_email != $billing_email ) { $user_data = wp_update_user( array( 'ID' => $user->ID, 'user_email' => $billing_email ) ); } }
Code goes in functions.php file of the active child theme (or active theme). It should work.
Note: When changing user email, WordPress Send an email to the new user email.