You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce注册:能否将注册表单值纳入管理员通知邮件?

当然可以!你完全能把WooCommerce注册表单里的字段值(不管是默认字段还是自定义字段)添加到管理员的注册通知邮件里,下面是具体的实现方案:

完整代码示例

先把你的现有代码和自定义字段的逻辑整合起来,直接用这段代码替换你现有的相关代码即可:

// 触发管理员收到新用户注册通知
add_action('woocommerce_created_customer', 'admin_email_on_registration', 10, 1);
function admin_email_on_registration( $customer_id ) {
    wp_new_user_notification( $customer_id );
}

// 自定义管理员通知邮件,插入注册表单字段
add_filter( 'wp_new_user_notification_email_admin', 'custom_wp_new_user_notification_email', 10, 3 );
function custom_wp_new_user_notification_email( $email, $user, $blogname ) {
    // 1. 获取注册表单里的字段值,替换成你实际需要的meta key
    // 示例:WooCommerce默认账单电话、自定义字段
    $user_phone = get_user_meta( $user->ID, 'billing_phone', true );
    $custom_field = get_user_meta( $user->ID, 'your_custom_field_key', true );

    // 2. 自定义邮件主题(可选,按需修改)
    $email['subject'] = sprintf( '[%s] 新用户注册提醒:%s', $blogname, $user->user_login );

    // 3. 自定义邮件内容,把字段值插进去
    // 这里用纯文本格式,也可以改成HTML格式(后面有示例)
    $email['message'] = sprintf(
        '嘿,%s有新用户注册啦!' . "\n\n" .
        '用户名:%s' . "\n" .
        '注册邮箱:%s' . "\n" .
        '联系电话:%s' . "\n" .
        '自定义信息:%s' . "\n\n" .
        '点击查看用户详情:%s',
        $blogname,
        $user->user_login,
        $user->user_email,
        !empty($user_phone) ? $user_phone : '用户未填写',
        !empty($custom_field) ? $custom_field : '无',
        get_edit_user_link( $user->ID )
    );

    return $email;
}
关键细节说明
  1. 获取字段值的核心:
    用get_user_meta()函数来读取用户注册时提交的字段值,第二个参数是字段对应的meta key——

    • 如果你用的是WooCommerce默认注册字段(比如姓名、地址、电话),meta key一般是billing_first_name、billing_phone这类带前缀的;
    • 如果是自己添加的自定义字段,需要确认你设置的meta key是什么(可以通过查看数据库wp_usermeta表,或者用用户元数据管理插件查看)。
  2. HTML格式邮件(可选):
    要是想让邮件排版更美观,可以改成HTML格式,只需要在函数里添加邮件头,并修改内容为HTML:

    // 添加HTML邮件头部
    $email['headers'][] = 'Content-Type: text/html; charset=UTF-8';
    
    // HTML格式的邮件内容
    $email['message'] = sprintf(
        '<h3>📢 %s新用户注册提醒</h3>' .
        '<div style="padding:10px; border:1px solid #eee; border-radius:4px;">' .
        '<p><strong>用户名:</strong>%s</p>' .
        '<p><strong>注册邮箱:</strong>%s</p>' .
        '<p><strong>联系电话:</strong>%s</p>' .
        '<p><strong>自定义信息:</strong>%s</p>' .
        '</div>' .
        '<p><a href="%s" style="color:#21759B;">点击查看用户详情</a></p>',
        $blogname,
        $user->user_login,
        $user->user_email,
        !empty($user_phone) ? $user_phone : '用户未填写',
        !empty($custom_field) ? $custom_field : '无',
        get_edit_user_link( $user->ID )
    );
    
  3. 容错处理:
    用!empty()判断字段是否有值,避免邮件里显示空白或者错误信息,提升邮件可读性。

内容的提问来源于stack exchange,提问作者Michelle Britton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:07:27