WooCommerce: Order Number Prefix / Suffix

As you know WooCommerce uses the “order ID” (which is also the ID of the order post in the database) as the order number. This displays in the “WooCommerce” > “Orders” table, on each line of the order, under the “Order ” column, as well as the order “quick view” window, the single order page and the customer’s My Account page.

But what if you need to add a prefix or a suffix to this number, so that this is in line with your business or invoice requirements?

Here’s the fix – enjoy!

In this example, I’ve added a prefix “BB-” in front of the order numbers. I can see the change here in the Order table, and also in the quick view window (eye icon), the single order page, and the My Account > My orders page

PHP Snippet: Add Prefix and/or Suffix to WooCommerce Order Numbers

In the snippet below, I’m simply adding text before and after the order ID. Simply edit or remove the strings to display prefix and/or suffix.

/**
 * @snippet       Order Number Prefix/Suffix
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_order_number', 'bbloomer_order_number_prefix', 9999, 2 );

function bbloomer_order_number_prefix( $order_id, $order ) {
	return 'Prefix-' . $order_id . '-suffix';
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

4 thoughts on “WooCommerce: Order Number Prefix / Suffix

  1. Hi Rodolfo,
    Is there a way to add a prefix to the order number based on the product category?

    So,
    if (cat_1) prefix = AB
    if (cat_2) prefix = CD

    Thanks.

    1. Hello Fred, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      1. Hi! Thank you for the code. We currently use a plugin for this which keeps the order id and creates a new custom one as well.

        Does this code change the order id’s in the database as well? If the code is removed in the future, what happens?

        1. Nope this is only a “display” change, nothing will be edited in the DB. If you remove the snippet you go back to the default display.

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *