首页 > 解决方案 > WooCommerce 自定义电子邮件模板“template_base”无法正常工作

问题描述

我正在开发一个创建自定义电子邮件模板的插件。

电子邮件模板将存储在插件本身上。但是,这无法正常工作。这是我的电子邮件课程

 class SV_SplitEmail_New_Order extends WC_Email
{

    /**
     * Constructor.
     */
    public function __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   = SVTHEMING_PATH.'app/Providers/Order_Splitting/templates/';
        $this->template_html   = 'emails/admin-new-split-order.php';
        $this->placeholders    = array(
            '{order_date}'   => '',
            '{order_number}' => '',
        );

        // Triggers for this 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 );

        // Call parent constructor.
        parent::__construct();


        // Other settings.
        $this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ) );
    }

这在仪表板上正确显示。

在此处输入图像描述

但是,当电子邮件发出时,它们是空白的。

如果我将电子邮件模板文件放在网站的主题文件夹中,则可以正常工作。

但是,这并不理想,因为在插件的部署/更新时,我总是需要更新主题的电子邮件模板。

我已确保$this->template_base正确定义,并且可以确认,因为仪表板显示正确的文件。

我错过了什么?

标签: wordpresswoocommerce

解决方案


我不确定您是否仍然遇到此问题,但我直到最近才找到解决方法(至少对于我的问题)。希望它会为您解决这个问题,或帮助其他人。

get_content_html()andget_content_plain()函数中,需要传入新的template_base路径。如果你像这样修改你的函数(或者如果你的类中没有它们,则添加它们),它应该解决它:

public function get_content_html() {
            return wc_get_template_html(
                    $this->template_html,
                    array(
                        'order'              => $this->object,
                        'email_heading'      => $this->get_heading(),
                        'additional_content' => $this->get_additional_content(),
                        'sent_to_admin'      => false,
                        'plain_text'         => false,
                        'email'              => $this,
                    ), '', $this->template_base
                );
        }

        public function get_content_plain() {
            return wc_get_template_html(
                    $this->template_plain,
                    array(
                        'order'              => $this->object,
                        'email_heading'      => $this->get_heading(),
                        'additional_content' => $this->get_additional_content(),
                        'sent_to_admin'      => false,
                        'plain_text'         => true,
                        'email'              => $this,
                    ), '', $this->template_base
                );
        }

在那之后,它似乎知道在哪里看,我的新电子邮件课程肯定触发得很好。


推荐阅读