Changing shipping method label names in Woocommerce 3

这一生的挚爱 提交于 2019-12-13 00:36:16

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/49523271/changing-shipping-method-label-names-in-woocommerce-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!