自定义订单状态切换至processing时触发处理邮件的方法
嘿,我来帮你搞定这个WooCommerce自定义状态切换触发邮件的问题!
你已经完成了关键的第一步——通过woocommerce_email_actions过滤器,把woocommerce_order_status_tree_to_processing这个状态切换动作加入到了WooCommerce的邮件触发列表中。接下来只需要确认几个核心点,就能让邮件正常触发:
1. 订单状态切换时会自动触发动作
WooCommerce本身有个内置机制:当订单状态从{旧状态}切换到{新状态}时,会自动触发woocommerce_order_status_{旧状态}_to_{新状态}这个动作。所以当你手动(或者通过代码)把订单状态从tree(标签waiting in tree)改为processing时,这个动作会被自动触发,而你的过滤器已经把这个动作和邮件系统关联起来了,这一步不需要额外写代码。
⚠️ 注意:如果是在后台手动修改订单状态,一定要确保是从waiting in tree这个自定义状态切换到Processing,对应的动作才会被触发。
2. 确保Processing状态的邮件通知已启用
默认情况下,WooCommerce的「订单已处理」邮件是针对订单进入processing状态时发送的,我们需要确认这个通知已经开启:
- 进入WooCommerce后台 → 设置 → 邮件
- 找到「订单已处理(Processing Order)」选项,点击「管理」
- 确保「启用该通知」是勾选状态,并且收件人设置正确(默认是下单客户)
额外:自定义专属邮件内容(可选)
如果你想针对从tree到processing的切换,发送和默认Processing邮件不一样的内容,可以注册一个自定义邮件类,示例代码如下:
// 注册自定义邮件类 add_action( 'woocommerce_email_classes', 'add_tree_to_processing_email_class' ); function add_tree_to_processing_email_class( $email_classes ) { // 避免重复加载类 if ( ! class_exists( 'WC_Email_Tree_To_Processing' ) ) { class WC_Email_Tree_To_Processing extends WC_Email { public function __construct() { // 配置邮件基础信息 $this->id = 'tree_to_processing'; $this->title = '订单已从等待状态进入处理中'; $this->description = '当订单从waiting in tree状态切换到processing时发送给客户的邮件'; $this->customer_email = true; $this->heading = '您的订单已开始处理'; $this->subject = '[{site_title}] 您的订单 #{order_number} 已开始处理'; // 绑定触发动作 add_action( 'woocommerce_order_status_tree_to_processing', array( $this, 'trigger' ), 10, 2 ); parent::__construct(); } // 邮件触发逻辑 public function trigger( $order_id, $order = false ) { if ( $order_id && ! is_a( $order, 'WC_Order' ) ) { $order = wc_get_order( $order_id ); } if ( is_a( $order, 'WC_Order' ) ) { $this->object = $order; $this->recipient = $this->object->get_billing_email(); $this->find['order-number'] = '{order_number}'; $this->replace['order-number'] = $this->object->get_order_number(); } 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() ); } // HTML版邮件内容 public function get_content_html() { ob_start(); wc_get_template( 'emails/customer-tree-to-processing.php', array( 'order' => $this->object, 'email_heading' => $this->get_heading(), 'sent_to_admin' => false, 'plain_text' => false, 'email' => $this, ) ); return ob_get_clean(); } // 纯文本版邮件内容 public function get_content_plain() { ob_start(); wc_get_template( 'emails/plain/customer-tree-to-processing.php', array( 'order' => $this->object, 'email_heading' => $this->get_heading(), 'sent_to_admin' => false, 'plain_text' => true, 'email' => $this, ) ); return ob_get_clean(); } } } $email_classes['WC_Email_Tree_To_Processing'] = new WC_Email_Tree_To_Processing(); return $email_classes; }
之后你需要在当前主题的woocommerce/emails/目录下,创建对应的模板文件customer-tree-to-processing.php(HTML版)和plain/customer-tree-to-processing.php(纯文本版),自定义邮件的具体内容即可。
总结
你现有的代码已经完成了邮件动作的注册,只要确保:
- 订单确实是从
tree状态切换到processing状态 - WooCommerce的「订单已处理」邮件(或你自定义的邮件)已启用
邮件就会自动触发发送啦!
内容的提问来源于stack exchange,提问作者Shalu Shaji

