如何在WooCommerce中批量重命名订单状态?
Hey there! I get that you're looking to rename two WooCommerce order statuses by editing your theme's functions.php file, and you have a working snippet for a single status but aren't sure how to expand it. Let's fix that—here's how to adjust the code to handle both your desired changes cleanly.
First, let's recap the original code's logic: it loops through all existing order statuses, keeps most of them as-is, and overrides the display name for a specific status key. To modify multiple statuses, we can either add extra conditional checks or use a mapping array (the latter is more scalable if you ever need to add more status changes later).
Option 1: Add Multiple Conditional Checks
This is a straightforward tweak to your original code—just add an extra if block for each status you want to rename:
function wc_renaming_order_statuses( $order_statuses ) { $new_order_statuses = array(); // Loop through each existing order status foreach ( $order_statuses as $key => $status ) { // Keep the original status by default $new_order_statuses[ $key ] = $status; // Rename 'wc-processing' to 'Paid' if ( 'wc-processing' === $key ) { $new_order_statuses[ $key ] = _x( 'Paid', 'Order status', 'woocommerce' ); } // Rename 'wc-on-hold' to 'Pending' if ( 'wc-on-hold' === $key ) { $new_order_statuses[ $key ] = _x( 'Pending', 'Order status', 'woocommerce' ); } } return $new_order_statuses; } add_filter( 'wc_order_statuses', 'wc_renaming_order_statuses' );
Option 2: Use a Status Mapping Array (More Scalable)
If you think you might need to rename more statuses down the line, this method is cleaner. We'll create an associative array that maps old status keys to their new display names, then loop through and apply the changes:
function wc_renaming_order_statuses( $order_statuses ) { // Define your status renaming mappings here $status_mappings = array( 'wc-processing' => _x( 'Paid', 'Order status', 'woocommerce' ), 'wc-on-hold' => _x( 'Pending', 'Order status', 'woocommerce' ) // Add more key-value pairs here if you need to rename additional statuses ); $new_order_statuses = array(); foreach ( $order_statuses as $key => $status ) { // Use the new name if the key exists in our mappings; else keep the original $new_order_statuses[ $key ] = isset( $status_mappings[ $key ] ) ? $status_mappings[ $key ] : $status; } return $new_order_statuses; } add_filter( 'wc_order_statuses', 'wc_renaming_order_statuses' );
Important Notes:
- Always add this code to your child theme's
functions.phpfile—never edit the parent theme directly, as your changes will be lost when the theme updates. - The
_x()function is used instead of a plain string to maintain WooCommerce's translation compatibility, so it's best to keep that intact. - After saving the changes, refresh your WooCommerce orders page, and you should see the updated status names immediately.
内容的提问来源于stack exchange,提问作者beth

