Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

php - Change "No shipping method has been selected.…" from WooCommerce checkout notice

I am trying to tackle an issue by renaming the default message below when there are no shipping methods available, either set by rule based plugins or there simply isn't any methods set in WooCommerce.

No shipping method has been selected. Please double check your address, or contact us if you need any help.

I have used the below function using the snippets plugin and I can confirm it changes the message in the cart and checkout underneath the shipping label.

However, if someone then attempts to checkout they get a red WooCommerce error message at the top of the screen which is still the default message above.

How do I also change this error message ? I would like to change it to something different than what my code below shows to give me the flexibility to put a short message in the cart totals box and a longer more informational message as to why no methods are available.

My Function:

add_filter( 'woocommerce_cart_no_shipping_available_html', 'no_shipping_available_html' );
add_filter( 'woocommerce_no_shipping_available_html', 'no_shipping_available_html' );

function no_shipping_available_html( $message ) {
    $country = WC()->customer->get_shipping_country();
    if ( !empty( $country ) ) {
        $all_countries  = WC()->countries->get_countries(); 
        return sprintf( 'Orders delivered into the EU are limited to €150 (inc shipping) or a max weight of 2kg. The items in you cart exceed this limit. Please remove an item or reduce the quantity required to bring the order value/weight within the permitted values.', $all_countries[ $country ] );
    }
    return 'Orders delivered into the EU are limited to €150 (inc shipping) or a max weight of 2kg. The items in you cart exceed this limit. Please remove an item or reduce the quantity required to bring the order value/weight within the permitted values.';
}

Error message at the top which remains unaffected by the function provided.

question from:https://stackoverflow.com/questions/66048796/change-no-shipping-method-has-been-selected-from-woocommerce-checkout-notice

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This text is located in WC_Checkout Class inside validate_checkout() method. There are no filters to make changes to it, but you can use WordPress gettext filter for that as follows:

add_filter(  'gettext',  'change_checkout_no_shipping_method_text', 10, 3 );
function change_checkout_no_shipping_method_text( $translated_text, $text, $domain ) {
    if ( is_checkout() && ! is_wc_endpoint_url() ) {
        $original_text = 'No shipping method has been selected. Please double check your address, or contact us if you need any help.';
        $new_text      = 'Here your own text replacement.';
        
        if ( $text === $original_text ) {
            $translated_text = $new_text;
        }
    }
    return $translated_text;
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...