WooCommerce作为主流的WordPress电商插件,默认邮件规则比较通用,很多时候无法满足个性化业务需求。当用户需要针对订单中的特定商品触发专属邮件时,通过自定义代码实现是更灵活、成本更低的方式。下面我们就一步步讲解完整的实现方法。

实现核心思路
整个功能的核心逻辑分为三步:首先监听WooCommerce订单状态变更的事件,然后在事件回调中遍历订单商品,判断是否满足特定商品条件,最后条件匹配时调用自定义邮件方法发送邮件。这里我们用到的核心钩子是woocommerce_order_status_changed,它会在订单状态发生变更时触发,非常适合做订单相关的后续操作。
判断订单是否包含特定商品
首先需要编写一个函数,用来检查订单中是否包含我们指定的商品。这里的特定商品可以通过商品ID、商品SKU或者商品分类来定义,下面以商品ID为例实现判断逻辑。
/**
* 检查订单是否包含指定ID的商品
* @param WC_Order $order 订单对象
* @param array $target_product_ids 目标商品ID数组
* @return bool 是否包含目标商品
*/
function check_order_has_target_product($order, $target_product_ids) {
// 获取订单所有商品
$items = $order->get_items();
foreach ($items as $item) {
// 获取商品ID
$product_id = $item->get_product_id();
if (in_array($product_id, $target_product_ids)) {
return true;
}
}
return false;
}编写自定义邮件内容
WooCommerce的邮件系统基于PHPMailer实现,我们可以通过woocommerce_email_classes钩子注册自定义邮件类,在类中定义邮件的标题、内容、收件人等信息。下面的示例是给客户发送包含特定商品使用说明的邮件。
/**
* 注册自定义邮件类
* @param array $email_classes 已有邮件类数组
* @return array 新增后的邮件类数组
*/
function register_custom_product_email($email_classes) {
// 自定义邮件类需要继承WC_Email
class WC_Email_Custom_Product_Notice extends WC_Email {
public function __construct() {
$this->id = 'custom_product_notice';
$this->title = '特定商品通知邮件';
$this->description = '当订单包含指定商品时发送给客户的自定义邮件';
$this->heading = '您的订单包含专属商品';
$this->subject = '订单专属商品使用说明';
// 收件人设置为客户
$this->recipient = $this->get_option('recipient', get_option('admin_email'));
// 模板路径,这里用默认路径即可,也可以自定义模板
$this->template_base = WC()->plugin_path() . '/templates/';
$this->template_html = 'emails/customer-note.php';
$this->template_plain = 'emails/plain/customer-note.php';
parent::__construct();
}
/**
* 触发邮件发送
* @param int $order_id 订单ID
*/
public function trigger($order_id) {
if (!$order_id) {
return;
}
$this->object = wc_get_order($order_id);
$this->recipient = $this->object->get_billing_email();
if ($this->is_enabled() && $this->get_recipient()) {
$this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments());
}
}
/**
* 获取邮件HTML内容
* @return string 邮件内容
*/
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
),
'',
$this->template_base
);
}
/**
* 获取邮件纯文本内容
* @return string 邮件内容
*/
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
),
'',
$this->template_base
);
}
}
$email_classes['WC_Email_Custom_Product_Notice'] = new WC_Email_Custom_Product_Notice();
return $email_classes;
}
add_filter('woocommerce_email_classes', 'register_custom_product_email');绑定订单状态变更触发逻辑
最后一步就是把判断条件和邮件发送关联起来,在订单状态变更为已付款等指定状态时,自动检查商品条件并发送邮件。下面的示例是当订单状态变为处理中时触发检查。
/**
* 订单状态变更时检查商品并发送自定义邮件
* @param int $order_id 订单ID
* @param string $old_status 旧状态
* @param string $new_status 新状态
* @param WC_Order $order 订单对象
*/
function trigger_custom_email_on_order_status_change($order_id, $old_status, $new_status, $order) {
// 只在订单状态变为处理中时触发,可根据需求调整
if ($new_status !== 'processing') {
return;
}
// 指定需要触发邮件的商品ID,可自行修改
$target_product_ids = array(123, 456);
// 检查订单是否包含目标商品
if (check_order_has_target_product($order, $target_product_ids)) {
// 获取自定义邮件实例并触发发送
$mailer = WC()->mailer();
$emails = $mailer->get_emails();
if (isset($emails['WC_Email_Custom_Product_Notice'])) {
$emails['WC_Email_Custom_Product_Notice']->trigger($order_id);
}
}
}
add_action('woocommerce_order_status_changed', 'trigger_custom_email_on_order_status_change', 10, 4);注意事项
- 商品ID可以在WordPress后台商品编辑页面的URL中找到,也可以直接在商品列表页查看。
- 如果需要给管理员发送邮件,只需要把自定义邮件类的recipient设置为管理员邮箱即可。
- 自定义邮件模板可以复制WooCommerce默认的邮件模板到主题目录下修改,路径为
woocommerce/emails/,修改后把代码中的模板路径指向主题目录即可。 - 测试时可以先创建一个测试订单,选择目标商品,将订单状态改为处理中,查看是否收到对应的自定义邮件。
扩展场景
除了根据商品ID判断,还可以扩展判断逻辑,比如根据商品SKU判断,只需要把获取商品ID的代码替换为获取商品SKU即可:
// 获取商品SKU的判断方式
$product = wc_get_product($product_id);
$sku = $product->get_sku();
if (in_array($sku, $target_skus)) {
return true;
}如果是根据商品分类判断,可以获取商品的分类ID,再和预设的分类ID数组对比,实现方式类似,只需要调整判断条件的逻辑即可。
WooCommerce自定义邮件订单商品条件钩子函数邮件触发修改时间:2026-05-25 16:35:02