如何在WooCommerce中自动给下单注册访客发送重置密码邮件
Auto-Send Password Reset Email to Auto-Registered WooCommerce Guests
Got it, let's fix this up. Your existing code already handles auto-registering guest users after their first order, syncing their billing/shipping details, and linking past orders to their new account. The only missing piece is triggering a password reset email so users can set their own secure password instead of getting a random one they'll probably ignore.
Here's the modified code with the password reset functionality added— I've commented the new lines to make it clear what's changed:
function wc_register_guests($order_id) { // 获取所有订单数据 $order = new WC_Order($order_id); // 从订单中获取用户邮箱 $order_email = $order->billing_email; // 检查是否存在使用该账单邮箱的用户 $email_exists = email_exists($order_email); $user_exists = username_exists($order_email); // 若不存在则为访客创建账号 if (!$user_exists && !$email_exists) { $random_password = wp_generate_password(); // 以邮箱为用户名、随机密码创建新用户 $user_id = wp_create_user($order_email, $random_password, $order_email); // 标记为WooCommerce访客客户 update_user_meta($user_id, 'guest', 'yes'); // 更新用户账单信息 update_user_meta($user_id, 'billing_address_1', $order->billing_address_1); update_user_meta($user_id, 'billing_address_2', $order->billing_address_2); update_user_meta($user_id, 'billing_city', $order->billing_city); update_user_meta($user_id, 'billing_company', $order->billing_company); update_user_meta($user_id, 'billing_country', $order->billing_country); update_user_meta($user_id, 'billing_email', $order->billing_email); update_user_meta($user_id, 'billing_first_name', $order->billing_first_name); update_user_meta($user_id, 'billing_last_name', $order->billing_last_name); update_user_meta($user_id, 'billing_phone', $order->billing_phone); update_user_meta($user_id, 'billing_postcode', $order->billing_postcode); update_user_meta($user_id, 'billing_state', $order->billing_state); // 更新用户配送信息 update_user_meta($user_id, 'shipping_address_1', $order->shipping_address_1); update_user_meta($user_id, 'shipping_address_2', $order->shipping_address_2); update_user_meta($user_id, 'shipping_city', $order->shipping_city); update_user_meta($user_id, 'shipping_company', $order->shipping_company); update_user_meta($user_id, 'shipping_country', $order->shipping_country); update_user_meta($user_id, 'shipping_first_name', $order->shipping_first_name); update_user_meta($user_id, 'shipping_last_name', $order->shipping_last_name); update_user_meta($user_id, 'shipping_method', $order->shipping_method); update_user_meta($user_id, 'shipping_postcode', $order->shipping_postcode); update_user_meta($user_id, 'shipping_state', $order->shipping_state); // 将历史订单关联到新用户账号 wc_update_new_customer_past_orders($user_id); // --- 新增代码:发送重置密码邮件 --- // 获取用户对象 $user_obj = get_user_by('id', $user_id); // 发送WordPress默认的重置密码邮件 if ($user_obj) { wp_send_password_reset_email($user_obj); } } } add_action('woocommerce_thankyou', 'wc_register_guests', 10, 1);
Quick Notes:
- Why
wp_send_password_reset_email? This sends the standard WordPress password reset email that users are already familiar with. It lets them set their own password instead of forcing them to use a random auto-generated one, which improves user experience. - Error safety: We added a quick check to confirm the user object exists before sending the email, just to avoid any potential runtime errors.
- Legacy compatibility: If you're using WordPress 5.2 or older (not recommended, but just in case), replace the new code block with this legacy method:
// 适配WordPress 5.2及更早版本的写法 $reset_key = get_password_reset_key($user_obj); wp_password_change_notification($user_obj, $reset_key);
Where to Add This Code:
- Stick it in your child theme's
functions.phpfile - Or use a custom code plugin (better for maintainability, so you don't lose changes when updating your theme)
内容的提问来源于stack exchange,提问作者Elichy
相关产品推荐
相关产品推荐

