最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

filters - Woocommerce get billing state manually

programmeradmin3浏览0评论

Im modifying the receipts for the email admin notifications with woocommerce filter "woocommerce_email_recipient_new_order" Im conditioning them to the state wich the order is billed. For this there is the method get_billing_state(), this works perfect sending the notifications but in the admin i got the error:

Uncaught Error: Call to a member function get_billing_state() on null.

Im assuming that I can call the billing state maually to put it in some way.

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

I apreciate any ideas.

Im modifying the receipts for the email admin notifications with woocommerce filter "woocommerce_email_recipient_new_order" Im conditioning them to the state wich the order is billed. For this there is the method get_billing_state(), this works perfect sending the notifications but in the admin i got the error:

Uncaught Error: Call to a member function get_billing_state() on null.

Im assuming that I can call the billing state maually to put it in some way.

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

I apreciate any ideas.

Share Improve this question asked Jan 17, 2020 at 18:29 AHrtztAHrtzt 134 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

To avoid that error, you have to add something in your function like:

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

This should solve your issue definitively…

发布评论

评论列表(0)

  1. 暂无评论