问题
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?
回答1:
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:
- Sending email to customer on cancelled order in Woocommerce
- Add recipients based on user role to failed and cancelled WooCommerce emails
来源:https://stackoverflow.com/questions/51635878/send-cancelled-and-failed-order-email-to-customer-in-woocommerce-3