Posted on

Adding recipients to quote (customer and admin emails) dynamically

// Add recipients to new quotes sent to admin by adding the following to functions.php
add_filter( ‘woocommerce_email_recipient_new_rfq’, ‘new_rfq_conditional_email_recipient’, 10, 2 );

function new_rfq_conditional_email_recipient( $recipient, $order ) {

$order_id=$order->get_id() ;

$some_field_name = get_post_meta( $order_id, “some_field_name”, true );

//add recipient based on order metadata
if ($some_field_name == “abc”) {
$recipient .= “, receipt_abc@somemail.com”;
}
elseif ($some_field_name == “xyz”){
$recipient .= “, receipt_xyz@somemail . com”;
}
//add recipient based on products in the order
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item_id => $item ) {
$product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
if ( $product_id === 1 ) {
$recipient .= “, receiptprod1@somemail.com”;
}
if ( $product_id === 2 ) {
$recipient .= “, receiptprod2@somemail.com”;
}
}

return $recipient;
}

//You can repeat the same pattern for the following emails:
//email sent to customer
add_filter( ‘woocommerce_email_recipient_customer_rfq’, ‘customer_rfq_conditional_email_recipient’, 10, 2 );
function customer_rfq_conditional_email_recipient( $recipient, $order ) {
//follow the same pattern as above
}

//proposal email sent to customer
add_filter( ‘woocommerce_email_recipient_customer_rfq_sent’, ‘customer_rfq_sent_conditional_email_recipient’, 10, 2 );
function customer_rfq_sent_conditional_email_recipient( $recipient, $order ) {
//follow the same pattern as above
}
//email sent to admin for notes
add_filter( ‘woocommerce_email_recipient_admin_note’, ‘admin_note_conditional_email_recipient’, 10, 2 );
function admin_note_conditional_email_recipient( $recipient, $order ) {
//follow the same pattern as above
}