Change default country WC
March 24, 2025 · AI Conversations
—
### ✅ Option 1: Change from WooCommerce Settings (No code)
1. Go to **WooCommerce** > **Settings**
2. Under the **General** tab, find **"Selling location(s)"** and **"Default customer location"**
3. For "Default customer location", choose one of the following:
– **Shop base address** – Uses your store’s address country.
– **Geolocate** – Detects user’s location automatically.
– **Geolocate (with page caching support)** – Same but with extra compatibility.
– **No location by default** – No prefilled country.
> If you want to set it specifically, go to the **“General Options”** and set **"Base location"** to the country you want as default.
—
### ✅ Option 2: Set programmatically via code (Custom default country)
If you want to **force a specific country regardless of store location or geolocation**, add this to your theme’s `functions.php` or in a custom plugin:
add_filter( 'default_checkout_country', 'custom_default_checkout_country' );
function custom_default_checkout_country( $country ) {
return 'GB'; // Replace with your desired country code (e.g., 'US', 'IN', 'DE', 'FR')
}
You can also set the default state like this:
add_filter( 'default_checkout_state', 'custom_default_checkout_state' );
function custom_default_checkout_state( $state ) {
return 'ENG'; // Example: 'CA' for California, 'ENG' for England
}
—
Let me know if you're using a page builder or a custom checkout plugin (like Checkout Field Editor), and I can tailor the instructions for that too.
add_filter( 'default_checkout_country', 'set_default_checkout_country_ireland' );
function set_default_checkout_country_ireland( $country ) {
return 'IE'; // IE is the country code for Ireland
}
### Optional: Set a default state for Ireland
If you want to set a default **county** (state) as well — for example, **Dublin**:
add_filter( 'default_checkout_state', 'set_default_checkout_state_ireland' );
function set_default_checkout_state_ireland( $state ) {
return 'D'; // 'D' is the state code for Dublin in Ireland
}
> 📝 Don’t forget to clear cache (including object cache if using one) and test it on an incognito window.
Let me know if you're using a custom checkout plugin or theme that might override this behavior.
add_filter( 'woocommerce_checkout_fields', 'make_postcode_and_phone_required' );
function make_postcode_and_phone_required( $fields ) {
$fields['billing']['billing_postcode']['required'] = true;
$fields['billing']['billing_phone']['required'] = true;
return $fields;
}
### 🧼 Clear Cache & Test:
– Clear your site and browser cache after adding this.
– Try testing the checkout in incognito mode or as a guest user.
Let me know if you’re using a custom checkout plugin or layout override — the implementation might vary slightly.
– **Country-dependent field rules** (e.g., some countries don’t require postcode)
– **Custom checkout plugins or themes** (e.g., Astra, Flatsome, Elementor, etc.)
– **JavaScript resetting WooCommerce field rules**
—
### ✅ Fix 1: Force Postcode Required via JS after country change
WooCommerce dynamically updates fields based on country selection using JavaScript. To override that behavior, you can force `billing_postcode` to remain required after the country updates.
**Add this JavaScript snippet** in your theme (preferably enqueue it in a custom script):
jQuery(function($) {
function forcePostcodeRequired() {
$('#billing_postcode').prop('required', true);
$('#billing_postcode_field').addClass('validate-required');
}
// Run on page load
forcePostcodeRequired();
// Re-run whenever country is changed
$('body').on('updated_checkout', function() {
forcePostcodeRequired();
});
});
You can enqueue it in your theme like this (add to `functions.php`):
add_action( 'wp_enqueue_scripts', 'force_postcode_required_script' );
function force_postcode_required_script() {
if ( is_checkout() ) {
wp_add_inline_script( 'woocommerce-checkout', "
jQuery(function($) {
function forcePostcodeRequired() {
$('#billing_postcode').prop('required', true);
$('#billing_postcode_field').addClass('validate-required');
}
forcePostcodeRequired();
$('body').on('updated_checkout', function() {
forcePostcodeRequired();
});
});
" );
}
}
—
### ✅ Fix 2: Set `billing_postcode` required for specific countries (e.g., IE)
WooCommerce may treat `billing_postcode` as **optional** for countries like Ireland by default.
To make it required **only when Ireland is selected**, use:
add_filter( 'woocommerce_default_address_fields', 'force_postcode_required_for_ireland' );
function force_postcode_required_for_ireland( $fields ) {
if ( isset($_POST['billing_country']) && $_POST['billing_country'] === 'IE' ) {
$fields['postcode']['required'] = true;
}
return $fields;
}
—
### ✅ Best Practice: Combine PHP + JS
Sometimes, **PHP alone isn't enough** because of WooCommerce JS updates. Using **both the PHP and JS methods together** ensures the field stays required even after cart updates, shipping changes, or AJAX reloads.
—
Let me know which theme or plugins you're using (like Checkout Field Editor, Elementor, etc.), and I can help make it even more precise.
—
### ✅ PHP Snippet – Change Label
add_filter( 'woocommerce_checkout_fields', 'custom_checkout_postcode_label' );
function custom_checkout_postcode_label( $fields ) {
$fields['billing']['billing_postcode']['label'] = 'Eircode'; // Change this to your custom label
return $fields;
}
> 📝 Example: For Ireland, replacing "Postcode / ZIP" with "Eircode" improves UX and clarity.
—
### 🧪 Bonus: Also change the placeholder (optional)
If you'd like to change the placeholder text too:
$fields['billing']['billing_postcode']['placeholder'] = 'Enter your Eircode';
So the full function becomes:
add_filter( 'woocommerce_checkout_fields', 'custom_checkout_postcode_label' );
function custom_checkout_postcode_label( $fields ) {
$fields['billing']['billing_postcode']['label'] = 'Eircode';
$fields['billing']['billing_postcode']['placeholder'] = 'Enter your Eircode';
return $fields;
}
—
Let me know if you're using a multi-language plugin (like WPML), and I can help make this label translatable.
Let’s fully fix that:
—
### ✅ Step 1: Force Postcode Required (you already did this)
You're already making `billing_postcode` required with this:
add_filter( 'woocommerce_checkout_fields', 'make_postcode_required' );
function make_postcode_required( $fields ) {
$fields['billing']['billing_postcode']['required'] = true;
return $fields;
}
—
### ✅ Step 2: Fix the "optional" label shown on the front-end using JS
Now we need to **remove the “(optional)” text** that WooCommerce adds with JavaScript on page load or country change:
add_action( 'wp_footer', 'force_required_remove_optional_label_script' );
function force_required_remove_optional_label_script() {
if ( is_checkout() ) :
?>
<script>
jQuery(function($) {
function fixPostcodeLabel() {
var label = $('#billing_postcode_field label');
label.find('.optional').remove(); // Remove "(optional)" span
}
// Run on load
fixPostcodeLabel();
// Run again after checkout updates
$('body').on('updated_checkout', function() {
fixPostcodeLabel();
});
});
</script>
<?php
endif;
}
—
### ✅ (Optional) Step 3: Also relabel it to “Eircode” if not done
add_filter( 'woocommerce_checkout_fields', 'custom_checkout_postcode_label' );
function custom_checkout_postcode_label( $fields ) {
$fields['billing']['billing_postcode']['label'] = 'Eircode';
$fields['billing']['billing_postcode']['placeholder'] = 'Enter your Eircode';
return $fields;
}
—
Now:
– ✅ Field is required
– ✅ Label shows “Eircode”
– ✅ “(optional)” is gone forever — even after country changes
Let me know if you're also using a multilingual plugin like WPML or Polylang and need this label translated!
Here’s the full updated solution:
—
### ✅ Final PHP Snippet: Make `billing_postcode` required + add `abbr.required`
add_filter( 'woocommerce_checkout_fields', 'customize_billing_postcode_field' );
function customize_billing_postcode_field( $fields ) {
$fields['billing']['billing_postcode']['label'] = 'Eircode <abbr class="required" title="required">*</abbr>';
$fields['billing']['billing_postcode']['required'] = true;
$fields['billing']['billing_postcode']['placeholder'] = 'Enter your Eircode';
return $fields;
}
—
### ✅ Remove "(optional)" via JS (if still appearing)
add_action( 'wp_footer', 'remove_optional_label_checkout' );
function remove_optional_label_checkout() {
if ( is_checkout() ) :
?>
<script>
jQuery(function($) {
function cleanupPostcodeLabel() {
var label = $('#billing_postcode_field label');
label.find('.optional').remove(); // Remove "(optional)" span if WooCommerce adds it
}
cleanupPostcodeLabel();
$('body').on('updated_checkout', function() {
cleanupPostcodeLabel();
});
});
</script>
<?php
endif;
}
—
### ✅ Result:
– ✅ Field label will show: `Eircode *` (with correct required styling)
– ✅ Asterisk will be wrapped in `*`
– ✅ The `(optional)` tag will be removed, even after AJAX checkout reload
Let me know if you want to do the same for `billing_phone` or other fields!
—
### ✅ Add this script (enqueue it or insert via `wp_footer`):
add_action( 'wp_footer', 'force_postcode_required_label_js' );
function force_postcode_required_label_js() {
if ( is_checkout() ) :
?>
<script>
jQuery(function($) {
function updatePostcodeLabel() {
var label = $('#billing_postcode_field label');
// Remove existing optional tag and old abbr
label.find('.optional, abbr.required').remove();
// Add the required asterisk
label.append(' <abbr class="required" title="required">*</abbr>');
}
// On load
updatePostcodeLabel();
// On checkout update (AJAX calls)
$('body').on('updated_checkout', function() {
updatePostcodeLabel();
});
});
</script>
<?php
endif;
}
—
### 🔍 What this does:
– Removes any existing `(optional)` text or “ to prevent duplicates
– Appends your desired asterisk: `*`
– Hooks into WooCommerce's `updated_checkout` event to ensure it sticks after country or shipping method changes
—
Let me know if you’d like to do this for multiple fields (e.g. `billing_phone`) or conditionally based on country (e.g. only when Ireland is selected).