Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 510
aliencity Avatar asked Nov 23 '25 11:11

aliencity


1 Answers

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 );
like image 162
7uc1f3r Avatar answered Nov 26 '25 11:11

7uc1f3r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!