Search code examples
phpwordpresswoocommercemetadataemail-notifications

Replacement recipient from order custom field on WooCommerce emails


I created a new field in the checkout part using the a checkout field plugin. This field allows customer to specify a third email address outside of the billing or shipping address. I want to send the order confirmation only to this address and not to the billing part.

After several searches, I used Send Woocommerce Order to email address listed on product page answer code, replacing the order item meta key 'email' by 'billing_email_guest' like:

$email = wc_get_order_item_meta( $item_id, 'billing_email_guest', true );

But it doesn't work. What I am doing wrong?


Solution

  • You are not using the correct way as this is about custom order meta data, but not order item meta data.

    Try the following instead:

    add_filter( 'woocommerce_email_recipient_customer_on_hold_order', 'additional_customer_email_recipient', 10, 2 ); // On hold Order
    add_filter( 'woocommerce_email_recipient_customer_processing_order', 'additional_customer_email_recipient', 10, 2 ); // Processing Order
    add_filter( 'woocommerce_email_recipient_customer_completed_order', 'additional_customer_email_recipient', 10, 2 ); // Completed Order
    function additional_customer_email_recipient( $recipient, $order ) {
        if ( ! is_a( $order, 'WC_Order' ) ) 
            return $recipient;
    
        $billing_email_guest = $order->get_meta('billing_email_guest');
        if( ! empty($billing_email_guest) ) {
            return $billing_email_guest;
        }
    
        return $recipient;
    }
    

    Code goes in functions.php file of your active child theme (or active theme). It should works.