0

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: utrecht@x.nl". "\r\n";
    }
    elseif ( $shipping_name == 'Bezorgen vanuit Utrecht' ) {
        $headers .= "Reply-to: utrecht@x.nl". "\r\n";
    }
    elseif( $shipping_name == 'Afhalen in Amsterdam' ) {
        $headers .= "Reply-to: amsterdam@x.nl". "\r\n";
    }
    elseif( $shipping_name == 'Bezorgen vanuit Amsterdam' ) {
        $headers .= "Reply-to: amsterdam@x.nl". "\r\n";
    }
    elseif( $shipping_name == 'Bezorgen in Amersfoort (gratis vanaf €50 ex btw bestelwaarde)' ) {
        $headers .= "Reply-to: amersfoort@x.nl". "\r\n";
    }
    else {
        $headers .= "Reply-to: info@x.nl". "\r\n";
    }   
    
    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.

rutvanelk
  • 35
  • 6
  • 1
    What is the error that you are getting? – Dula Nov 16 '21 at 10:31
  • Super wierd but the error is gone... I do get 2 emails now in the reply to.. I will change the question – rutvanelk Nov 16 '21 at 12:10
  • 1
    @rutvanelk _"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."_ - `.=` adds something to an existing string, while `=` overwrites the existing value. See: [How can I combine two strings together in PHP?](https://stackoverflow.com/questions/8336858/how-can-i-combine-two-strings-together-in-php) – 7uc1f3r Nov 16 '21 at 12:45
  • Thanks that solved the problem! – rutvanelk Nov 18 '21 at 09:36

2 Answers2

0

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: example@gmail.com";
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: fedex@gmail.com";
    }
}
$args['headers'] .= $reply_email . "\r\n";
return $args;

}

gaurav sharma
  • 534
  • 2
  • 6
0

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 = 'utrecht@x.nl';
    $amsterdam = 'amsterdam@x.nl';
    $amersfoort = 'amersfoort@x.nl';
    $info = 'info@x.nl';

    $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: utrecht@x.nl". "\r\n";
        }
        elseif ( $shipping_name == 'Bezorgen vanuit Utrecht' ) {
            $headers = "Reply-to: utrecht@x.nl". "\r\n";
        }
        elseif( $shipping_name == 'Afhalen in Amsterdam' ) {
            $headers = "Reply-to: amsterdam@x.nl". "\r\n";
        }
        elseif( $shipping_name == 'Bezorgen vanuit Amsterdam' ) {
            $headers = "Reply-to: amsterdam@x.nl". "\r\n";
        }
        elseif( $shipping_name == 'Bezorgen in Amersfoort' ) {
            $headers = "Reply-to: amersfoort@x.nl". "\r\n";
        }   
    }   
    return $headers;
}
rutvanelk
  • 35
  • 6