Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Add a Prefix to WooCommerce Order Numbers Based on User Roles?

Want to gain instant insights into the types of customers placing orders on your WooCommerce store? With this simple customization, you can effortlessly distinguish between orders from regular customers and wholesale clients by adding distinct prefixes to order numbers. This insight helps you make smart decisions to grow your store. Let’s see the implementation part that dynamically adds prefixes to WooCommerce order numbers based on the roles of the users.

Solution: Add a Prefix to WooCommerce Order Numbers Based on User Roles

The code snippet retrieves the user associated with each order and assigns a prefix based on those roles. If the user is a regular customer (‘customer’ role), the prefix ‘CS’ is assigned. If they are a gold-level distributor (‘distributor_gold’ role), the prefix ‘GLD’ is assigned, and so on. 

function ts_filter_woocommerce_order_number( $order_number, $order ) {
    // Get user
    $user = $order->get_user();
    
    // Roles
    $roles = (array) $user->roles;
    
    // Compare
    if ( in_array ( 'customer', $roles ) ) {
        $prefix = 'CS'; // Prefix for regular customers
    } elseif ( in_array ( 'distributor_gold', $roles ) ) {
        $prefix = 'GLD'; // Prefix for gold-level distributors
    } elseif ( in_array ( 'distributor_silver', $roles ) ) {
        $prefix = 'SLVR'; // Prefix for silver-level distributors
    } elseif ( in_array ( 'wholesale_customer', $roles ) ) {
        $prefix = 'WH'; // Prefix for wholesale customers
    } elseif ( in_array ( 'administrator', $roles ) ) {
        $prefix = 'ADMIN'; // Prefix for administrators (optional)
    } else {
        $prefix = ''; // Default prefix for other cases
    }
    
    return $prefix . $order_number;
}
add_filter( 'woocommerce_order_number', 'ts_filter_woocommerce_order_number', 10, 2 );

Output

Whenever an order number is generated in WooCommerce, the code allows us to modify the order number by adding the desired prefixes based on the user’s role.

How to Add a Prefix to WooCommerce Order Numbers Based on User Roles? - Tyche Softwares


Just like customizing your WooCommerce orders to add a prefix based on user roles, it’s also beneficial to add details, such as phone numbers and email addresses to Order IDs. With this feature, you will not only improve order identification but also gain quick access to vital customer information.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x