Garath
Garath

Reputation: 121

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

Upvotes: 2

Views: 1608

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253849

For Ireland "postcode" field you need to alter the default WooCommerce country locale settings that are defined on WC_Countries get_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

Upvotes: 4

Related Questions