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

如何在WooCommerce自定义订单状态变更邮件中添加抄送邮箱?

解决WooCommerce自定义订单状态邮件添加抄送的问题

一、先排查自定义状态对应的邮件ID

首先要确认你的自定义订单状态关联的邮件ID,可通过以下代码输出所有已注册的WooCommerce邮件信息:

add_action( 'woocommerce_email', 'list_all_woocommerce_email_ids' );
function list_all_woocommerce_email_ids( $email_class ) {
    foreach ( $email_class->emails as $email ) {
        echo '<p>邮件ID:' . $email->id . ' | 对应状态:' . $email->title . '</p>';
    }
}

将代码添加到主题functions.php或自定义插件中,访问前端任意页面,就能找到你自定义状态对应的邮件ID。

二、直接为目标邮件添加抄送

拿到正确的邮件ID后,使用woocommerce_email_headers钩子添加抄送,无需新建邮件ID:

add_filter( 'woocommerce_email_headers', 'add_cc_to_target_order_email', 10, 3 );
function add_cc_to_target_order_email( $headers, $email_id, $order ) {
    // 替换为你查到的自定义状态邮件ID
    $target_id = 'customer_custom_status';
    $cc_address = 'your-cc-email@example.com';

    if ( $email_id === $target_id ) {
        $headers .= "Cc: " . $cc_address . "\r\n";
    }

    return $headers;
}

把$target_id换成实际邮件ID,$cc_address改为需要抄送的邮箱即可。

三、若自定义状态无默认关联邮件

如果你的自定义状态没有对应的默认邮件,需先注册自定义邮件类,再添加抄送:

// 注册自定义邮件类
add_action( 'woocommerce_email_classes', 'register_custom_order_status_email' );
function register_custom_order_status_email( $email_classes ) {
    class WC_Custom_Status_Email extends WC_Email {
        public function __construct() {
            $this->id = 'customer_custom_status'; // 自定义邮件ID
            $this->title = '自定义订单状态通知';
            $this->description = '订单变为自定义状态时发送的通知邮件';
            $this->customer_email = true;
            $this->template_html = 'emails/customer-custom-status.php';
            $this->template_plain = 'emails/plain/customer-custom-status.php';
            
            parent::__construct();
        }

        public function trigger( $order_id ) {
            if ( $order_id ) {
                $this->object = wc_get_order( $order_id );
                $this->recipient = $this->object->get_billing_email();
                
                if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
                    return;
                }
                
                $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
            }
        }
    }

    $email_classes['WC_Custom_Status_Email'] = new WC_Custom_Status_Email();
    return $email_classes;
}

// 为自定义邮件添加抄送
add_filter( 'woocommerce_email_headers', 'add_cc_to_custom_status_email', 10, 3 );
function add_cc_to_custom_status_email( $headers, $email_id, $order ) {
    if ( $email_id === 'customer_custom_status' ) {
        $headers .= "Cc: your-cc-email@example.com\r\n";
    }
    return $headers;
}

// 订单状态变更时触发邮件
add_action( 'woocommerce_order_status_custom_status_slug', 'trigger_custom_status_email', 10, 2 );
function trigger_custom_status_email( $order_id, $order ) {
    WC()->mailer()->get_emails()['WC_Custom_Status_Email']->trigger( $order_id );
}

注意替换代码中的custom_status_slug为你实际的自定义状态标识,同时确保邮件模板文件存在(可复制默认订单邮件模板修改)。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 16:22:18