Change Woocommerce order number to specific number of digits

Rate this post

I need to change the Woocommerce order number from 4 to 5 digits. I know I can a prefix or suffix with this

add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number', 1, 2);

function change_woocommerce_order_number($order_id, $order) {
    $prefix = 'WC';
    $suffix = '-XY'; 
    return $prefix . $order->id . $suffix;

But how can I change the number of digits?



Use str_pad() PHP function as follows:

add_filter( 'woocommerce_order_number', 'customize_order_number', 10, 2 );
function customize_order_number( $order_id, $order ) {
    $digits = 5;
    $prefix = '';
    $suffix = ''; 

    return $prefix . str_pad($order_id, $digits, '0', STR_PAD_LEFT) . $suffix;

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Leave a Reply

Your email address will not be published.

Back to top button