skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. You could use a plugin like Loco translate if you want, that makes easier to do your job.. but you could try this code, similar to @LoicTheAztec inside functions.php (either on your theme or child theme file)

        add_filter( 'gettext', 'ds_translate_woocommerce_strings', 999, 3 );
    function ds_translate_woocommerce_strings( $translated, 
    $untranslated, $domain ) {
        if ( ! is_admin() ) {
            switch ($translated) {
            case 'the message you want to be translated exactly as you see it in WP':
                    $translated = 'your new message';
                    break;
            }
        }
        return $translated;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search