Im使用woocommerce筛选器修改电子邮件管理员通知的收据“woocommerce_email_recipient_new_order
“我将它们调整为订单计费的状态。为此,有一个方法get\\u billing\\u state(),它可以完美地发送通知,但在管理员中,我遇到了错误:
未捕获错误:在null上调用成员函数get\\u billing\\u state()。
我假设我可以打电话给账单状态Maualy,以某种方式表达它。
function tm_destinatario_condicionado_wc( $recipient, $order ) {
$estados = array(
\'aguascalientes\' => \'AG\',
\'baja-california\' => \'BC\',
// etc etc ...
);
// Get billing state 2 digits
// This is what is causing the error
$estado_siglas = $order->get_billing_state();
// Get the keys of $estados by the value (wich is the 2 digit billing
// state) to get state name
$estado_slug = array_search( $estado_siglas, $estados );
// Gets the id of a taxonomy related to the state based on the name
// obtained from $estados theres a custon field containing the recipients
$estado_id = get_term_by( \'slug\', $estado_slug, \'estados\' )->term_id;
// get the custom field with the recipients
$emails = get_field( \'sucursal_email\', \'estados_\' . $estado_id );
// Add the custom field content to the recipient
$recipient .= \', \' . $emails;
return $recipient;
}
add_filter( \'woocommerce_email_recipient_new_order\', \'tm_destinatario_condicionado_wc\', 10, 2 );
我很欣赏任何想法。
最合适的回答,由SO网友:LoicTheAztec 整理而成
为了避免该错误,您必须在函数中添加以下内容:
add_filter( \'woocommerce_email_recipient_new_order\', \'tm_destinatario_condicionado_wc\', 10, 2 );
function tm_destinatario_condicionado_wc( $recipient, $order ) {
// To avoid an error in backend
if( ! is_a($order, \'WC_Order\') ) return $recipient;
$estados = array(
\'aguascalientes\' => \'AG\',
\'baja-california\' => \'BC\',
// etc etc ...
);
// Get billing state 2 digits
// This is what is causing the error
$estado_siglas = $order->get_billing_state();
// Get the keys of $estados by the value (wich is the 2 digit billing
// state) to get state name
$estado_slug = array_search( $estado_siglas, $estados );
// Gets the id of a taxonomy related to the state based on the name
// obtained from $estados theres a custon field containing the recipients
$estado_id = get_term_by( \'slug\', $estado_slug, \'estados\' )->term_id;
// get the custom field with the recipients
$emails = get_field( \'sucursal_email\', \'estados_\' . $estado_id );
// Add the custom field content to the recipient
$recipient .= \', \' . $emails;
return $recipient;
}
这将彻底解决您的问题…