Adding Custom Fields to Emails – WooCommerce

Advertisement

Another day another snippet for WooCommerce, this time adding custom order meta fields to order email, yeah this is helpful for site administrator or site owner to check for custom order fields via email without looking ordered item in site dashboard, how handy is that?

Lets get started.

Below is a simple yet functional example of adding Appointment custom meta fields to order email


add_filter( 'woocommerce_email_order_meta_fields', 'woocommerce_email_order_meta_fields_func', 10, 3 );
function woocommerce_email_order_meta_fields_func( $fields, $sent_to_admin, $order ) {

	$fields['appointment_date'] = array(
		'label' => __( 'Appointment Date', 'woocommerce' ),
		'value' => wptexturize( get_post_meta( $order->id, 'appointment_date', true ) )
	);

	$fields['appointment_time'] = array(
		'label' => __( 'Appointment Time', 'woocommerce' ),
		'value' => wptexturize( get_post_meta( $order->id, 'appointment_time', true ) )
	);

	//... more meta fields goes here

	return $fields;
}

How about adding HTML code?

Yup I got you covered, but this time we use action instead of filter, so we’re FREE to add anything we want.


add_action( 'woocommerce_email_after_order_table', 'woocommerce_email_after_order_table_func' );
function woocommerce_email_after_order_table_func( $order ) {
	?>

	<h3>Make an appointment</h3>
	<table>
		<tr>
			<td>Date: </td>
			<td><?php echo wptexturize( get_post_meta( $order->id, 'appointment_date', true ) ); ?></td>
		</tr>
		<tr>
			<td>Time: </td>
			<td><?php echo wptexturize( get_post_meta( $order->id, 'appointment_time', true ) ); ?></td>
		</tr>
		<!--additional custom meta and HTML code goes here-->
	</table>

	<?php
}

Hope that helps, happy coding ^_^

Advertisement