Set city as required if country is Singapore

I would like to set city checkout field as required if country is Singapore (if we only sell to Singapore, the city field is obligatorily optional).

This is what I did so the user can only choose Singapore as the city, but there still is the optional label, which I want to become required.

add_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields', 10, 1 );
function custom_checkout_fields( $fields )
{
    $fields['billing']['billing_city']['type'] = 'select';
    $fields['billing']['billing_city']['options'] = array('Singapore' = 'Singapore');

    $fields['shipping']['shipping_city']['type'] = 'select';
    $fields['shipping']['shipping_city']['options'] = array('Singapore' = 'Singapore');

    return $fields;
}

I tried $fields['billing']['billing_city']['required'] = true; but it doesn't work...

How can I change optional to required?

Topic woocommerce-offtopic Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.