移除WooCommerce「订单待处理」及「新订单」邮件中的订单编号
Hey there! Let's work through this problem together—you want to completely hide WooCommerce's auto-generated order numbers (both from customers and admins) until your third-party plugin assigns the custom order number, and ensure only that custom number appears in all emails and views afterward. Here's a solid, code-based solution that covers all bases:
First, we'll use WooCommerce's built-in woocommerce_order_number filter to override the order number displayed anywhere—frontend, backend, emails, you name it. This checks if your custom order number exists, and uses that instead of the original. If the custom number isn't ready yet, it returns nothing (or a placeholder of your choice) so the original number never shows.
Add this code to your theme's functions.php file, or a custom plugin (recommended for safety if you switch themes):
add_filter('woocommerce_order_number', 'replace_original_order_number_with_custom', 10, 2); function replace_original_order_number_with_custom($order_number, $order) { // Replace 'custom_order_number' with the meta key your plugin uses for custom numbers // (Check wp_postmeta table if you're unsure—many plugins use a prefix like '_custom_order_number') $custom_number = $order->get_meta('custom_order_number'); // Use custom number if available; else, return empty string to hide original if (!empty($custom_number)) { return $custom_number; } else { return ''; // Optional: Replace with 'Order Processing...' for a friendlier placeholder } }
While the above filter covers most email cases, we'll add extra safeguards to make sure the original order number doesn't sneak into email subjects or content before the custom number is assigned.
Filter Email Subjects
This removes or replaces the original order number in both admin "New Order" and customer "Order Processing" email subjects:
// Target both admin and customer order emails add_filter('woocommerce_email_subject_new_order', 'customize_email_subject_for_custom_order_numbers', 10, 2); add_filter('woocommerce_email_subject_customer_processing_order', 'customize_email_subject_for_custom_order_numbers', 10, 2); function customize_email_subject_for_custom_order_numbers($subject, $order) { $custom_number = $order->get_meta('custom_order_number'); if (!empty($custom_number)) { // Swap the default {order_number} placeholder with your custom number $subject = str_replace('{order_number}', $custom_number, $subject); } else { // Strip any original order number from the subject if custom isn't ready $subject = preg_replace('/\s*\#{0,1}\s*[0-9]+/', '', $subject); } return $subject; }
Clean Up Email Content
This ensures the order number section in emails either shows the custom number or gets removed entirely if the custom number isn't assigned yet:
add_filter('woocommerce_email_order_details', 'remove_original_order_number_from_emails', 10, 4); function remove_original_order_number_from_emails($order_details, $order, $sent_to_admin, $plain_text) { $custom_number = $order->get_meta('custom_order_number'); if (!empty($custom_number)) { // Replace any leftover references to the original order ID/number $original_number = $order->get_id(); $order_details = str_replace($original_number, $custom_number, $order_details); $order_details = str_replace('{order_number}', $custom_number, $order_details); } else { // Remove the entire order number line from HTML/plain text emails if (!$plain_text) { $order_details = preg_replace('/<tr.*?order-number.*?<\/tr>/s', '', $order_details); } else { $order_details = preg_replace('/Order Number:\s*[0-9]+/', '', $order_details); } } return $order_details; }
We also need to make sure admins don't see the original order number in the backend orders list before the custom number is assigned:
// Customize the order number column in admin orders list add_action('manage_shop_order_posts_custom_column', 'customize_admin_order_number_display', 10, 2); function customize_admin_order_number_display($column, $post_id) { if ($column === 'order_number') { $order = wc_get_order($post_id); $custom_number = $order->get_meta('custom_order_number'); if (!empty($custom_number)) { echo '<a href="' . get_edit_post_link($post_id) . '">' . esc_html($custom_number) . '</a>'; } else { // Show a placeholder instead of the original number echo '<span style="color: #666;">Order Pending Custom Number</span>'; } } }
- Meta Key Check: Double-check the meta key your third-party plugin uses (e.g.,
_custom_order_numberinstead ofcustom_order_number). You can verify this by looking at thewp_postmetatable for an order that has the custom number assigned. - Test Flows: Create a test order and verify:
- Before the custom number is assigned: No original order number appears in customer emails, admin emails, or backend order views.
- After the custom number is assigned: The custom number shows up everywhere the original would have.
- Plugin Compatibility: If your third-party plugin has a hook that fires when the custom number is assigned, you could add a flag to ensure the placeholder switches over immediately, but the above code works dynamically by checking for the meta value on every load.
内容的提问来源于stack exchange,提问作者RBaum

