Order numbers can be set to be created in many predictable formats, such as alphanumeric patterns, sequential order number patterns, random patterns, etc. However, some store owners or external integrations might require the order numbers to be followed in a consistent length as well. In such cases, you can use leading zeros to order numbers and maintain the length of the order numbers as you prefer.
Solution: Modify WooCommerce Order Numbers with Leading zeros
The code adds leading zeros to the order ID to maintain a consistent length of 5 characters. For example, if the default order ID is 123, the order number would be padded with leading zeros to ensure it’s always 5 characters long. So, the order number would become “00123”.
add_filter( 'woocommerce_order_number', 'ts_add_leading_zeros_to_order_number', 1, 2 ); function ts_add_leading_zeros_to_order_number( $order_id, $order ) { // Add leading zeros to order ID $order_id_with_zeros = str_pad( $order_id, 5, '0', STR_PAD_LEFT ); return $order_id_with_zeros; }
Output
The output image shows that all order numbers have a consistent length of 5 characters, with leading zeros added as necessary.
If you’re curious about making your order numbers to be arraned in a sequence, there’s a post we have covered for you. It talks about adding the adding sequential custom WooCommerce order numbers. It could be a useful read to help you improve your order tracking!