【问题标题】:Send cancelled and failed order email to customer in Woocommerce 3在 Woocommerce 3 中向客户发送取消和失败的订单电子邮件
【发布时间】:2019-01-09 04:16:26
【问题描述】:

我需要向 Woocommerce 3.4+ 中的客户发送取消和失败的订单电子邮件。 我不断收到Fatal error: Uncaught Error: Call to a member function get_billing_email() on null in 我从 stackoverflow 中尝试了几个函数(如下所示),结果相同:

function wc_cancelled_order_add_customer_email( $recipient, $order )
{
 return $recipient .= "," . $order->get_billing_email();
}
add_filter( 'woocommerce_email_recipient_cancelled_order', 'wc_cancelled_order_add_customer_email', 10, 2 );
add_filter( 'woocommerce_email_recipient_failed_order', 'wc_cancelled_order_add_customer_email', 10, 2 );

怎么了?如何避免此错误?

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    您应该检查$order 参数是否是WC_Order 类的有效实例:

    add_filter( 'woocommerce_email_recipient_cancelled_order', 'wc_cancelled_order_add_customer_email', 10, 2 );
    add_filter( 'woocommerce_email_recipient_failed_order', 'wc_cancelled_order_add_customer_email', 10, 2 );
    function wc_cancelled_order_add_customer_email( $recipient, $order ){
        // Avoiding errors in backend (mandatory when using $order argument)
        if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
    
        return $recipient .= "," . $order->get_billing_email();
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    您也可以在这种特殊情况下使用:

    // Avoiding errors in backend (mandatory when using $order argument)
    if ( ! method_exists( $order, 'get_billing_email' ) ) return $recipient;
    

    相关和相似:

    【讨论】:

    • 谢谢你,成功了!
    • 你也可以使用这种不同的方式:Sending email to customer on cancelled order in Woocommerce...
    • 我已经尝试过该功能,但似乎并非每次都触发电子邮件......它适用于新的状态更改,而不是它似乎停止工作。我知道这是合乎逻辑的,但有些客户是好奇的人 :-)
    • 奇怪的事情发生了。它今天停止工作是否有原因(向客户发送电子邮件,没有任何更新)...只有当我删除if( is_admin() ) return $recipient;它才能再次工作:-)
    • 我也期待,但是 php 错误日志是空的。在if( is_admin() ) return $recipient; 停止工作后,我首先再次尝试了上面topic 建议的代码(它会产生php 错误),然后我尝试返回并删除if( is_admin() ) return $recipient;,它现在可以工作了(但它之前产生了错误: -))。
    【解决方案2】:

    我找到了一个非常好的方法:

    首先,通过向woocommerce_email_classes 添加过滤器来扩展 woocommerce 默认电子邮件:

    add_filter('woocommerce_email_classes', function ($classes) {
       $classes['WC_Email_Customer_Order_Failed'] = include __DIR__ . "/wc-emails/class-wc-customer-order-failed.php";
       // you can add as many classes as you want here
       return $classes;
    }); 
    

    然后,我们创建./wc-emails/class-wc-customer-order-failed.php 文件。我建议从位于/wp-content/plugins/woocommerce/emails/class-wc-email-{$email-name}.php 下的原始WC_Email 类之一开始。确保在完成后重命名课程。在撰写本文时,我使用的是 Woocommerce 5.3.0,class-wc-customer-order-failed.php 应该如下所示:

    <?php
    
    if (!defined('ABSPATH')) {
        exit; // Exit if accessed directly.
    }
    
    if (!class_exists('WC_Email_Customer_Order_Failed', false)) {
    
    /**
     * Cancelled Order Email.
     *
     * An email sent to the admin when an order is cancelled.
     *
     * @class       WC_Email_Cancelled_Order
     * @version     2.2.7
     * @package     WooCommerce\Classes\Emails
     * @extends     WC_Email
     */
    class WC_Email_Customer_Order_Failed extends WC_Email
    {
    
        /**
         * Constructor.
         */
        public function __construct()
        {
            $this->id             = 'custom_failed_order';
            // add this to send to customer
            $this->customer_email = true;
            $this->title          = 'This is a custom email template';
            $this->description    = 'You can modify this text to appear in the admin area';
            // I'm pointing to a new template which I created in the THEME folder
            // to use default email file should be admin-failed-order.php
            $this->template_html  = 'emails/customer-failed-order.php';
            $this->template_plain = 'emails/plain/customer-failed-order.php';
            $this->placeholders   = array(
                '{order_date}'   => '',
                '{order_number}' => '',
            );
    
            // Triggers for this email.
            add_action('woocommerce_order_status_pending_to_failed_notification', array($this, 'trigger'), 10, 2);
            add_action('woocommerce_order_status_on-hold_to_failed_notification', array($this, 'trigger'), 10, 2);
    
            // Call parent constructor.
            parent::__construct();
    
        }
    
        /**
         * Get email subject.
         *
         * @since  3.1.0
         * @return string
         */
        public function get_default_subject()
        {
            return __('[{site_title}]: Order #{order_number} has failed', 'woocommerce');
        }
    
        /**
         * Get email heading.
         *
         * @since  3.1.0
         * @return string
         */
        public function get_default_heading()
        {
            return __('Order Failed: #{order_number}', 'woocommerce');
        }
    
        /**
         * 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')) {
                $this->object                         = $order;
                // this is the correct place to get_billing_email() working
                $this->recipient                      = $this->object->get_billing_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();
        }
    
        /**
         * Get content 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(),
                    'additional_content' => $this->get_additional_content(),
                    'sent_to_admin'      => false,
                    'plain_text'         => false,
                    'email'              => $this,
                )
            );
        }
    
        /**
         * Get content plain.
         *
         * @return string
         */
        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(),
                    // remove sending to admin, there is another email that does that
                    'sent_to_admin'      => false,
                    'plain_text'         => true,
                    'email'              => $this,
                )
            );
        }
    
        /**
         * Default content to show below main email content.
         *
         * @since 3.7.0
         * @return string
         */
        public function get_default_additional_content()
        {
            return __('Hopefully they’ll be back. Read more about <a href="https://docs.woocommerce.com/document/managing-orders/">troubleshooting failed payments</a>.', 'woocommerce');
        }
    
        /**
         * Initialise settings form fields.
         */
        public function init_form_fields()
        {
            /* translators: %s: list of placeholders */
            $placeholder_text  = sprintf(__('Available placeholders: %s', 'woocommerce'), '<code>' . esc_html(implode('</code>, <code>', array_keys($this->placeholders))) . '</code>');
            $this->form_fields = array(
                'enabled'            => array(
                    'title'   => __('Enable/Disable', 'woocommerce'),
                    'type'    => 'checkbox',
                    'label'   => __('Enable this email notification', 'woocommerce'),
                    'default' => 'yes',
                ),
                // no need to define recipients in the admin panel
                // 'recipient'          => array(
                //     'title'       => __('Recipient(s)', 'woocommerce'),
                //     'type'        => 'text',
                //     /* translators: %s: WP admin email */
                //     'description' => sprintf(__('Enter recipients (comma separated) for this email. Defaults to %s.', 'woocommerce'), '<code>' . esc_attr(get_option('admin_email')) . '</code>'),
                //     'placeholder' => '',
                //     'default'     => '',
                //     'desc_tip'    => true,
                // ),
                'subject'            => array(
                    'title'       => __('Subject', 'woocommerce'),
                    'type'        => 'text',
                    'desc_tip'    => true,
                    'description' => $placeholder_text,
                    'placeholder' => $this->get_default_subject(),
                    'default'     => '',
                ),
                'heading'            => array(
                    'title'       => __('Email heading', 'woocommerce'),
                    'type'        => 'text',
                    'desc_tip'    => true,
                    'description' => $placeholder_text,
                    'placeholder' => $this->get_default_heading(),
                    'default'     => '',
                ),
                'additional_content' => array(
                    'title'       => __('Additional content', 'woocommerce'),
                    'description' => __('Text to appear below the main email content.', 'woocommerce') . ' ' . $placeholder_text,
                    'css'         => 'width:400px; height: 75px;',
                    'placeholder' => __('N/A', 'woocommerce'),
                    'type'        => 'textarea',
                    'default'     => $this->get_default_additional_content(),
                    'desc_tip'    => true,
                ),
                'email_type'         => array(
                    'title'       => __('Email type', 'woocommerce'),
                    'type'        => 'select',
                    'description' => __('Choose which format of email to send.', 'woocommerce'),
                    'default'     => 'html',
                    'class'       => 'email_type wc-enhanced-select',
                    'options'     => $this->get_email_type_options(),
                    'desc_tip'    => true,
                ),
            );
        }
    }
    }
    
    return new WC_Email_Customer_Order_Failed();
    

    请注意

    在 Woocommerce 更新时,您应该检查自定义文件是否已更新,并由您决定是否保持兼容性:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-10
      • 2017-08-08
      • 2014-03-09
      • 2020-06-03
      • 2017-08-19
      • 2016-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多