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

如何从WooCommerce客户预订取消邮件中移除供应商收件人

解决WooCommerce Bookings取消预订邮件仅发送给客户的问题

原代码的问题分析

你之前的代码存在几个关键问题:

  • 直接返回数组而非WooCommerce要求的逗号分隔的邮箱字符串,邮件系统无法正确解析
  • 获取全站所有供应商邮箱的逻辑不合理,应该定位到当前预订对应的供应商,而非所有供应商
  • 错误地用客户邮箱去对比供应商邮箱,忽略了原始收件人列表里的实际内容

修正后的实现方案

方案1:精准过滤当前预订的供应商邮箱

假设你的供应商是预订关联商品的作者(多供应商场景常见逻辑),可以用以下代码:

add_filter( 'woocommerce_email_recipient_customer_booking_cancelled', 'exclude_vendor_from_cancellation_email', 20, 2 );

function exclude_vendor_from_cancellation_email( $recipient, $booking ) {
    // 仅在有有效预订对象时执行
    if ( ! is_a( $booking, 'WC_Booking' ) ) {
        return $recipient;
    }

    // 获取当前预订关联的商品ID
    $product_id = $booking->get_product_id();
    // 获取商品对应的供应商邮箱(假设供应商是商品作者)
    $vendor_id = get_post_field( 'post_author', $product_id );
    $vendor_email = get_userdata( $vendor_id )->user_email;

    // 将收件人字符串拆分为数组
    $recipients = explode( ',', $recipient );
    // 移除供应商邮箱
    $filtered_recipients = array_filter( $recipients, function( $email ) use ( $vendor_email ) {
        return trim( $email ) !== $vendor_email;
    } );

    // 重新合并为逗号分隔的字符串返回
    return implode( ',', $filtered_recipients );
}

方案2:强制仅保留客户邮箱

如果不需要考虑原始收件人列表,直接只发送给预订客户:

add_filter( 'woocommerce_email_recipient_customer_booking_cancelled', 'only_send_cancellation_email_to_customer', 20, 2 );

function only_send_cancellation_email_to_customer( $recipient, $booking ) {
    if ( is_a( $booking, 'WC_Booking' ) ) {
        // 直接返回客户邮箱
        return $booking->get_customer_email();
    }
    return $recipient;
}

其他可用钩子说明

  • woocommerce_booking_cancelled_notification:可以拦截整个取消通知的发送逻辑,完全自定义收件人规则
  • woocommerce_email_headers:如需更精细控制邮件头部信息,可通过此钩子调整

注意:代码需添加到你的主题functions.php文件或自定义插件中,修改前请备份站点数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 15:57:48