With WooCommerce, you will lack the flexibility to customize order numbers to include additional information, such as the product category. But here is a rescue code snippet that can be incredibly useful in scenarios where you need to quickly identify orders based on the product category.
By adding the product category to your order numbers, you can instantly see which orders are related to each category. Let’s dive into the solution to quickly spot orders based on product categories.
Solution: Add Product Category Name to WooCommerce Order Numbers
The code snippet will add product category names to the WooCommerce Order IDs.
add_filter( 'woocommerce_order_number', 'ts_custom_order_number', 1, 2 ); function ts_custom_order_number( $order_id, $order ) { // Initialize category name variable $category_name = ''; // Get items in the order $items = $order->get_items(); // Check if items exist and get the first item if ( $items && $first_item = reset( $items ) ) { // Get the product ID of the first item $product_id = $first_item->get_product_id(); // Check if product ID exists and get the product object if ( $product_id && $product = wc_get_product( $product_id ) ) { // Get category IDs of the product $categories = $product->get_category_ids(); // Check if categories exist and get the name of the first category if ( $categories && $category = get_term_by( 'id', $categories[0], 'product_cat' ) ) { // Get the name of the category $category_name = $category->name; } } } // Return category name + order ID return $category_name . $order_id; }
Output
The code snippet modifies the default order IDs by appending the associated product categories to the orders.
Similarly, just as you can access product category details directly from the admin orders page, you can also add order numbers with date prefix. This way store owners can quickly manage orders using the order placement date.