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.
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.
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.
Hi, Just saw this Combining codes post. I have a question earlier on Sequential Order number (SO Number) and I can use the above code to combine. But the Generating of SO Number Number — how can that be combined into the above code? So far I have tried this but am stuck at SO Number. Also I need leading 0 (to 4 digits format of the SO number — how can I add that code in as well?) I am referring to your respective posts : 1) Leading zeros – https://www.tychesoftwares.com/how-to-modify-woocommerce-order-numbers-with-leading-zeros/ — and 2) Sequential Order number generation –… Read more »
Hi Faustine,
Unfortunately, we won’t be able to provide support for requests involving combining multiple snippets or any other custom solutions like this.
I am really just short on one set of codes for the Combination — ie : Sequential number generation… 🙁