在Woocommerce 3中将取消和失败的订单电子邮件发送给客户

时间:2018-08-01 14:27:33

标签: php wordpress woocommerce orders email-notifications

我需要向 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 );

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

2 个答案:

答案 0 :(得分:2)

您需要检查$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;

相关和相似的内容:

答案 1 :(得分:1)

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

首先,通过向 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 更新的情况下,您应该检查自定义文件是否已更新,并由您来保持兼容性:)