Search the site:

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
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

21 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Trevor
Trevor
4 years ago

G’day, this is quite a simple fix and works wonderfully. I am wondering if it would be possible to add the date in the order number too? I tried the following but could not make it work: $new_order_id = $order_id . $date_created . $suffix; or $new_order_id = $order_id . $date_paid . $suffix; If we could make this work, then we would also like to reset the order_id every 24 hours. This means each date would have X number of orders and it would reset itself for the next day. We would like to implement this to stop competitors from checking… Read more »

Rashmi Malpande
Admin
3 years ago
Reply to  Trevor

Hey Trevor,

We found your idea quite interesting and have written a blog on it. Here it is – https://www.tychesoftwares.com/how-to-reset-woocommerce-order-numbers-every-24-hours/

Do let us know your thoughts on it.

Matt
Matt
3 years ago

How can this be changed to add and change the database to use the needed prefixes?

ebson
ebson
3 years ago
Reply to  Vishal Kothari

Hello. Can you tell us something more about that? Because for example, when user try to check status of order using that number he’ll see error. + what if i want to add ass prefix random number? Please help me with that 🙂

Augustin
Augustin
3 years ago
Reply to  Vishal Kothari

Hi,
I’m may be a little bit late but I have the same issue with the prefix in database… I have try with your code, with Custom Order Numbers for WooCommerce, WooCommerce Sequential Order Numbers and YITH WooCommerce Sequential Order Number each time the order id wasn’t with a prefix … I have seen in the database it’s a bighint I can’t modify it…
I’m still looking for a solution or a way

Jovyn
1 year ago
Reply to  Vishal Kothari

Hi vishal, im also facing the same issue, after i added the prefix to my order number, when i try to track the order status, it gives an error. it only recognize the default order id given by woocommerce. im using the aftership tracking plugin as well.

any ideas?

bashayer
bashayer
3 years ago

Hello How can I use the order number in messages sent to customers instead of order id

bashayer
bashayer
3 years ago
Reply to  Vishal Kothari

hi Vishal Kothari
I wont to use order number in woosms plugins my problem
when send sms to customers or admin the message get order id of woocommerce not order number

Ony
Ony
2 years ago
Reply to  Vishal Kothari

Hello, thanks for great content.
I am not anything near an expert! Thanks for the content on how to add prefix in order number but I will like to have a starting order number, and have it gone up say order 234, then next is 235, 236 etc. Because I am moving an already existing site from Hikashop to woocommerce, so I need the order number to start from a certain number.

I will really appreciate your help.

Monika
Monika
2 years ago

Is it possible to connect this code with Woocommerce Groups? e.g if user is at Premium group – order number get prefix PR if user is at group Register other order numer get prefix RG?? I will be grateful for some tips.

Dan
Dan
2 years ago

This works, however, when you export your oders to csv or pdf, all prefixes are gone. Is there a way to keep these in place?

from purchased

Subscribe for more offers

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