Show Full Country Name In Woocommerce Checkout Error

Share Now

//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');
Picture of Nahid Mahamud

Nahid Mahamud

Web Developer | Graphic Designer | WordPress & Woo-commerce Expert