2
votes

I need to send cancelled and failed order email to customers in Woocommerce 3.4+. I'm constantly getting Fatal error: Uncaught Error: Call to a member function get_billing_email() on null in I've tried few function (like below) from stackoverflow with same result:

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 );

What is wrong? How can I avoid this error?

2

2 Answers

1
votes

I have found a very nice approach:

First, extend the woocommerce default emails by adding a filter to woocommerce_email_classes:

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;
}); 

Then, we create the ./wc-emails/class-wc-customer-order-failed.php file. I recommend starting with one of the original WC_Email classes, located under /wp-content/plugins/woocommerce/emails/class-wc-email-{$email-name}.php. Make sure you rename the class after you do it. As the time of this writing I'm using Woocommerce 5.3.0, and the class-wc-customer-order-failed.php should look like this:

<?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();

Please note

In occasion of Woocommerce update, you should check if the customised files have been updated, and its up to you to keep compatibility :)

3
votes

You should need check that $order argument is valid instance of the WC_Order Class:

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();
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

You could also use instead in this particular case:

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

Related and similar: