Rename A Country In WooCommerce
Rename A Country In WooCommerce

The default country names used by WooCommerce are usually correct. However, there are some occasions that you may want to change the default name of a country in WooCommerce into a shorter or more commonly used name.
Fortunately, WooCommerce makes it easy for you. All you need to do is add a small block of code into functions.php file. You will also need to make four small changes in the code.
Table of Contents
Add The Code Snippet
-
Open functions.php file in your theme’s root folder.
The easiest way to edit a theme file in WordPress is via the WordPress dashboard. After logging into the dashboard, click on the Appearance menu and then choose Theme Editor on the left sidebar.
In the window, you can see the template files and other documents from your active theme under Theme Files on the right side of the screen.
If you scroll down a bit, you can find the functions.php with a label Theme Functions. Click on functions.php to open it in the WordPress theme editor.
-
Copy the code snippet given below and paste.
Place it at the bottom of the functions.php file just before the closing PHP tag if there is one.
/**
* Rename a country
*/
add_filter( 'woocommerce_countries', 'rename_ireland' );
function rename_ireland( $countries ) {
$countries['IE'] = 'Ireland';
return $countries;
}
Explanation of the code
If you are not familiar with PHP or WordPress code, the code may look intimidating but it’s simple once you understand what each line of code does.
The first three lines of the code block are PHP comments.
The fourth line binds a new custom function that we will define at the bottom to woocommerce_countries filter hook. In this code, the name of the new custom function is rename_ireland.
The code block from 6th to 9th lines defines the custom function that we want to bind to the woocommerce_countries filter hook. You can see that the name of the function in the 6th line and the second parameter for the add_filter function are the same.
In the 7th line, the name of Ireland is changed from its original value to Ireland. So, this is the line of code you need to change if you want to rename a county in WooCommerce.
-
Rename the function to a name that helps you remember it’s purpose.
For example, if you are changing the country name of the US, you can rename the function as rename_us or rename_united_states.
Please remember that you need to change the function name at both places: where you call the function and where you define the function. Otherwise, it won’t work.
-
Replace the alpha 2 country code with the code of the country of which you want to change the name.
If you don’t know the Alpha-2 code of the countries as used in WooCommerce, you can refer to Table 1 given below.
-
-
Enter the desired name inside the single quotes after the equal sign on the 7th line.
-
This will be displayed on your website.
-
Save and close functions.php file and you are set to go.
Table 1
http://creatingawebstore.com/woocommerce-country-codes-and-state-codes-list.html