Reputation: 83
I want to show postcode/zip field even for the countries that do not use postcodes/zip on WooCommerce checkout page.
WooCommerce hides postcode/zip field by default for countries that don't use them.
I have used following filter in theme functions.php
but it doesn't work.
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
function custom_override_default_address_fields( $address_fields ) {
$address_fields['postcode']['hidden'] = false;
return $address_fields;
}
How can I override this behavior?
Upvotes: 3
Views: 827
Reputation: 1
Want to share a code I came up with to solve the zip problem for UAE and some other countries with no zip codes:
function solution_to_countries_with_no_zipcodes($package_destination_country) {
$countries_with_no_postcodes = array('AE', 'AF', 'AG', 'AI', 'AL', 'AN', 'AO', 'AW', 'BB', 'BF', 'BH', 'BI', 'BJ', 'BM', 'BO', 'BS', 'BT', 'BW', 'BZ', 'CD', 'CF', 'CG', 'CI', 'CK', 'CL', 'CM', 'CO', 'CR', 'CV', 'DJ', 'DM', 'DO', 'EC', 'EG', 'ER', 'ET', 'FJ', 'FK', 'GA', 'GD', 'GH', 'GI', 'GM', 'GN', 'GQ', 'GT', 'GW', 'GY', 'HK', 'HN', 'HT', 'IE', 'IQ', 'IR', 'JM', 'JO', 'KE', 'KH', 'KI', 'KM', 'KN', 'KP', 'KW', 'KY', 'LA', 'LB', 'LC', 'LK', 'LR', 'LS', 'LY', 'ML', 'MM', 'MO', 'MR', 'MS', 'MT', 'MU', 'MW', 'MZ', 'NA', 'NE', 'NG', 'NI', 'NP', 'NR', 'NU', 'OM', 'PA', 'PE', 'PF', 'PY', 'QA', 'RW', 'SA', 'SB', 'SC', 'SD', 'SL', 'SN', 'SO', 'SR', 'SS', 'ST', 'SV', 'SY', 'TC', 'TD', 'TG', 'TL', 'TO', 'TT', 'TV', 'TZ', 'UG', 'UY', 'VC', 'VE', 'VG', 'VN', 'VU', 'WS', 'XA', 'XB', 'XC', 'XE', 'XL', 'XM', 'XN', 'XS', 'YE', 'ZM', 'ZW');
return in_array($package_destination_country, $countries_with_no_postcodes);
}
// Remove postcode validation and the field for specific countries
add_filter('woocommerce_checkout_fields', function($fields) {
$package_destination_country = WC()->customer->get_shipping_country();
if (solution_to_countries_with_no_zipcodes($package_destination_country)) {
// Remove postcode field from billing and shipping fields
unset($fields['billing']['billing_postcode']);
unset($fields['shipping']['shipping_postcode']);
}
return $fields;
}, 30);
// Disable postcode validation for specific countries
add_filter('woocommerce_default_address_fields', function($fields) {
$package_destination_country = WC()->customer->get_shipping_country();
if (solution_to_countries_with_no_zipcodes($package_destination_country)) {
if (isset($fields['postcode'])) {
$fields['postcode']['required'] = false;
}
}
return $fields;
}, 30);
// CSS to hide postcode fields for specific countries
add_action('wp_footer', function() {
$package_destination_country = WC()->customer->get_shipping_country();
$countries_with_no_postcodes = array('AE', 'AF', 'AG', 'AI', 'AL', 'AN', 'AO', 'AW', 'BB', 'BF', 'BH', 'BI', 'BJ', 'BM', 'BO', 'BS', 'BT', 'BW', 'BZ', 'CD', 'CF', 'CG', 'CI', 'CK', 'CL', 'CM', 'CO', 'CR', 'CV', 'DJ', 'DM', 'DO', 'EC', 'EG', 'ER', 'ET', 'FJ', 'FK', 'GA', 'GD', 'GH', 'GI', 'GM', 'GN', 'GQ', 'GT', 'GW', 'GY', 'HK', 'HN', 'HT', 'IE', 'IQ', 'IR', 'JM', 'JO', 'KE', 'KH', 'KI', 'KM', 'KN', 'KP', 'KW', 'KY', 'LA', 'LB', 'LC', 'LK', 'LR', 'LS', 'LY', 'ML', 'MM', 'MO', 'MR', 'MS', 'MT', 'MU', 'MW', 'MZ', 'NA', 'NE', 'NG', 'NI', 'NP', 'NR', 'NU', 'OM', 'PA', 'PE', 'PF', 'PY', 'QA', 'RW', 'SA', 'SB', 'SC', 'SD', 'SL', 'SN', 'SO', 'SR', 'SS', 'ST', 'SV', 'SY', 'TC', 'TD', 'TG', 'TL', 'TO', 'TT', 'TV', 'TZ', 'UG', 'UY', 'VC', 'VE', 'VG', 'VN', 'VU', 'WS', 'XA', 'XB', 'XC', 'XE', 'XL', 'XM', 'XN', 'XS', 'YE', 'ZM', 'ZW');
if (in_array($package_destination_country, $countries_with_no_postcodes)) {
echo '<style>
#billing_postcode_field, #shipping_postcode_field {
display: none !important;
}
</style>';
}
});
Upvotes: 0
Reputation: 29614
You can use the woocommerce_get_country_locale
filter hook, to unhide this by default for all countries.
So you get:
function filter_woocommerce_get_country_locale( $country_locale ) {
// Loop through
foreach( $country_locale as $key => $locale ) {
// Isset
if ( isset ( $locale['postcode']['hidden'] ) ) {
// When true
if ( $locale['postcode']['hidden'] == true ) {
// Set to false
$country_locale[$key]['postcode']['hidden'] = false;
}
}
}
return $country_locale;
}
add_filter( 'woocommerce_get_country_locale', 'filter_woocommerce_get_country_locale', 10, 1 );
Upvotes: 2