How to add a prefix and/or suffix to the order number in WooCommerce

In WooCommerce, order numbers are generated incrementally, making them predictable. To improve customization and security, we can add a prefix and/or a suffix to order numbers.

In this article, we’ll see how to achieve this using WooCommerce filters without affecting the system’s internal logic.

Adding a prefix and/or suffix to the order number

WooCommerce allows modifying the order number displayed to the user using the woocommerce_order_number filter.

Add the following code to your theme’s functions.php file or a custom plugin:

<?php
function custom_order_number( $order_id ) {
    $prefix = 'ORD-';  // You can change the prefix
    $suffix = '-2024'; // You can change the suffix

    return $prefix . $order_id . $suffix;
}
add_filter( 'woocommerce_order_number', 'custom_order_number' );
  • woocommerce_order_number: This filter allows modifying the order number before displaying it.
  • custom_order_number: Function that receives the order_id and adds a prefix and a suffix.

Important: this does not affect the database

This method only modifies the display of the order number. Internally, WooCommerce will continue using the real order_id in the database, ensuring compatibility with payment plugins and order management systems.

Additional Customization

If you want the prefix or suffix to change dynamically based on the date or user, you can modify the function as follows:

<?php
function dynamic_order_number( $order_id ) {
    $prefix = 'ORD-' . date('Y');  // Dynamic year as a prefix
    $suffix = '-' . get_current_user_id(); // User ID as a suffix

    return $prefix . $order_id . $suffix;
}
add_filter( 'woocommerce_order_number', 'dynamic_order_number' );

Adding a prefix or suffix to the WooCommerce order number is a simple way to customize the customer experience without affecting the system’s functionality.

Do you have any questions or need help with WooCommerce? Let us know in the comments!

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to Top
0
Would love your thoughts, please comment.x
()
x