Within WooCommerce I created a custom field for a email address. Now I need WooCommerce to send completed order emails only to that custom field address if there is a value in that field.
I figured out how to send the completed order email to a custom email adres as BCC but now I'm trying to have the emails send to a custom field email adres without the standard email address.
Custom field email as BCC (working code):
//* Send invoice to accounts payable on completed orders *//
add_filter( 'woocommerce_email_headers',
'wcmos_email_cc_alternative', 10, 3);
function wcmos_email_cc_alternative( $recipient, $email_id, $order )
{
if ($email_id == 'customer_completed_order') {
$alternative_email = $order->get_meta( 'purchase_order_email',
true );
if ( $alternative_email ) {
$recipient = '$alternative_email';
}
}
return $recipient;
}
Custom field email as main reciepient (failing code):
add_filter( 'woocommerce_email_recipient_customer_completed_order',
'wcmos_email_cc_alternative', 10, 3);
function wcmos_email_cc_alternative( $recipient, $email_id, $item_id
) {
if ($email_id == 'customer_completed_order') {
$alternative_email = $order->get_meta( 'purchase_order_email',
true );
$po_email = wc_get_order_item_meta( $item_id,
'purchase_order_email - '.$enroll_num, true );
if ( $alternative_email ) {
$recipient = ', $po_email';
}
}
return $recipient;
}