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.
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.
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.
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.
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
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?
nvm 🙂 I figured it out. I had to get the user role of the customer, not the logged in user.
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?
Great! this works perfectly. Order number is printed with PRE/SUFFIX in all our systems.
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?
I too am wondering where this post may be…