Skip to content
Advertisement

Send failed order email notification to the customer in Woocommerce

I’m using the following code in my theme functions.php file to send order failed email only to customer instead of admin:

function wc_failed_order_email_to_customer( $recipient, $order ){
     return $recipient = $order->billing_email;
}
add_filter( 'woocommerce_email_recipient_failed_order', 'wc_failed_order_email_to_customer', 10, 2 );

And it works…

But I’m getting the following error in php log file:

Error: billing_email was called incorrectly. Order properties should not be accessed directly. Backtrace: require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), do_action('template_redirect'), WP_Hook->do_action, WP_Hook->apply_filters, WC_AJAX::do_wc_ajax, do_action('wc_ajax_checkout'), WP_Hook->do_action, WP_Hook->apply_filters, WC_AJAX::checkout, WC_Checkout->process_checkout, WC_Checkout->process_order_payment, WC_BraintreePlugin_FrameworkSV_WC_Payment_Gateway_Direct->process_payment, WC_BraintreePlugin_FrameworkSV_WC_Payment_Gateway_Direct->do_transaction, WC_BraintreePlugin_FrameworkSV_WC_Payment_Gateway->do_transaction_failed_result, WC_BraintreePlugin_FrameworkSV_WC_Payment_Gateway->mark_order_as_failed, WC_Order->update_status, WC_Order->save, WC_Order->status_transition, do_action('woocommerce_order_status_pending_to_failed'), WP_Hook->do_action, WP_Hook->apply_filters, WC_Emails::send_transactional_email, do_action_ref_array, WP_Hook->do_action, WP_Hook->apply_filters, WC_Email_Failed_Order->trigger, WC_Email->get_recipient, apply_filters('woocommerce_email_recipient_failed_order'), WP_Hook->apply_filters, wc_failed_order_email_to_customer, WC_Abstract_Legacy_Order->__get, wc_doing_it_wrong. This message was added in version 3.0.

How can I solve this error?

Advertisement

Answer

You need to replace billing_email by the WC_Order method get_billing_email() like:

add_filter( 'woocommerce_email_recipient_failed_order', 'wc_failed_order_email_to_customer', 10, 2 );
function wc_failed_order_email_to_customer( $recipient, $order ){
     if( ! is_a( $order, 'WC_Order' ) ) 
         return $recipient;

     if( $billing_email = $order->get_billing_email() ) 
         $recipient = $billing_email;
     return $recipient;
}

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

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement