Enhancing Order Email: Adding customer email and phone in the order email of WooCommerce

How to add customer email and phone in order email of WooCommerce

In the world of online selling, it’s important to make things easy for both sellers and buyers. One way to do that is by upgrading how you handle orders. This guide will show you how to make things smoother by adding customer email and phone in the order email of WooCommerce. Let’s dive into simple steps to make your order management better.

Introduction

Sometimes a store manager finds it a bit difficult to see the customers’ contact information from the orders of WooCommerce. It’s a bit lengthy process since he has to open order management, and then order details to view the information. To ease the process of finding customer information, we can add the customer email and phone in the order email. This order email is sent to the store manager when a customer places an order on the website.

Knowing the default Order Email

To make our changes, we need to know where and how to fit customer details in the Order email. By default, the WooCommerce order email does not contain customers’ details. The default order emails look like below:

woocommerce order email without customer email and phone in the order email
WooCommerce order email without customer contact details

Having a customer email and phone in the order email provides ease to store managers. The store manager can contact their customers easily in this case since they are getting contact details in the email itself.

The expected output: customer email and phone in the order email

After adding the above snippet, the order email will look like the below:

woocommerce order email with customer email and phone in the order email
WooCommerce order email with customer contact details

Now, the store manager can easily get customer email and phone in the order email without logging into the wp-admin portal.

Implementation no.1: customer email and phone in the order email

In this article, we will be going to add a customer email and phone in the order email of WooCommerce. We need to write a hook that will modify the order email and will add customer contact information. Open functions.php of your child theme and add the following snippet:

<?php
// WooCommerce admin order email - add customer information
  function w3_order_email_customer_details( $order ) {
    $phone = $order->get_billing_phone();
    $email = $order->get_billing_email();

    echo '<h2>'._e( 'Customer details', 'woocommerce' ).'</h2>
         <ul>';
          if($phone) {
            echo '<li>
                <strong>'.wp_kses_post( 'Email address').':</strong> 
                <span class="text">'.wp_kses_post( $email ).'</span>
              </li>';
          }
          if($phone) {
            echo '<li>
                <strong>'.wp_kses_post( 'Phone').':</strong> 
                <span class="text">'.wp_kses_post( $phone ).'</span>
              </li>';
          }
      echo '</ul>';
  };

// add action
  add_action( 'woocommerce_email_customer_details', 'w3_order_email_customer_details', 10, 4 );
?>

The above action will work for WooCommerce version 2.5 or above.

Last but not least, in the above snippet, we have used the _e and wp_kses_post functions.

_e: used to display translated text if your website is multilingual.

wp_kses_post: sanitizes content for allowed HTML tags for post content.

Implementation no.2: by hooks method

As with the WordPress admin Order table customization, the functions.php file is our go-to place for adding functionality to WooCommerce order emails.

Creating a Function to Retrieve Customer Info

Let’s start by writing a function that retrieves customer email and phone information associated with each order.

// Function to get customer email and phone
function get_customer_contact_info($order_id) {
    $customer_data = array(
        'email' => get_post_meta($order_id, '_billing_email', true),
        'phone' => get_post_meta($order_id, '_billing_phone', true),
    );
    return $customer_data;
}

Modifying WooCommerce Email Content

Now, we’ll modify the content of WooCommerce order emails to include customer email and phone details.

// Function to add customer details to order emails
function add_customer_details_to_order_email($order, $sent_to_admin, $plain_text, $email) {
    $customer_info = get_customer_contact_info($order->get_id());

    // Add customer details to the email content
    echo "\n\nCustomer Email: " . esc_html($customer_info['email']);
    echo "\nCustomer Phone: " . esc_html($customer_info['phone']);
}
add_action('woocommerce_email_customer_details', 'add_customer_details_to_order_email', 20, 4);

Testing and Checking: Making Sure It Works

Before you make these changes live, it’s important to test them out. We’ll check to make sure customer email and phone in the order email details show up smoothly without causing any issues.

Conclusion: Better Order Management with Customer Details

Well done! You’ve successfully improved your WordPress admin Order email by adding customer email and phone details. This small change makes your order system more efficient and user-friendly. By adding customer details to the Order email, you’re not just handling orders; you’re creating a friendlier and more personalized online shopping experience. Enjoy making these improvements!

In the next article, we will see how to add the customers’ email and phone in the Order table of WordPress admin.

Related Posts