Show Full Country Name in WooCommerce Checkout Error

Quick Summary

  • Hook woocommerce_add_error to swap the two-letter ISO country code in the unsupported shipping country error for the full country name.
  • It finds the first half of the message, skips an optional the before the code, and looks up the name in WC()->countries->countries.
  • Matching depends on the exact English wording, so update $line1 and $line2 if your store language or a WooCommerce update changes the text.

When a customer picks a shipping country you don’t ship to, the WooCommerce checkout error can show the two-letter ISO country code instead of the full country name. This snippet catches that message and swaps the code for the full name, so the customer sees the country spelled out.

Add it to your child theme’s functions.php or a code snippets plugin. WooCommerce must be active.

PHP
//SHOW FULL COUNTRY NAME IN WOOCOMMERCE ERROR MESSAGE
// Convert error message of ISO 2 country code into country name
function wc_error_convert_country_code($message) {
    $line1 = 'Unfortunately <strong>we do not ship to '; // first half of the message
    $line2 = '</strong>. Please enter an alternative shipping address.'; // second half of the message
    // find $line1 sub-string in $message string
    $pos = strpos($message, $line1); // get position of sub-string
    if ($pos !== false) {
        // Does $line1 has ' the' append to it ?
        if (strpos($message, ' the', $pos) !== false) {
            $country_code2 = substr($message, strlen($line1) + 4, 2); // reads ISO 2 country code
            $country_name = WC()->countries->countries[$country_code2]; // converts to full country name
            $message = $line1 . ' the ' . $country_name . $line2;
        } else {
            $country_code2 = substr($message, strlen($line1) , 2); // reads ISO 2 country code
            $country_name = WC()->countries->countries[$country_code2]; // converts to full country name
            $message = $line1 . $country_name . $line2;
        }
    }
    return $message;
}
add_filter('woocommerce_add_error', 'wc_error_convert_country_code');

How it works

The function hooks into woocommerce_add_error, which filters the text of every error notice WooCommerce adds. Other messages pass through unchanged; only the shipping one is rewritten.

  • $line1 and $line2 are the two halves of the error text. strpos() checks whether the message contains the first half.
  • For some countries the message reads to the before the code. If the is found, the code starts four characters later, so substr() skips over it.
  • WC()->countries->countries is WooCommerce’s list of country names, keyed by ISO code. The code is looked up there and the message is rebuilt with the full name.

Things to keep in mind

The match depends on the exact English wording of the message, HTML tags included. If your store runs in another language, or a WooCommerce update changes the text, the check simply stops matching and the original message shows. Copy the current text from your checkout and update $line1 and $line2 if they differ.

The code also assumes the message starts with $line1, because it reads the country code from a fixed position counted from the start of the string.

For checkout validation and error messages tailored to your store, see my WooCommerce development services.

Share on X (opens in a new tab) Share on LinkedIn (opens in a new tab) Share on Facebook (opens in a new tab)

ChatGPT (opens in a new tab) Claude (opens in a new tab) Gemini (opens in a new tab) Perplexity (opens in a new tab) Grok (opens in a new tab)

More Insights

See More