我试图发送邮箱从我的插件使用标准的WooCommerce布局,但我想使用我自己的模板位于我的插件.当我将我的模板移动到/wp-content/plugins/woocommerce/templates/emails时,以下代码可以工作:

function get_custom_email_html( $heading = false, $mailer ) {

    $template = 'emails/my-custom-email-i-want-to-send.php';

    return wc_get_template_html( $template, array(      
        'email_heading' => $heading,
        'sent_to_admin' => false,
        'plain_text'    => false,
        'email'         => $mailer
    ) );

}

// load the mailer class
$mailer = WC()->mailer();

//format the email
$recipient = "someone@somewhere.com";
$subject = __("Hi! Here is a custom notification from us!", 'theme_name');
$content = get_custom_email_html( $subject, $mailer );
$headers = "Content-Type: text/html\r\n";

//send the email through wordpress
$mailer->send( $recipient, $subject, $content, $headers );

我假设我需要添加一个指向GET_CUSTOM_EMAIL_html()的路径,但我不确定如何添加.有人能给我个提示吗? 我的模板是一个非常简单的:

/*
 * @hooked WC_Emails::email_header() Output the email header
 */
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
<p><?php esc_html_e( 'This a test', 'my-plugin' ); ?></p>
<?php
/*
 * @hooked WC_Emails::email_footer() Output the email footer
 */
do_action( 'woocommerce_email_footer', $email );

推荐答案

您的插件代码中缺少一些东西,比如在使用wc_get_template_html()时需要定义的插件路径.

为了测试,我已经触发了邮箱使用一个功能钩在template_redirect动作挂钩时,在前端浏览,限制为"管理员"用户角色.

我已经更改了模板名称和路径:在您的插件文件夹中,该文件现在位于:templates/emails/my-custom-email.php.

代码:

function my_plugin_path(){
    return untrailingslashit( plugin_dir_path( __FILE__ ) ); 
}

function get_custom_email_html( $mailer, $email_heading = false ) {
    $template = 'emails/my-custom-email.php';

    return wc_get_template_html( $template, array(      
        'email_heading' => $email_heading,
        'sent_to_admin' => false,
        'plain_text'    => false,
        'email'         => $mailer
    ), '', my_plugin_path() . "/templates/" );
}

add_action('template_redirect', 'send_custom_email_notifications');
function send_custom_email_notifications(){
    if ( current_user_can('administrator')) {
        // Get the WC_Emails object
        $mailer = WC()->mailer();

        // Define variable arguments for the email
        $recipient = "someone@somewhere.com";
        $subject = __("Hi! Here is a custom notification from us!", 'theme_name');
        $content = get_custom_email_html( $mailer, $subject );
        $headers = "Content-Type: text/html\r\n";

        // Send the email using WooCommerce WC_Emails send() method
        $mailer->send( $recipient, $subject, $content, $headers );
    }
}

经过测试,它起作用了.

注意:我已经使用了您的邮箱模板的Html内容.

Php相关问答推荐

允许Laravel路由指定`withTrashed`,尽管有显式的模型绑定

在Woocommerce变量产品中的标签附近显示所选属性选项名称

照明\Support\Collection::Filter():参数#1($Callback)的类型必须为?可调用、已给定数组、已调用

将变体设置字段添加到WooCommerce中的特定变量产品

ForHumans函数中的月当量与单位限制

Php解压缩deflate64

根据产品类型显示或隐藏WooCommerce管理产品自定义字段

在Laravel中修改JSON输出

在WooCommerce获取所有具有全球产品属性品牌的简单产品

如何在execute语句中使用存储过程参数?

使用 php Carbon 获取两个日期之间的随机日期和时间

用户注册表单中的 HTTP 错误 405

跟踪通过 WooCommerce 分层定价表插件进行的购买

在WooCommerce购物车和 checkout 页面中更改总计文本

如何设置Mailersend PHP API 模板变量?

PHP header() 是否缓存重定向,如果是,如何防止它这样做?

如何在不解压缩/重新压缩的情况下连接(连接)两个缩小的值

在 laravel 中更新表单时如何在 Select 选项中保留旧值?

fopen 功能不断向我发送我重新加载网页时已发布的重复版本的表单

如何将文件直接上传到 Cloudinary 而不先保存在 public 文件夹中