Woocommerce: Add prefix or suffix to order number without plugin
Here is an example you can add prefix to the order number,
add_filter( 'woocommerce_order_number', 'add_prefix_woocommerce_order_number', 1, 2); function add_prefix_woocommerce_order_number( $order_id, $order ) { return $order->get_date_created()->format ('Ymd').$order_id; }
The script above will use order created date as prefix. If the order number originally is 1234
, and the order created date is 2020-10-30
, then the final order number will become this 202010301234
.
Likewise, if you want to add suffix, it is similar:
add_filter( 'woocommerce_order_number', 'add_suffix_woocommerce_order_number', 1, 2); function add_suffix_woocommerce_order_number( $order_id, $order ) { return $order_id.$order->get_date_created()->format ('Ymd');//note $order_id is at ahead }
Of course you can add prefix and suffix altogether to the original order id.
eg: "prefix-here-".$order_id."suffix-here"
You get the idea.
Bonus tips, if you want the order id to be fixed length and add `0` (Zero-pad) to the left or right of the id, you can use following script.
add_filter( 'woocommerce_order_number', 'add_padding_woocommerce_order_number', 1, 2); function add_padding_woocommerce_order_number( $order_id, $order ) { return str_pad($order_id, 8, '0', STR_PAD_LEFT); //the 4th parameter (pad_type) can be STR_PAD_RIGHT, or STR_PAD_BOTH as well }
The above script will change order id with `1234` become `00001234`;
And again you can add prefix with padding as well, eg: $order->get_date_created()->format ('Ymd').str_pad($order_id, 8, '0', STR_PAD_LEFT);
More about php `str_pad`, you can check it out here.