In WordPress, when you create new content, such as blog posts, product pages, and images, they’re categorized as post types. These post types receive sequential IDs in the wp_posts table of the database. Where things get disruptive in this default sequential order numbering system is when post types are inserted or uploaded between new WooCommerce orders.
This issue often causes WooCommerce order numbers to break, and that’s why you will see non-sequential orders in your WooCommerce orders table. To effectively address this, we’ve meticulously outlined a proven, straightforward WooCommerce customization solution in this post.
Solution: Add Sequential Custom Order Numbers in WooCommerce
The code snippet will add sequential order numbers to the WooCommerce Order IDs. The code also provides you the flexibility to specify the desired number of digits (e.g. $digits = 4;) for the order number. For example, if the current sequential order number is 1, the order number will be padded with zeros to make it 0001
.
add_filter( 'woocommerce_checkout_create_order', 'ts_save_order_number_metadata' ); function ts_save_order_number_metadata( $order ) { $digits = 4; // Number of desired digits for the number part $data = get_option('wc_sequential_order_number'); // Get order number sequential helper registered data $number = isset($data['sequential']) ? intval($data['sequential']) + 1 : 1; $data['sequential'] = $number; // Update order number sequential helper registered data update_option('wc_sequential_order_number', $data); // Add order number as custom metadata $order->add_meta_data('_order_number', str_pad($number, $digits, '0', STR_PAD_LEFT), true); } // Read the order number from metadata add_filter( 'woocommerce_order_number', 'ts_define_order_number', 10, 2 ); function ts_define_order_number( $order_id, $order ) { if ( $order_number = $order->get_meta('_order_number') ) { $order_id = $order_number; } return $order_id; }
Output
When a new order is created during the checkout process, the code retrieves the current sequential order number from the database (stored in the wc_sequential_order_number option), increments it by 1, and then saves it back to the database. It also adds the order number as custom metadata to the order object, padding it with zeros to achieve the desired number of digits.
If you want to add some extra flair to your WooCommerce order numbers, like adding a special prefix or suffix, then it’s just a few steps away. Check out here to add prefix or suffix to WooCommerce orders.
Hi — Possible combine this Generate Sequential Number set of codings with your Adding Prefix set of codings and — also to add a way to “RESET” the initial number? (Tried the combinations too many times and now its AS-2025-12, I need it to be back to AS-2025-0001. Currently I have the Prefix coding and the Sequential number coding in separate Code Snippet Instances. (1) and (2) (1) // Change Order Number format – Add Prefix add_filter( ‘woocommerce_order_number’, ‘change_woocommerce_order_number’ ); function change_woocommerce_order_number( $order_id ) { $prefix = ‘AS’.’-‘. date(‘Y’); $order = wc_get_order( $order_id ); $new_order_id = $prefix . ‘-‘. $order_id… Read more »
Hi Faustine, Thanks for reaching out with your detailed query! It seems you’re trying to combine multiple code snippets for things like prefixes, sequential numbering, and resets. The issue you’re encountering arises from the fact that WooCommerce order numbers are inherently tied to the post ID in the wp_posts table and in HPOS the database structure is bit different. This means that custom modifications to the order number could conflict with the default order ID handling and so it leads to unpredictable results. All your requirements can be easily handled with our Custom Order Numbers Pro plugin. Please take a look… Read more »
Hmm we do not use HPOS. Just need to have the Prefix + Year + (Index) number to be together .. and then have to reset for now…. since I tried so many times. …. (For systematic order numbers for the retail business)
Probably yearly reset is another code altogether as well . which may be able to achieve if we can edit your 24 hours reset code in another post.
Any way to achieve via Code snippets for the above?
I get what you’re asking for, but it’s not possible to give independent snippets for various functionalities in bits & pieces.
I mean can the whole thing be combined into 1 coding snippet?
I did try the 1st 2.. and it worked once.. then somehow when I combined both together it didn’t work. will try again later.
I would also love to know if its possible to RESET “created” order number to 1 when Year 2026 comes. (So that is linked to your 24 hours RESET post). Will maybe try the 2 codes separately again — since that was how it works on the 1st time.. but ideally would have been better to combine — PREFIX + Generate Sequential order number + RESET per year together… Is there a way to manually RESET to 1 via code snippet — since I tried test ordering so many times…. (I used your Custom Order Number plugin to reset currently… Read more »
Thank you for this. It helps. But have a question, since I made the trials till Order number 6, it seems even if i deleted the orders and try again, Order number will add on to the 6 — become 7. How can I “reset” it to 0 to start again for LIVE orders?
Thanks.