I am trying to change the sender email for all emails related to two products in WooCommerce, but not for all the others.
I have the code below to change the sender email, but I am not sure how to make it work for only those two products (by product id or category).
function change_sender_email( $original_email_address ) {
return 'admin@example.com';
}
add_filter( 'wp_mail_from', 'change_sender_email' );
Could I somehow use the filter 'woocommerce_email_recipient_customer_completed_order
'?
I know how to use that to conditionally change the recipient of the email, but I couldn't get it to work to change the sender email.
You can use: woocommerce_email_from_address
// Change email sender address
function my_email_from_address( $from_email, $wc_email ) {
// Get the WC_Order object instance
$order = $wc_email->object;
// Get items
$items = $order->get_items();
// Loop through
foreach ( $items as $item ) {
// Get product ID
$product_id = $item->get_product_id();
// Compare
if ( $product_id == 30 ) {
$from_email = 'my.email1@stackoverflow.com';
} elseif ( $product_id == 32 ) {
$from_email = 'my.email2@stackoverflow.com';
}
}
return $from_email;
}
add_filter( 'woocommerce_email_from_address', 'my_email_from_address', 20, 2 );