Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to add a prefix or suffix to WooCommerce Order number

This post describes how you can add prefix or suffix to WooCommerce Order number. By default, it’s only a numeric value.

Shown below is your WooCommerce Orders page.

Add a prefix or suffix to WooCommerce Order number - WooCommerce Orders Page

In WooCommerce, a new order is generated from the wc_create_order(), which is present in includes/wc-core-functions.php file. The call to wc_create_order() is initiated from the create_order() in includes/class-wc-checkout.php file.

The order number that is shown above, 10, is the value of the ID field of the wp_posts table. That is because WooCommerce adds all it’s orders to the wp_posts table with post type as shop_order. So whatever is the ID field’s value of the last record in that table, it’s incremented by 1 & that becomes your order number.

Some businesses require that order numbers should be shown differently by adding a prefix & suffix or they should start from a number which is greater than their current highest order’s number or it could be any such variation.

If you are not well versed with coding now you can easily add a prefix or a suffix to your WooCommerce orders without doing any code changes using our Custom Order Numbers for WooCommerce plugin, you can even numerate WooCommerce Orders as desired either sequentially or randomly:

 

WooCommerce provides a filter woocommerce_order_number. This filter is applied in the get_order_number() in the file includes/wc-abstract-order.php.

This is where you can attach your function & create a prefix or suffix for your WooCommerce order number. I added the below code in my theme’s functions.php file:

add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );

function change_woocommerce_order_number( $order_id ) {
    $prefix       = 'VK/';
    $suffix       = '/TS';
    $new_order_id = $prefix . $order_id . $suffix;
    return $new_order_id;
}

The new order number VK/10/TS will get displayed at all places, including your order details page & email notifications. It doesn’t change anything in the database though.

Add a prefix or suffix to WooCommerce Order number - WooCommerce Orders page

The filter woocommerce_order_number is only useful to add prefix or suffix to your WooCommerce order numbers. It cannot be used to set a starting number for your orders.

WooCommerce provides another filter woocommerce_new_order_data that allows you to modify the order data just before it is inserted in the wp_posts table using the wp_insert_post(). We will look at that in another post.

Another use of this filter is to add the date of creation of the order and create custom order numbers such that it would reset for the next day. You can read about it here – How to reset WooCommerce order numbers every 24 hours.

Browse more in: WooCommerce Tutorials, WooCommerce How Tos

Share It:

Subscribe
Notify of
21 Comments
Newest
Oldest
Inline Feedbacks
View all comments
Zdenek
3 years ago

Hey, works great!
Please is there any way to modify reference order number according to custom order number, like they would be the same?
Thank you

3 years ago

Hey Vishal,
Thank you – this is great and I used it to add the suffix “-WS” to the end of order numbers if the current user’s role was “administrator”. It worked great except – when I look at my Woocommerce orders, now *all* of the order numbers have a “-WS” applied to them.
Is this the expected outcome, or did I miss something/do something wrong?

3 years ago
Reply to  Aram

nvm 🙂 I figured it out. I had to get the user role of the customer, not the logged in user.

3 years ago

Hello, i put the snippets and work but why when i want to add new order from admin it returns Uncaught Error: Call to a member function format() on null?

5 years ago

Great! this works perfectly. Order number is printed with PRE/SUFFIX in all our systems.

Joy
5 years ago

Hi, at the end of this post it is stated “WooCommerce provides another filter woocommerce_new_order_data that allows you to modify the order data just before it is inserted in the wp_posts table using the wp_insert_post(). We will look at that in another post.” However, this post was written almost 2 years ago and I can’t seem to find any such post anywhere.. Could you expand on this topic?

Emilio G. Busnelli
4 years ago
Reply to  Joy

I too am wondering where this post may be…

21
0
Would love your thoughts, please comment.x
()
x