Hi WooCommerce Support Team,
Thank you for your response. I wanted to let you know that I have successfully resolved the issue with making the email field optional during checkout using custom code. Here’s a brief overview of how it works:
Custom PHP Code Implementation:
// Make email field optional add_filter( 'woocommerce_billing_fields', 'make_email_field_optional' ); function make_email_field_optional( $fields ) { $fields['billing_email']['required'] = false; return $fields; } // Identify and log in user by phone number or email add_action( 'woocommerce_before_checkout_process', 'identify_and_log_in_user_by_phone_number_or_email' ); function identify_and_log_in_user_by_phone_number_or_email() { $phone_number = $_POST['billing_phone']; $email = $_POST['billing_email']; // Check if phone number is provided if ( ! empty( $phone_number ) ) { $user_query = new WP_User_Query( array( 'meta_query' => array( array( 'key' => 'billing_phone', 'value' => $phone_number, 'compare' => '=' ) ) ) ); $users = $user_query->get_results(); if ( $users ) { $user = $users[0]; wp_set_current_user( $user->ID ); wp_set_auth_cookie( $user->ID ); do_action( 'wp_login', $user->user_login, $user ); } else { // Create a new user if phone number is not found $user_data = array( 'user_login' => $phone_number, 'role' => 'customer' ); if ( ! empty( $email ) ) { $user_data['user_email'] = $email; } $user_id = wp_insert_user( $user_data ); if ( $user_id ) { update_user_meta( $user_id, 'billing_phone', $phone_number ); if ( ! empty( $email ) ) { update_user_meta( $user_id, 'billing_email', $email ); } wp_set_current_user( $user_id ); wp_set_auth_cookie( $user_id ); do_action( 'wp_login', $user_data['user_login'], $user ); } } } // Check if email is provided if ( ! empty( $email ) ) { $user_query = new WP_User_Query( array( 'meta_query' => array( array( 'key' => 'user_email', 'value' => $email, 'compare' => '=' ) ) ) ); $users = $user_query->get_results(); if ( $users ) { $user = $users[0]; wp_set_current_user( $user->ID ); wp_set_auth_cookie( $user->ID ); do_action( 'wp_login', $user->user_login, $user ); } } // Display error messages if phone number and email are empty if ( empty( $phone_number ) && empty( $email ) ) { wc_add_notice( __( 'Please enter your phone number or email address.', 'text-domain' ), 'error' ); } }
Advantages of Custom Code:
- Consistent User Identification: Unlike using checkout field editor plugins which might treat every customer as a new customer (due to the lack of record consistency), this code ensures that returning customers are identified properly, whether they use a phone number or email.
- Improved User Experience: The system logs in returning users automatically based on their phone number or email, creating a seamless checkout experience.
- Flexibility: Users can check out with either a phone number or email, providing a more flexible approach.
For anyone needing similar functionality, they can use this custom code. If there are any issues or additional support is needed, please feel free to contact me through my website: Huzaifa Qureshi.
Thank you!
Best regards,
Huzaifa Qureshi