1
votes

All order email sending to CUSTOMER from default woocommerce email address, for example, [email protected] and this is OK, but I want add a reply email address for these emails, so user able to reply this custom address, such as [email protected].

Also I want set this reply email address based on $order shipping method. If shipping method is 'local_pickup_plus' set reply address to [email protected], else set [email protected].

I figure out, I can modify headers with woocommerce_email_headers filter, but only for email sent to the admin.

add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 3);
function mycustom_headers_filter_function( $headers, $object, $order ) {
    if ($object == 'new_order') {
        $headers .= 'Reply-to: [email protected]';
    }

    return $headers;
}

How can I set this for customer emails?

1

1 Answers

1
votes

The following code will allow you to add a different reply email conditionally based on shipping methods for customer email notifications:

// Utility function to get the shipping method Id from order object
function wc_get_shipping_method_id( $order ){
    foreach ( $order->get_shipping_methods() as $shipping_method ) {
        return $shipping_method->get_method_id();
    }
}

// Add coditionally a "reply to" based on shipping methods IDs for specific email notifications
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', 'customer_processing_order', 'customer_completed_order');

    // Only for specific email notifications to the customer
    if( in_array( $email_id, $allowed_email_ids ) ) {
        // Local Pickup Plus shipping method
        if( wc_get_shipping_method_id( $order ) === 'local_pickup_plus' ){
            $headers .= "Reply-to: [email protected]". "\r\n"; // Email adress 1
        } 
        // Other shipping methods
        else {
            $headers .= "Reply-to: [email protected]". "\r\n"; // Email adress 2
        }
    }

    return $headers;
}

Code goes in function.php file of your active child theme (active theme). Tested and works.