1

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?

starball
  • 20,030
  • 7
  • 43
  • 238

1 Answers1

6

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I tried adding the code as it is and it did not work. I do have the USPS Shipping Plugin installed, though the shipping options that I have enabled are through WooCommerce (that is, I have not selected any of the shipping options within the USPS plugin). Here's the website URL for the cart page (though shipping options only display once items are added to the cart): https://brewhanger.com/cart/ – Austin Sobotka Mar 28 '18 at 18:48
  • @AustinSobotka Answer updated… Your original label names where not the good one in your question… I have added an alternative based on the Method IDs. Also you will need to refresh shipping methods as explained… – LoicTheAztec Mar 29 '18 at 11:39
  • So this may or may not be related, but I've just noticed that since making this change items are no longer being packed together. That is, if I place two or more items in my cart it charges shipping for each item, rather than adding the items together into one package. I have the "pack items together" option selected. If this isn't related I'll post elsewhere! – Austin Sobotka Mar 30 '18 at 19:28
  • @AustinSobotka The code that I use in this answer only change existing shipping methods label names without altering the existing shipping methods structured data and packages. So it is absolutely not involved in the issue that you describe… – LoicTheAztec Mar 31 '18 at 21:15
  • @LoicTheAztec I am using this on one of my sites, and I have found that it will randomly stop working and I would have to refresh the shipping caches again. Have you found this an issue at all? I have tried using 'woocommerce_cart_shipping_method_full_label' as someone suggested it could be the fact I'm updating the label. Though this no longer pulls into the email notification as the right label or in the order information. – Tom Jul 20 '20 at 10:50
  • @Tom this code still works nicelly… so in your case there is something else that is making trouble. – LoicTheAztec Jul 20 '20 at 13:06
  • @LoicTheAztec I am showing a date in the label which shows a date that is calculated based on the shipping time + product turnaround of the item. So the label is calculated differently each time. Could it be this that is causing the issue? – Tom Jul 20 '20 at 13:23
  • @Tom Maybe, I can't say… – LoicTheAztec Jul 20 '20 at 13:26