WooCommerce自定义Shipped订单状态邮件异常问题求助
解决WooCommerce自定义Shipped订单状态邮件触发错误的问题
我来帮你排查这个问题——你的代码里有几个关键逻辑错误,导致切换到Shipped状态时发送的是默认Processing邮件,而非自定义邮件。下面是具体问题分析和完整修复方案:
问题根源
- 错误复用默认邮件对象:你在
shipped_status_custom_notification函数中直接修改并触发了WC_Email_Customer_Processing_Order这个系统默认的Processing邮件对象,这就是为什么收到的是Processing状态邮件的核心原因。 - 未关联自定义邮件类:虽然你添加了
woocommerce_order_status_wc-shipped动作,但没有为这个状态创建对应的自定义邮件类,WooCommerce不知道应该触发哪一封自定义邮件。
完整修复代码
1. 保留正确的自定义状态注册代码
这部分你之前的实现是对的,可以直接保留:
/** * Add custom status to order list */ add_action( 'init', 'register_custom_post_status', 10 ); function register_custom_post_status() { register_post_status( 'wc-shipped', array( 'label' => _x( 'Shipped', 'Order status', 'woocommerce' ), 'public' => true, 'exclude_from_search' => false, 'show_in_admin_all_list' => true, 'show_in_admin_status_list' => true, 'label_count' => _n_noop( 'Shipped <span class="count">(%s)</span>', 'Shipped <span class="count">(%s)</span>', 'woocommerce' ) ) ); } /** * Add custom status to order page drop down */ add_filter( 'wc_order_statuses', 'custom_wc_order_statuses' ); function custom_wc_order_statuses( $order_statuses ) { $order_statuses['wc-shipped'] = _x( 'Shipped', 'Order status', 'woocommerce' ); return $order_statuses; }
2. 创建自定义Shipped邮件类
这是实现自定义邮件的核心,我们需要继承WooCommerce的邮件基类,定义专属的邮件内容、主题等:
// 自定义Shipped状态邮件类 if ( ! class_exists( 'WC_Email_Customer_Shipped_Order' ) ) { class WC_Email_Customer_Shipped_Order extends WC_Email { public function __construct() { // 基础配置:邮件ID、受众、标题描述 $this->id = 'customer_shipped_order'; $this->customer_email = true; $this->title = __( 'Customer Shipped Order', 'woocommerce' ); $this->description = __( '发送给用户的订单已发货通知邮件', 'woocommerce' ); $this->template_html = 'emails/customer-shipped-order.php'; $this->template_plain = 'emails/plain/customer-shipped-order.php'; $this->placeholders = array( '{order_date}' => '', '{order_number}' => '', ); // 绑定邮件触发动作 add_action( 'woocommerce_order_status_wc-shipped_notification', array( $this, 'trigger' ), 10, 2 ); // 调用父类构造完成初始化 parent::__construct(); } // 触发邮件的逻辑 public function trigger( $order_id, $order = false ) { $this->setup_locale(); 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->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() ); $this->placeholders['{order_number}'] = $this->object->get_order_number(); } // 检查邮件是否启用且收件人有效,然后发送 if ( $this->is_enabled() && $this->get_recipient() ) { $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); } $this->restore_locale(); } // 默认邮件主题 public function get_default_subject() { return __( '你的{site_title}订单已发货', 'woocommerce' ); } // 默认邮件标题(HTML邮件内的标题) public function get_default_heading() { return __( '你的订单已发货', 'woocommerce' ); } // 获取HTML格式邮件内容 public function get_content_html() { return wc_get_template_html( $this->template_html, array( 'order' => $this->object, 'email_heading' => $this->get_heading(), 'sent_to_admin' => false, 'plain_text' => false, 'email' => $this, ) ); } // 获取纯文本格式邮件内容 public function get_content_plain() { return wc_get_template_html( $this->template_plain, array( 'order' => $this->object, 'email_heading' => $this->get_heading(), 'sent_to_admin' => false, 'plain_text' => true, 'email' => $this, ) ); } } }
3. 注册自定义邮件到WooCommerce系统
把刚才创建的邮件类添加到WooCommerce的邮件列表中:
add_filter( 'woocommerce_email_classes', 'add_custom_shipped_email' ); function add_custom_shipped_email( $emails ) { $emails['WC_Email_Customer_Shipped_Order'] = new WC_Email_Customer_Shipped_Order(); return $emails; }
4. 修正状态切换时的邮件触发逻辑
替换你之前的shipped_status_custom_notification函数,改为触发我们的自定义邮件:
add_action('woocommerce_order_status_changed', 'trigger_shipped_custom_notification', 10, 4); function trigger_shipped_custom_notification( $order_id, $from_status, $to_status, $order ) { // 仅当订单状态切换为shipped时触发 if( $to_status === 'shipped' ) { do_action( 'woocommerce_order_status_wc-shipped_notification', $order_id, $order ); } }
自定义邮件模板(可选)
如果你需要修改邮件的具体内容,可以按以下步骤操作:
- 复制WooCommerce默认的邮件模板文件(比如
wp-content/plugins/woocommerce/templates/emails/customer-processing-order.php) - 粘贴到你的主题目录下的
woocommerce/emails文件夹中(如果没有该文件夹就新建) - 重命名为
customer-shipped-order.php - 按照你的需求修改模板内的文字和排版即可
内容的提问来源于stack exchange,提问作者Fawad
相关产品推荐
相关产品推荐

