Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce require fields

Below is the code I am using to make states field mandatory for all places but for specific countries like Germany its still not mandatory. I want to make it mandatory for all.

add_filter( 'woocommerce_checkout_fields', 'custom_override_default_address_fields' );
function custom_override_default_address_fields($fields){
        $fields['billing']['state']['required'] = true;
        $fields['shipping']['state']['required'] = true;
    }
    return $fields;
}

1 Answers

I figured out WordPress itself removes the required state field in many countries (like Kuwait) and it can not be made required using,

   $fields['billing']['state']['required'] = true;
   $fields['shipping']['state']['required'] = true;

What I did is, I checked the size of of the input value in the state drop-down (when user presses order button) and if the value was empty, I showed an error.

function my_custom_checkout_field_process() {
    // You can make your own control here
    if ( ! $_POST[ 'billing_state' ] )
    {

        wc_add_notice( __('PLEASE SELECT A STATE' ), 'error' );
    }
}

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!