如何在WooCommerce 3中修改USPS配送方式的显示名称
Got it! To rename those USPS shipping options to shorter, cleaner labels, you can use WooCommerce's woocommerce_shipping_method_title filter hook. This lets you override the default title displayed in the cart and checkout pages.
Here's the code you'll need:
add_filter( 'woocommerce_shipping_method_title', 'custom_rename_usps_shipping_methods', 10, 2 ); function custom_rename_usps_shipping_methods( $original_title, $shipping_method ) { // Check the original title (matches what you see in cart/checkout) $lowercase_title = strtolower( $original_title ); if ( $lowercase_title === 'first-class package service' ) { return 'Standard'; } elseif ( $lowercase_title === 'priority mail' ) { return 'Priority'; } // Return original title for all other shipping methods return $original_title; }
How to use this code:
- Add this snippet to your child theme's
functions.phpfile. Always use a child theme so your changes don't get overwritten when you update your main theme. - If you don't want to edit theme files, use a custom code plugin to add the code safely.
Alternative (more reliable if titles change):
If you want to target the USPS methods by their internal IDs (which don't change even if the title updates), you can adjust the code to check the method's ID instead. For USPS, the IDs are typically something like usps_first_class and usps_priority_mail (your exact IDs might vary—you can find them by inspecting the shipping method settings in WooCommerce). Here's that version:
add_filter( 'woocommerce_shipping_method_title', 'custom_rename_usps_shipping_methods_by_id', 10, 2 ); function custom_rename_usps_shipping_methods_by_id( $original_title, $shipping_method ) { // Check the shipping method's internal ID switch( $shipping_method->id ) { case 'usps_first_class': // Replace with your actual First-Class ID return 'Standard'; case 'usps_priority_mail': // Replace with your actual Priority Mail ID return 'Priority'; default: return $original_title; } }
Just replace the placeholder IDs with the ones from your WooCommerce USPS settings, and this will work even if the default titles get updated. Either way, once you add the code, the shipping labels should update immediately in your cart and checkout pages.
内容的提问来源于stack exchange,提问作者Austin Sobotka

