You need to enable JavaScript to run this app. WooCommerce自定义邮件模板设置template_base后发送内容为空
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce自定义邮件模板设置template_base后发送内容为空

问题原因拆解

你遇到的“后台能正常显示模板配置、邮件却发空内容、放主题目录就正常”的现象,是开发WooCommerce自定义邮件类时非常容易踩的坑,核心不是路径写错了,而是执行逻辑和WooCommerce的内置规则没对齐:

  • 父类构造函数覆盖自定义属性
    你当前的代码顺序是先给$this->template_base、$this->template_html赋值,最后才调用parent::__construct()。但WooCommerce内置的WC_Email父类构造逻辑,会在初始化时强制把$this->template_base重置为WooCommerce核心自带的模板目录,直接覆盖你之前设置的插件路径。
    后台之所以能显示正确的模板路径,是因为邮件设置页的路径展示是纯文本拼接类内定义的字符串,根本不会读取运行时被父类覆盖后的实际属性值,才会出现视觉上“路径正确”的假象。
  • 模板查找优先级规则的干扰
    WooCommerce查找邮件模板的固定顺序是:优先加载主题目录下woocommerce/emails/内的同名模板 → 遍历过滤器注册的自定义路径 → 最后才读取类属性指向的自定义模板目录。这也是你把模板放到主题下就能正常运行的原因——它根本没走到你插件目录的查找分支。
  • 遗漏了模板加载的强制指定逻辑
    很多人以为只要设置了$template_base属性,WooCommerce就会自动去对应目录找模板,但实际上核心的wc_get_template_html()函数,只有在收到明确传入的模板根路径参数时,才会优先查找自定义目录的文件,否则会严格按默认优先级逻辑查找。

修复方案

  1. 调整构造函数内的代码执行顺序:必须先调用父类构造完成核心初始化,再赋值所有自定义属性,避免被父类逻辑覆盖。
  2. 统一处理路径格式:用trailingslashit()给所有路径补全末尾斜杠,避免路径拼接错误。
  3. 重写get_content_html()方法,在调用模板加载函数时显式传入自定义模板路径,跳过默认优先级的干扰。

调整后的核心代码参考:

class SV_SplitEmail_New_Order extends WC_Email
{
    public function __construct() {
        // 第一步必须先调用父类构造,避免后续赋值被覆盖
        parent::__construct();

        // 父类初始化完成后再覆盖自定义属性
        $this->id              = 'sv_split_new_order';
        $this->title           = __( 'New Order - Split Orders', 'woocommerce' );
        $this->description     = __( 'New order emails are sent to chosen recipient(s) when a new order is received. Compatible with Split and Plain orders. The default "New Order" email should be disabled.', 'woocommerce' );
        $this->customer_email  = false;
        // 统一处理路径格式,确保末尾带斜杠
        $this->template_base   = trailingslashit( SVTHEMING_PATH . 'app/Providers/Order_Splitting/templates' );
        $this->template_html   = 'emails/admin-new-split-order.php';
        $this->placeholders    = array(
            '{order_date}'   => '',
            '{order_number}' => '',
        );
        $this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ) );

        // 注册所有状态切换的触发钩子
        add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
        add_action( 'woocommerce_order_status_pending_to_completed_notification', array( $this, 'trigger' ), 10, 2 );
        add_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );
        add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
        add_action( 'woocommerce_order_status_failed_to_completed_notification', array( $this, 'trigger' ), 10, 2 );
        add_action( 'woocommerce_order_status_failed_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );
        add_action( 'woocommerce_order_status_cancelled_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
        add_action( 'woocommerce_order_status_cancelled_to_completed_notification', array( $this, 'trigger' ), 10, 2 );
        add_action( 'woocommerce_order_status_cancelled_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );
    }

    /**
     * 触发邮件发送
     */
    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->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();
    }

    /**
     * 获取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' => true,
                'plain_text'    => false,
                'email'         => $this,
            ),
            '',
            $this->template_base
        );
    }

测试注意事项

  • 测试时先临时清空主题目录下的同名邮件模板,避免被优先级规则优先加载干扰结果。
  • 确认SVTHEMING_PATH常量定义的是插件根目录的绝对路径,且模板文件的读权限正常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.28 21:36:04