woocommercehook-woocommerceemail-notificationsuser-data

Use WooCommerce account name and email instead of billing name and email for invoices and email notifications


I want to use the WordPress account User Name on the customer emails and admin order notifications instead of the Billing Name.

Our use case is that employees are enrolling for an online course. They create an account on our site with their own name and email, but their employer is paying for the course, so the Billing info is that of the employer. I need the admin order notification to tell me the name of the person who's actually taking the course.

Here's a WP support thread that perfectly articulates the situation: https://wordpress.org/support/topic/order-emails-being-addressed-to-billing-name-instead-of-account-name/

The person who replied linked to a StackOverflow thread that doesn't answer the question or even come at the question from the right direction.

I've spent months googling about this! I've gone down so many rabbit holes here on SO and on blogs, and I've seen other people asking about this, but I haven't found any solution. I've identified that the fields I want to use are first_name and last_name instead of billing_first_name and billing_last_name. I'm collecting this user info in a separate section at checkout, in addition to the billing info. I've looked at \plugins\woocommerce\templates\emails\admin-new-order, but I don't recognize any obvious place where I can just replace billing_last_name with last_name.

Can anyone help with a snippet that will achieve this, or show me how to modify the email templates?


Solution

  • Note that when an order is placed, the account billing email is also changed, so user billing email can't be used.

    Then you need to use the WordPress user_email that can be found under My Account > Account details (or in WordPress User edit page on main email account field).

    Based on this previous answer, try the following:

    // Replace the billing email for notifications with the main account User email
    add_action( 'init' , function() {
        // Here below define all the targeted customer notification email IDs 
        $email_ids = [
            'customer_on_hold_order', 
            'customer_processing_order', 
            'customer_completed_order',
            'customer_refunded_order',
            'customer_partially_refunded_order',
            'customer_invoice',
            'customer_note',
        ];
    
        foreach( $email_ids as $email_id ) {
            add_filter( "woocommerce_email_recipient_{$email_id}", 'replacement_email_recipient_for_customer', 20, 2 );
        }
    }, 20 );
    
    function replacement_email_recipient_for_customer( $recipient, $order ) {
        $user = $order->get_user();
    
        if ( $user->user_email ) {
            return $user->user_email;
        }
        return $recipient;
    }
    
    // Change the first name displayed in the email notification itself
    add_filter( 'woocommerce_order_get_billing_first_name', function( $billing_first_name, $order ) {
        // Not in front end
        if ( is_wc_endpoint_url() ) {
            return $billing_first_name;
        }
    
        $user = $order->get_user();
    
        if ( $user->user_email && $user->first_name ) {
            return $user->first_name;
        }
        return $billing_first_name;
    }, 20, 2);
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

    For email Ids, see: Target a specific email notification with the email id in WooCommerce