Leading zeros in the WooCommerce order number

To change the WooCommerce order number to include leading zeros, add the following code to the functions.php file in the theme folder you use on your installation. To change the number of leading zeros, modify the variable $max_length accordingly.

add_filter( 'woocommerce_order_number', 'swp_woocommerce_order_number_leading_zeros', 10 );
function swp_woocommerce_order_number_leading_zeros( $order_id ) {
    $max_length = 8;
    $new_order_id = str_pad( $order_id, $max_length, '0', STR_PAD_LEFT );
    return $new_order_id;

View the code on Gist.

If you do not use the child theme, remember that changes made to the functions.php file will be overwritten when the theme is updated.

While copying code to functions.php file it is not nessesary to copy <?php opening tag.

About grola

Passionate about Wordpress and WooCommerce for many years. Author of plugins and short snippets improving Wordpress and WooCommerce.

View all posts by grola →