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

How to Combine Multiple Elements to Custom Order Numbers in WooCommerce?

The customization of combining multiple elements to custom order numbers benefits both the backend management process and customers. It allows customers and store owners to quickly identify the order date, customer, and product category just by looking at the order column. Let’s see how to implement this WooCommerce customization and make sorting and locating orders much more efficient.

Solution: Combine Multiple Elements to Custom Order Numbers

The code will include details such as order date, customer initials, product category, and a random number from a custom order number in the format YYYY/MM/DD-CI-PC-RandomNumber.

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

function ts_custom_order_number_format($order_id, $order) {
    // Get order date
    $order_date = $order->get_date_created();

    // Get customer initials
    $customer_id = $order->get_customer_id();
    $customer = new WC_Customer($customer_id);
    $first_name = $customer->get_first_name();
    $last_name = $customer->get_last_name();
    $customer_initials = substr($first_name, 0, 1) . substr($last_name, 0, 1);

    // Get product category
    $items = $order->get_items();
    $product_category = '';

    foreach ($items as $item) {
        $product_id = $item->get_product_id();
        $product_categories = get_the_terms($product_id, 'product_cat');

        if (!empty($product_categories)) {
            $product_category = $product_categories[0]->name;
            break; // Use the first product category found
        }
    }

    // Generate random order number
    $random_order_number = rand(1000, 9999);

    // Combine elements to form order number
    $custom_order_number =  $order_date->format('Y/m/d') . '-' . $customer_initials . '-' . $product_category . '-' . $random_order_number;

    return $custom_order_number;
}

Output

The order numbers displayed in the image below are more informative and unique since they combine multiple elements such as order date, customer initials, and product category name.

How to Combine Multiple Elements to Custom Order Numbers in WooCommerce? - Tyche Softwares

Customers will also be able to view the customized order number on the order received page or in their account’s orders page, as shown below.

How to Combine Multiple Elements to Custom Order Numbers in WooCommerce? - Tyche Softwares

If you prefer sequential order numbers, we have a separate post on adding sequential custom order numbers. Please just hop on to the insightful post we made to get more info. 

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