How to Change Order Numbers in WooCommerce

In this tutorial, I am going to share with you how you can change order numbers everywhere on your WooCommerce store; in particular, we will try to add custom prefixes and suffixes to it and even to use some variable parameters as a part of the order number template.

Understanding Database Structure

Before we actually try to go ahead and change order numbers, it is better to have a clear understanding of how it is working under the hood.

For better or for worse, an order number is actually a value of the ID column in the WordPress database.

  • ID in wp_posts – for legacy WooCommerce orders,
  • id in wp_wc_orders – for HPOS orders.

Just take a look at this screenshot:

Order number in wp_wc_orders WooCommerce table.
Yes, it is an order number.

Why WooCommerce order numbers aren’t in sequential order?

The sequential order numbers feature is quite an important one, why WooCommerce doesn’t have it by default?

Let’s figure it out.

The ID value in the wp_posts table is auto-incremented, and, as you probably know, the Posts, Pages, Attachments, Products, and Orders share the same table here. It means that the order numbers of two orders wouldn’t be in sequential order if, before the second order was created, there was also a new post draft created or a new file uploaded in the WordPress admin.

The question is – why is it also happening for HPOS orders when WooCommerce orders have a separate database table, wp_wc_orders?

Because of order placeholders, of course!

Order placeholders basically consist of a single row in the wp_posts table in order to preserve the order number in case you used the high-performance order storage for a while and then decided to switch back to legacy orders for some reason.

Order data storage feature in WooCommerce

In my other tutorial, I have also discussed how to create sequential order numbers.

Changing Order Numbers (Examples)

Now we know the theory part, and it is time for practical examples. In all the examples below, in order to change order numbers, we’re going to use the same filter hook, which is the woocommerce_order_number hook.

The changes are going to be applied everywhere, even in WooCommerce emails.

But, of course, the database values will remain unchanged in any case.

Add Prefixes and Suffixes to Order Numbers

This part is super-easy; all we need to do is use the code snippet below on our WooCommerce store.

add_filter( 'woocommerce_order_number', 'rudr_change_order_number' );

function rudr_change_order_number( $order_number ) {
	
	$prefix = 'RUDR/';
 	$suffix = '/NL';
	
	return $prefix . $order_number . $suffix;

}

If you don’t know where to insert this code, I recommend you check this guide.

The result:

WooCommerce change order numbers – add prefix and suffix
You can see on the screenshot that we added both prefixes and suffixes to order numbers.

Using Custom Order Number Template

A “custom order number template” is when you use a specific dynamic value as an order number prefix or suffix. For example, it could be a country code or a customer ID. You can see this functionality in its full potential in my plugin, Multisite Order Sync for WooCommerce.

This part is going to be more interesting, mainly because the woocommerce_order_number hook allows you to pass a second parameter into its callback function, which is actually a WC_Order object. This means that you can use literally any information from the order as a part of its order number.

add_filter( 'woocommerce_order_number', 'rudr_order_number_template', 25, 2 );

function rudr_order_number_template( $order_number, $order ) {
	
    $country_code = $order->get_billing_country();
    return "{$order_number}/{$country_code}";

}

Here you go:

WooCommerce order numbers with country codes

Change Order Numbers for Specific Orders Only

For example, let’s say that we want to add a “PP” prefix for all orders that were paid with PayPal. Easy enough!

add_filter( 'woocommerce_order_number', 'rudr_order_number_paypal', 25, 2 );

function rudr_order_number_paypal( $order_number, $order ) {

	if( 'paypal' === $order->get_payment_method() ) {
		$order_number = "PP-{$order_number}";
	}

	return $order_number;

}
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 15 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X