Ad

Make Postcode Checkout Field Required For Ireland In WooCommerce

In WooCommerce checkout page I'm looking to change the postcode field (Irish Eircode) to being a required field, by default this is an optional field when the selected country is Ireland. I just want to know is there a way to change this to required and to have a little red star beside the "Eircode" to signify that it is a required field.

The screenshot below should give a clear idea of what I mean:

enter image description here

Ad

Answer

For Ireland "postcode" field you need to alter the default WooCommerce country locale settings that are defined on WC_Countriesget_country_locale() method as follow (on lines 950 to 958):

'IE' => array(
    'postcode' => array(
        'required' => false,
        'label'    => __( 'Eircode', 'woocommerce' ),
    ),
    'state'    => array(
        'label' => __( 'County', 'woocommerce' ),
    ),
),

So using the following hooked function you can make the postcode field required:

add_filter( 'woocommerce_get_country_locale', 'ireland_country_locale_change', 10, 1 );
function ireland_country_locale_change( $locale ) {
    $locale['IE']['postcode']['required'] = true;

    return $locale;
}

Code goes in functions.php file of your active child theme (active theme). Tested and works.

Similar answer: Make state checkout field required for a specific country in Woocommerce

Ad
source: stackoverflow.com
Ad