导读:本期聚焦于小伙伴创作的《WooCommerce订单中如何根据特定商品条件触发自定义邮件》,敬请观看详情,探索知识的价值。以下视频、文章将为您系统阐述其核心内容与价值。如果您觉得《WooCommerce订单中如何根据特定商品条件触发自定义邮件》有用,将其分享出去将是对创作者最好的鼓励。

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

WooCommerce订单中如何根据特定商品条件触发自定义邮件

实现核心思路

整个功能的核心逻辑分为三步:首先监听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

免责声明:已尽一切努力确保本网站所含信息的准确性。网站部分内容来源于网络或由用户自行发表,内容观点不代表本站立场。本站是个人网站免费分享,内容仅供个人学习、研究或参考使用,如内容中引用了第三方作品,其版权归原作者所有。若内容触犯了您的权益,请联系我们进行处理。
内容垂直聚焦
专注技术核心技术栏目,确保每篇文章深度聚焦于实用技能。从代码技巧到架构设计,为用户提供无干扰的纯技术知识沉淀,精准满足专业提升需求。
知识结构清晰
覆盖从开发到部署的全链路。前端、网络、数据库、服务器、建站、系统层层递进,构建清晰学习路径,帮助用户系统化掌握网站开发与运维所需的核心技术栈。
深度技术解析
拒绝泛泛而谈,深入技术细节与实践难点。无论是数据库优化还是服务器配置,均结合真实场景与代码示例进行剖析,致力于提供可直接应用于工作的解决方案。
专业领域覆盖
精准对应开发生命周期。从前端界面到后端逻辑,从数据库操作到服务器运维,形成完整闭环,一站式满足全栈工程师和运维人员的技术需求。
即学即用高效
内容强调实操性,步骤清晰、代码完整。用户可根据教程直接复现和应用于自身项目,显著缩短从学习到实践的距离,快速解决开发中的具体问题。
持续更新保障
专注既定技术方向进行长期、稳定的内容输出。确保各栏目技术文章持续更新迭代,紧跟主流技术发展趋势,为用户提供经久不衰的学习价值。