phpwordpresswoocommercecheckoutpostal-code

How to prevent postcode/zip field from getting hidden for some countries in WooCommerce


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?


Solution

  • 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 );