简体   繁体   English

WooCommerce电子邮件未通过WooCommerce外部的自定义WordPress页面模板发送

[英]WooCommerce email not sending via custom WordPress page template outside of WooCommerce

I've created a new page in WordPress and set a custom made template as template for the new page. 我已经在WordPress中创建了一个新页面,并设置了一个自定义模板作为新页面的模板。

In my template I'm doing some stuff like show a HTML content. 在我的模板中,我正在做一些事情,例如显示HTML内容。 After all worked fine I've decided to add a do_action in my custom template to sent an email via WooCommerce email client/class. 一切正常之后,我决定在自定义模板中添加一个do_action以通过WooCommerce电子邮件客户端/类发送电子邮件。

So I've set up a new email class and build a custom trigger in this class which triggers the email sending: 因此,我建立了一个新的电子邮件类,并在该类中构建了一个自定义触发器,该触发器触发了电子邮件的发送:

// Triggers for this email.
add_action( 'trigger_rated_email', array( $this, 'trigger' ), 10, 10 );

/**
     * Trigger the sending of this email.
     *
     * @param int $order_id The order ID.
     * @param WC_Order|false $order Order object.
     */
    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' ) ) {
            $developer_id = get_post_meta( $order_id, 'developer_id', true );
            $developer    = get_userdata( $developer_id );

            $this->object                         = $order;
            $this->recipient                      = $developer->user_email;
            $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();
    }

This add_action triggers the trigger() method in the email class which sends the email. add_action触发发送电子邮件的电子邮件类中的trigger()方法。 To trigger the sending I've done this in my custom template PHP file: 为了触发发送,我已经在我的自定义模板PHP文件中完成了此操作:

do_action( 'trigger_rated_email', $order_id );

After saving it I've uploaded it to my server and called the page but there is no email sent to my email account. 保存后,我将其上传到服务器并调用了该页面,但是没有电子邮件发送到我的电子邮件帐户。

So I've did a lot of checks to clarify if there is a problem in my email class: 因此,我做了很多检查来弄清楚我的电子邮件班级是否存在问题:

  1. Triggered the email from an other page (not custom template) -> email gets sent 从其他页面触发电子邮件(不是自定义模板)->发送电子邮件
  2. Checked if there is an error in my debug.log -> no error 检查我的debug.log中是否有错误->没有错误

So what is the problem? 那是什么问题呢? I think there must be a problem in the custom template so my first thoughts are that the do_action is not called correctly because it don't knows the add_action from my class. 我认为自定义模板中肯定存在问题,因此我的第一想到是do_action没有正确调用,因为它不知道类中的add_action

After a lot of hours and a help from a good friend we've found the problem. 经过许多小时的努力和一个好朋友的帮助,我们发现了问题所在。 The point is that when you want to sent email from a non WooCommerce page you need to initialize the WooCommerce mailer first. 关键是,当您要从非WooCommerce页面发送电子邮件时,需要首先初始化WooCommerce邮件程序。

So for all who wants to sent emails from a non WooCommerce page do this here: 因此,对于所有想要从非WooCommerce页面发送电子邮件的人,请在此处执行以下操作:

WC()->mailer();
do_action( 'trigger_your_custom_email', $order_id );

In your custom WooCommerce email class do this here: 在您的自定义WooCommerce电子邮件类中,请在此处执行以下操作:

add_action( 'trigger_your_custom_email', array( $this, 'trigger' ), 10, 10 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM