Augment order emails with custom information

In this small guide we’re going to add a custom field in the WooCommerce order administration page to allow store owners to include any extra information they want in emails sent to the client regarding their orders.

Install and activate a child theme

The first step on our process here is to create and install a child theme. If you are using one of our themes you can easily grab its child theme from our downloads section. If not, you can read our beginner’s guide on child themes to create your own. This step is essential in order to preserve our changes throughout theme updates.

All code below will be placed in our child theme’s functions.php file. If the file is empty the code will go just below the opening <?php tag, otherwise, if it has contents, the code should be placed at the end of the file, before the closing ?> PHP tag if it exists.

Adding the custom field

In our example we will be adding a custom field to the admin order page which we will use to include a tracking code for the client to watch their package’s delivery route. We have added custom fields to WooCommerce products in many of our guides and the process is pretty similar here as well.

add_action( 'woocommerce_admin_order_data_after_order_details', 'cssigniter_add_admin_order_field' );
function cssigniter_add_admin_order_field( $order ){  
	woocommerce_wp_text_input(
		array(
			'id'            => 'tracking_code',
			'label'         => __( 'Tracking code', 'your-text-domain' ),
			'value'         => get_post_meta( $order->get_id(), 'tracking_code', true ),
			'wrapper_class' => 'form-field-wide',
		)
	);
}

add_action( 'woocommerce_process_shop_order_meta', 'cssigniter_save_admin_order_field' );
function cssigniter_save_admin_order_field( $ord_id ) {
	update_post_meta( $ord_id, 'tracking_code', wc_clean( $_POST['tracking_code'] ) );
}

We use the woocommerce_admin_order_data_after_order_details hook to add our field in and the woocommerce_process_shop_order_meta one in order to save any data entered to it.

Our new field appears in the order details page as expected.

Include the new custom field to emails

Now let’s include the custom field to emails sent to the client.

add_filter( 'woocommerce_email_order_meta_fields', 'cssigniter_custom_order_email_fields', 10, 3 );
function cssigniter_custom_order_email_fields( $fields, $sent_to_admin, $order ) {
    $fields['meta_key'] = array(
        'label' => __( 'Tracking Code', 'your-text-domain' ),
        'value' => get_post_meta( $order->id, 'tracking_code', true ),
    );
    return $fields;
}

We use the woocommerce_email_order_meta_fields hook to filter the email order meta fields and add our own. We just need a label for our field and its value. The value is pulled from the order’s meta and we need to make sure the meta key used (tracking_code in this case) matches the id used in the custom field added to the order admin page earlier. That’s it, now the new field will appear on any email sent to the customer.

Wrapping up

Using the information from this guide we can easily add any field we want to the admin order page in and create more informative order related emails for our clients. Did you find this guide helpful? Let us know in the comments below.

Related Articles

2 comments

  1. scopieg says:

    I am a subscriber to your themes and was looking through your blog and this post caught my eye because I have spent hours searching for a way to add Purchase Notes to each Variation in WC. I can’t believe I’m the only one that’s ever asked for this. It would work just like Product Purchase Notes that only print on the order email when they purchase a specific product, the Variation Purchase Note would only print when they purchased that specific variation.

    It sounds like a big ask, but do you have any ideas?

    1. Nik says:

      Hello.
      Unfortunately this is not something we can handle. This is functionality missing from WooCommerce. You could try submitting a feature request on WooCommerce’s issue tracker here https://github.com/woocommerce/woocommerce/issues and perhaps the team will consider it if it gathers attention from the community.

Leave a Reply

Your email address will not be published.