WooCommerce手机号替代邮箱注册的实现方案咨询
Got it, let's break down how to completely replace email with phone numbers in your WooCommerce flows—since your customers don't use emails at all, we'll auto-generate the required email field and set the username to their phone number.
Step 1: Modify the Registration Form
First, we'll add a required phone field to the registration form and remove the default email field.
// Add phone number field to registration form add_action('woocommerce_register_form', 'add_phone_to_registration_form'); function add_phone_to_registration_form() { ?> <p class="form-row form-row-wide"> <label for="reg_phone"><?php _e('Phone Number', 'woocommerce'); ?> <span class="required">*</span></label> <input type="tel" class="input-text" name="reg_phone" id="reg_phone" value="<?php if (!empty($_POST['reg_phone'])) echo esc_attr($_POST['reg_phone']); ?>" required /> </p> <?php } // Remove default email field from registration form add_action('woocommerce_register_form_start', 'remove_email_from_registration'); function remove_email_from_registration() { remove_action('woocommerce_register_form', 'woocommerce_register_form_email'); }
Step 2: Validate Phone & Auto-Set Username/Email
Next, we'll ensure the phone number is required, then auto-generate the username (set to the phone number) and a dummy email in the format phone@yourdomain.com.
// Validate phone number is required for registration add_filter('woocommerce_registration_errors', 'validate_phone_registration', 10, 3); function validate_phone_registration($errors, $username, $email) { if (empty($_POST['reg_phone'])) { $errors->add('phone_error', __('Phone number is required', 'woocommerce')); } // Optional: Add check for duplicate phone numbers $existing_user = get_users(array( 'meta_key' => 'billing_phone', 'meta_value' => $_POST['reg_phone'], 'number' => 1 )); if (!empty($existing_user)) { $errors->add('phone_duplicate', __('This phone number is already registered', 'woocommerce')); } return $errors; } // Auto-set username and dummy email from phone number add_action('woocommerce_created_customer', 'auto_set_username_and_email_from_phone'); function auto_set_username_and_email_from_phone($customer_id) { if (isset($_POST['reg_phone'])) { $phone = sanitize_text_field($_POST['reg_phone']); // Update username to phone number wp_update_user(array( 'ID' => $customer_id, 'user_login' => $phone, 'user_nicename' => $phone )); // Create dummy email (replace @yourdomain.com with your actual domain) $dummy_email = $phone . '@yourdomain.com'; wp_update_user(array( 'ID' => $customer_id, 'user_email' => $dummy_email )); // Save phone number to user's billing/shipping meta update_user_meta($customer_id, 'billing_phone', $phone); update_user_meta($customer_id, 'shipping_phone', $phone); } }
Step 3: Adjust Checkout Flow
We need to remove the email field from checkout too, and ensure new customers created during checkout get their username/email auto-set from their phone.
// Remove email fields from checkout add_filter('woocommerce_checkout_fields', 'remove_email_from_checkout'); function remove_email_from_checkout($fields) { unset($fields['billing']['billing_email']); unset($fields['shipping']['shipping_email']); return $fields; } // Auto-set username and email for checkout customers add_action('woocommerce_checkout_create_customer', 'auto_set_checkout_user_details_from_phone', 10, 2); function auto_set_checkout_user_details_from_phone($customer, $data) { $phone = isset($data['billing_phone']) ? sanitize_text_field($data['billing_phone']) : ''; if (!empty($phone)) { $customer->set_username($phone); $dummy_email = $phone . '@yourdomain.com'; // Match the registration email suffix $customer->set_email($dummy_email); } } // Hide any leftover email-related hints in checkout add_filter('woocommerce_checkout_get_value', 'hide_email_hints_in_checkout', 10, 2); function hide_email_hints_in_checkout($value, $input) { if ($input === 'billing_email' || $input === 'shipping_email') { return ''; } return $value; }
Step 4: Allow Phone Number Login
Finally, let customers log in using their phone number instead of email/username:
// Enable phone number login add_filter('authenticate', 'allow_phone_login', 20, 3); function allow_phone_login($user, $username, $password) { if (empty($username) || empty($password)) return $user; // Check if input is a phone number, then find the associated user if (is_numeric($username)) { $matching_users = get_users(array( 'meta_key' => 'billing_phone', 'meta_value' => $username, 'number' => 1 )); if (!empty($matching_users)) { $user = wp_authenticate_username_password(null, $matching_users[0]->user_login, $password); } } return $user; }
Important Notes
- Replace
@yourdomain.comwith your actual domain (or any suffix you prefer) to keep the dummy emails unique. - Test the flow end-to-end: registration, checkout as a new user, and login with the phone number to ensure everything works.
- If you already have a phone field in your checkout, you can skip adding it again—just make sure it's set as required.
内容的提问来源于stack exchange,提问作者mohamad ali Hajipoor

