1

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.

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…

  • 1
    Thank you very much, i was conditioning it in the same way but just if it was not admin and it was not working but that way solved the problem. 🍻 – A. Hartzet Jan 18 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.