【问题标题】:Send failed order email notification to the customer in Woocommerce在 Woocommerce 中向客户发送订单失败电子邮件通知
【发布时间】:2019-05-10 21:55:40
【问题描述】:

我在我的主题 functions.php 文件中使用以下代码仅将订单失败的电子邮件发送给客户而不是管理员:

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

而且它有效……

但我在 php 日志文件中收到以下错误:

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_Braintree\Plugin_Framework\SV_WC_Payment_Gateway_Direct->process_payment, WC_Braintree\Plugin_Framework\SV_WC_Payment_Gateway_Direct->do_transaction, WC_Braintree\Plugin_Framework\SV_WC_Payment_Gateway->do_transaction_failed_result, WC_Braintree\Plugin_Framework\SV_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.

我该如何解决这个错误?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您需要将billing_email 替换为WC_Order 方法get_billing_email() 之类的:

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

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

    【讨论】:

    • 谢谢,在第 4 行应该是 $recipient,同样在电子邮件设置选项卡中,有一个“致命错误”消息,它没有完全加载页面,出了点问题。
    • @tatifox 我已经更新了我的代码……你现在应该不会收到任何错误。
    • 它不发送电子邮件,但是当is_admin() 被替换为! is_a( $order, 'WC_Order' ) 时它工作了。
    • @talifox 是的,你是对的......我已经做出了其他答案......我很累,抱歉(更新)。
    • 它对我不起作用。相反,尝试stackoverflow.com/a/47649154/4446644 并完成!
    猜你喜欢
    • 2017-08-08
    • 2019-01-09
    • 1970-01-01
    • 2018-09-19
    • 2014-03-09
    • 1970-01-01
    • 2020-06-03
    • 2017-08-19
    • 2018-06-07
    相关资源
    最近更新 更多