Print invoice number in email

If you need to the invoice number in the email to which you attach an invoice, you will need to make sure an invoice number is actually generated – typically WooCommerce first creates the email text and then creates the PDF, in which case the invoice number is not available yet. You can trigger this with a filter. Here’s an example filter that will print the invoice number before the order table in the email (using the WooCommerce woocommerce_email_before_order_table hook).

If you haven’t worked with code snippets (actions/filters) or functions.php before, read this guide: How to use filters

add_action( 'woocommerce_email_before_order_table', 'woocommerce_email_print_invoice_number', 10, 4 );
function woocommerce_email_print_invoice_number( $order, $sent_to_admin, $plain_text, $email ) {
	// limit to specific order statuses
	$allowed_statuses = array( 'processing', 'completed' );
	if (!in_array($order->get_status(), $allowed_statuses)) {
		return;
	}

	// create/initialize invoice
	$invoice = wcpdf_get_invoice( $order, true ); // true makes sets 'init' which makes sure an invoice number is created;
	$invoice_number = $invoice->get_number();
	// invoice number is an object, but has a __toString magic method so if you use it as a string it will print the formatted number.
	// You can also explicitly get the formatted or plain number:
	$formatted_invoice_number = $invoice_number->get_formatted();
	$plain_invoice_number = $invoice_number->get_plain();

	echo "Invoice number: {$formatted_invoice_number}";
}

Other actions/filters you can use can be found here: https://docs.woocommerce.com/wc-apidocs/hook-docs.html
(search for ‘woocommerce_email’). Do check that the filter or action you are using does have the $order object loaded!

If you’d rather insert the invoice number directly in an email template, you only need part of the above snippet:

<?php
// create/initialize invoice
$invoice = wcpdf_get_invoice( $order, true ); // true makes sets 'init' which makes sure an invoice number is created;
$invoice_number = $invoice->get_number();
// invoice number is an object, but has a __toString magic method so if you use it as a string it will print the formatted number.
// You can also explicitly get the formatted or plain number:
$formatted_invoice_number = $invoice_number->get_formatted();
$plain_invoice_number = $invoice_number->get_plain();

echo "Invoice number: {$formatted_invoice_number}";
?>