WooCommerce手动获取计费状态

时间:2020-01-17 作者:AHrtzt

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 );
我很欣赏任何想法。

1 个回复
最合适的回答,由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;
}
这将彻底解决您的问题…

相关推荐

为内置钩子调用do_action和Apply_Filters是否安全?

我正在开发一个插件,它需要复制一些内置的WordPress逻辑。(此逻辑不能用任何内置方法调用,也不能独立连接到。)在这个动作序列中,WordPress的正常行为是调用动作挂钩(do_action(\'wp_login\', ...)) 和过滤器挂钩(apply_filters(\'login_redirect\', ...)).如果在对应于在Core中调用它们的时间点调用它们,那么直接从我的插件调用这些内置钩子是否安全(并且是可以接受的做法)?或者,其他与此相关的开发人员期望在非常特定的时间执行操作的风