skip to Main Content

Hey I would like to change the reply to emailadress for the new_order emails based on the shipping item.

I have tried:

add_filter( 'woocommerce_email_headers', 'add_headers_replay_to_conditionally', 10, 3 );
function add_headers_replay_to_conditionally( $headers, $email_id, $order ) {
    // Avoiding errors
    if ( ! is_a( $order, 'WC_Order' ) || ! isset( $email_id ) )
        return $headers;

    // The defined emails notifications to customer
    $allowed_email_ids = array('customer_on_hold_order', 'woocommerce_new_order');

    $shipping_items = $order->get_items('shipping');
    $shipping_item  = reset($shipping_items);
    $shipping_name = $shipping_item->get_name();

    if ( $shipping_name == 'Afhalen in Utrecht' ) {
        $headers .= "Reply-to: [email protected]". "rn";
    }
    elseif ( $shipping_name == 'Bezorgen vanuit Utrecht' ) {
        $headers .= "Reply-to: [email protected]". "rn";
    }
    elseif( $shipping_name == 'Afhalen in Amsterdam' ) {
        $headers .= "Reply-to: [email protected]". "rn";
    }
    elseif( $shipping_name == 'Bezorgen vanuit Amsterdam' ) {
        $headers .= "Reply-to: [email protected]". "rn";
    }
    elseif( $shipping_name == 'Bezorgen in Amersfoort (gratis vanaf €50 ex btw bestelwaarde)' ) {
        $headers .= "Reply-to: [email protected]". "rn";
    }
    else {
        $headers .= "Reply-to: [email protected]". "rn";
    }   
    
    return $headers;
}

But I get the "new" email adres (based on shipping item) + the standaard email adres in the reply to field.. I would like to have only the new one.

2

Answers


  1. Chosen as BEST ANSWER

    After some investigation this works for me:

    function add_headers_replay_to_conditionally( $headers, $email_id, $order ) {
        // Avoiding errors
        if ( ! is_a( $order, 'WC_Order' ) || ! isset( $email_id ) )
            return $headers;
    
        // The defined emails notifications to customer
        $allowed_email_ids = array('customer_on_hold_order');
    
        // Only for specific email notifications to the customer
        $utrecht = '[email protected]';
        $amsterdam = '[email protected]';
        $amersfoort = '[email protected]';
        $info = '[email protected]';
    
        $shipping_items = $order->get_items('shipping');
        $shipping_item  = reset($shipping_items);
        $shipping_name = $shipping_item->get_name();
        
        if( in_array( $email_id, $allowed_email_ids ) ) {
    
            if ( $shipping_name == 'Afhalen in Utrecht' ) {
                $headers = "Reply-to: [email protected]". "rn";
            }
            elseif ( $shipping_name == 'Bezorgen vanuit Utrecht' ) {
                $headers = "Reply-to: [email protected]". "rn";
            }
            elseif( $shipping_name == 'Afhalen in Amsterdam' ) {
                $headers = "Reply-to: [email protected]". "rn";
            }
            elseif( $shipping_name == 'Bezorgen vanuit Amsterdam' ) {
                $headers = "Reply-to: [email protected]". "rn";
            }
            elseif( $shipping_name == 'Bezorgen in Amersfoort' ) {
                $headers = "Reply-to: [email protected]". "rn";
            }   
        }   
        return $headers;
    }
    

  2. Try this approach

    add_filter('wp_mail', 'wdm_sent_from_email', 99, 1);
    function wdm_sent_from_email( $args ) {
    $order = wc_get_order( $order_id );
    $reply_email = "Reply-To: [email protected]";
    foreach ( $order->get_items('shipping') as $item_id => $item ) {
       $order_item_name             = $item->get_name();
        $order_item_type             = $item->get_type();
        $shipping_method_title       = $item->get_method_title();
        $shipping_method_id          = $item->get_method_id(); 
        $shipping_method_instance_id = $item->get_instance_id(); 
        $shipping_method_total       = $item->get_total();
        $shipping_method_total_tax   = $item->get_total_tax();
        $shipping_method_taxes       = $item->get_taxes();
        if($shipping_method_id == "fedex"){
            $reply_email = "Reply-To: [email protected]";
        }
    }
    $args['headers'] .= $reply_email . "rn";
    return $args;
    

    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search