Get customers name in confirmation email

I am working with Woocommerce and I am about to make the order confirmation email.

It want the mail to say:

Read More

“Hi [customers name]”

How do you get Woocommerce to print the customers name?

Related posts

Leave a Reply

4 comments

  1. You need the order object, so depending on what hook you are using it should be there. Try something like this:

    add_action('woocommerce_order_status_completed','my_woo_email');
    function my_woo_email($order_id){
    
               $order = new WC_Order( $order_id );
               $to = $order->billing_email;
               $subject = 'this is my subject';
               $message = 'Hi '.$order->billing_first_name.' '.$order->billing_email;$order->billing_last_name.', thanks for the order!';
    
                    woocommerce_mail( $to, $subject, $message, $headers = "Content-Type: text/htmlrn", $attachments = "" )
    
    }
    

    This is untested but should get you started

  2. As of WooCommerce 3.x the $order object’s properties should not be accessed directly as suggested in the previous answers. The proper way is now:

    echo 'Hi ' . $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
    
  3. Here’s my starter example of getting a bunch of the WooCommerce customer info inside a WP_Query loop:

    $args = array(
        'post_status'    => 'any',
        'post_type'      => 'shop_order',
        'posts_per_page' => -1,
        'order'          => 'ASC'
    );
    
    $query = new WP_Query( $args );
    
    while ( $query->have_posts() ) {
    
        $query->the_post();
    
        $order    = new WC_Order( $query->post->ID );
    
        echo $order->billing_address_1;
        echo $order->billing_address_2;
        echo $order->billing_city;
        echo $order->billing_company;
        echo $order->billing_country;
        echo $order->billing_email;
        echo $order->billing_first_name;
        echo $order->billing_last_name;
        echo $order->billing_phone;
        echo $order->billing_postcode;
        echo $order->billing_state;
        echo $order->cart_discount;
        echo $order->cart_discount_tax;
        echo $order->customer_ip_address;
        echo $order->customer_user;
        echo $order->customer_user_agent;
        echo $order->order_currency;
        echo $order->order_discount;
        echo $order->order_key;
        echo $order->order_shipping;
        echo $order->order_shipping_tax;
        echo $order->order_tax;
        echo $order->order_total;
        echo $order->payment_method;
        echo $order->payment_method_title;
        echo $order->shipping_address_1;
        echo $order->shipping_address_2;
        echo $order->shipping_city;
        echo $order->shipping_company;
        echo $order->shipping_country;
        echo $order->shipping_first_name;
        echo $order->shipping_last_name;
        echo $order->shipping_method_title;
        echo $order->shipping_postcode;
        echo $order->shipping_state;
    }
    wp_reset_postdata();
    

    Hopefully this will be helpful for someone else.

  4. you can get customer data from $order object, something like this.

    $customer = get_userdata( $order->get_customer_id() );
    $name = $customer->display_name;