Search code examples
phpwordpresswoocommercelabel

Changing shipping method label names in Woocommerce 3


I've added two shipping options from USPS to my Wordpress site: priority mail and first-class package service. The options are displayed as "first-class package service" and "priority mail", respectively, on both the cart and the checkout pages.

I find the titles both a bit bulky, and would like to change them to read "standard" and "priority", respectively, on both the cart and checkout pages.

Any ideas as to what code I would need to add to accomplish this?


Solution

  • Update: Your labels names where not correct (Also added alternative based on method IDs)

    The code below will allow you to change/rename your shipping method displayed label names. Below there is 2 Alternatives:

    1) Based on Shipping Wethod label name:

    add_filter( 'woocommerce_package_rates', 'change_shipping_methods_label_names', 20, 2 );
    function change_shipping_methods_label_names( $rates, $package ) {
    
        foreach( $rates as $rate_key => $rate ) {
    
            if ( __( 'First-Class Package Service', 'woocommerce' ) == $rate->label )
                $rates[$rate_key]->label = __( 'Standard', 'woocommerce' ); // New label name
    
            if ( __( 'Priority Mail', 'woocommerce' ) == $rate->label )
                $rates[$rate_key]->label = __( 'Priority', 'woocommerce' ); // New label name
        }
    
        return $rates;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    You should need to refresh the shipping caches:
    1) First this code is already saved on your function.php file.
    2) In Shipping settings, enter in a Shipping Zone and disable a Shipping Method and "save". Then re-enable that Shipping Method and "save". You are done.


    2) Based on Shipping Method IDs:

    add_filter( 'woocommerce_package_rates', 'change_shipping_methods_label_names', 10, 2 );
    function change_shipping_methods_label_names( $rates, $package ) {
    
        foreach( $rates as $rate_key => $rate ) {
    
            if ( 'wc_services_usps:1:first_class_package' == $rate_key )
                $rates[$rate_key]->label = __( 'USPS first class', 'woocommerce' ); // New label name
    
            if ( 'wc_services_usps:1:pri' == $rate_key )
                $rates[$rate_key]->label = __( 'USPS priority', 'woocommerce' ); // New label name
        }
        return $rates;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    You should need to refresh the shipping caches:
    1) First this code is already saved on your function.php file.
    2) In Shipping settings, enter in a Shipping Zone and disable a Shipping Method and "save". Then re-enable that Shipping Method and "save". You are done.