If you run an online store that sells products across various categories, you might prioritize certain product categories over others. For example, imagine an online store selling products from categories such as groceries and fresh vegetables. In such cases, store owners would prefer to prioritize expedited shipping for the fresh vegetables category.
Whether you need to process order fulfillment for these specific categories or make a quick note of such orders without searching through the order details page, this WooCommerce customization is a breeze for you! Let’s see how you can easily set up this WooCommerce customization.
Solution: Add a Prefix to WooCommerce Order Numbers
The code snippet will add a prefix along with a unique custom order number containing items from the specific product category. For example, if your prefix is SPECIAL-
and the original order number is 1234
, the modified order number will be SPECIAL-1234
.
function ts_change_order_id_after_payment_completion( $order_id ) { // Get the order object $order = wc_get_order( $order_id ); // Check if the order contains products from selected categories if ( ts_order_contains_selected_categories( $order ) ) { // Generate a new order ID based on your custom logic $new_order_id = ts_generate_custom_order_id(); // Update the order ID $order->set_order_key( wc_clean( $new_order_id ) ); $order->set_id( null ); $order->save(); } } // Hook the function to run after payment completion add_action( 'woocommerce_payment_complete', 'ts_change_order_id_after_payment_completion', 10, 1 ); // Define a function to generate a custom order ID function ts_generate_custom_order_id() { // Example logic: Concatenate a prefix with the current time $custom_order_id = 'PAID-' . uniqid(); // Modified prefix to 'PAID-' and added a unique identifier return $custom_order_id; } // Function to check if an order contains products from selected categories function ts_order_contains_selected_categories( $order ) { // Define the selected category IDs $selected_category_ids = array( 50, 20, 30 ); // Replace with your category IDs // Loop through order items foreach ( $order->get_items() as $item_id => $item ) { $product_id = $item->get_product_id(); $terms = get_the_terms( $product_id, 'product_cat' ); if ( $terms && ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { if ( in_array( $term->term_id, $selected_category_ids ) ) { return true; } } } } return false; } // Modify the order number using the woocommerce_order_number filter hook function ts_modify_order_number( $order_id ) { // Get the order object $order = wc_get_order( $order_id ); // Check if the order contains products from selected categories if ( $order && ts_order_contains_selected_categories( $order ) ) { // Generate a new order ID based on your custom logic $new_order_id = ts_generate_custom_order_id(); return $new_order_id; } return $order_id; // Return the original order ID if no products from selected categories } // Hook the function to modify the order number add_filter( 'woocommerce_order_number', 'ts_modify_order_number', 10, 1 );
Output
If the order contains products from specific categories as mentioned in the code, it modifies the order number by adding a prefix using the woocommerce_order_number filter.
As this customization focuses on adding a prefix only to specific categories, you can enhance it further by including the associated category name to all WooCommerce order IDs. This simple yet powerful tweak gives you instant insight into which product categories are tied to each order.