Avada Forums Community Forum WooCommerce: Add additional e-mail recipient (new order) depending on post-code

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • undeutsch
    Participant
    Post count: 2

    Hello!

    I’d like to add additional e-mail recipients depending on the customers post-code. We have different suppliers for different provinces (e.g. 4614 – Supplier 1, 3314 – Supplier 2) and it is necessary to direct the orders to the responsible suppliers. For accounting we need to direct the new orders to the responsible department (currently set in the WooCommerce backend).

    I already tried to use the WooCommerce filter for the e-mail recipient to add the e-mails based on the post-code. For this purpose i set up two arrays and tried to get the shipping post-code (or the billing post-code) to use in this if-function.

    This is the code i tried to use on our website:

    add_filter( 'woocommerce_email_recipient_new_order', 'cond_recipients_email_notifications', 10, 2 );
    function cond_recipients_email_notifications( $recipient, $order ) {
    
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
    
    // TARGET-ZIP-CODE
    $zip_zone1 = array( '4614', '4072', '4615', '4064', '4062', '4611' );
    $zip_zone2 = array( '3314', '3353', '3313', '3312', '3350', '3322', '3354' );
    
    // User ZIP-CODE 
    $user_zip_zone =  $order->get_shipping_postcode();
    if(empty($user_shipping_postcode))
        $user_zip_zone = $order->get_billing_postcode();
    
    // ADD EMAIL IF ZIP-CODE MATCHES TARGET-ZIP-CODE
    if ( in_array( $user_zip_zone, $zip_zone1)) {
                $recipient .= ', e-mail-supplier1@e-mail.at';
        } elseif ( in_array( $user_zip_zone, $zip_zone2) ) {
        $recipient .= ', e-mail-supplier2@e-mail.at';
        }
    
    return $recipient;
    }

    I tried to implement this snippet in the functions.php of my child theme but it doesn’t work as expected. I’m obviously missing something but i have no clue what my mistake could be. Unfortunately i have little experience with php so my understanding of this matter is limited.

    Does anybody know a way to implement the needed function in WooCommerce or how to make this code-snippet work? What is wrong with my code?

    Help is very appreciated!

    kolawoletech
    Participant
    Post count: 1

    Did you ever find a solution to this issue? I know it is a few years late

    undeutsch
    Participant
    Post count: 2

    The problem has now been solved, yes. Here is my new code:

    add_filter( 'woocommerce_email_recipient_new_order', 'cond_recipients_email_notifications', 10, 2 );
    function cond_recipients_email_notifications( $recipient, $order ) {
    
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
    
    // TARGET-ZIP-CODE
    $zip_zone1 = array( 'ZIP-Code', 'ZIP-Code', 'ZIP-Code' );
    $zip_zone2 = array( 'ZIP-Code', 'ZIP-Code', 'ZIP-Code' );
    
    // User ZIP-CODE 
    $user_zip_zone =  $order->get_shipping_postcode();
    if(empty($user_shipping_postcode))
        $user_zip_zone = $order->get_billing_postcode();
    
    // ADD EMAIL IF ZIP-CODE MATCHES TARGET-ZIP-CODE
    if ( in_array( $user_zip_zone, $zip_zone1)) {
                $recipient .= ', EMAIL@DOMAIN.COM';
        } elseif ( in_array( $user_zip_zone, $zip_zone2) ) {
        $recipient .= ', EMAIL@DOMAIN.COM';
        }
    
    return $recipient;
    }
    harrycat
    Participant
    Post count: 1

    Hi there,

    how is your new code different from the original one? did you change anything else for it to work?

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.