Search the site:

Home Blog How to add a prefix or suffix to WooCommerce Order number

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 How Tos, WooCommerce Tutorials

15
Leave a Reply

avatar
7 Comment threads
8 Thread replies
11 Followers
 
Most reacted comment
Hottest comment thread
12 Comment authors
jesperJoyDanMonikaOny Recent comment authors

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

  Subscribe  
newest oldest
Notify of
Trevor
Guest
Trevor

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

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
Guest
Matt

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

bashayer
Guest
bashayer

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

Monika
Guest
Monika

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
Guest
Dan

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