Billing fields in Woocommerce checkout page and my account page shows individual error if the required fields are empty. Well, if all fields are empty, all errors for those empty fields will be shown like:
- First name is a required field
- Last name is a required field
- Street address is a required field
and so on…
I want to display only one error if all the required fields are empty, like “ERROR: All fields are empty. Please fill in all required fields to place order.” Well I somehow solved this problem on checkout page with the code below:
add_action( 'woocommerce_after_checkout_validation', 'show_one_err', 9999, 2);
function show_one_err( $fields, $errors ){
// if any validation errors
if( !empty( $errors->get_error_codes() ) ) {
// remove all of them
foreach( $errors->get_error_codes() as $code ) {
$errors->remove( $code );
}
// add our custom one
$errors->add( 'validation', 'Please fill in all required fields to place order.' );
}
}
My problem right now is how to apply these changes in Woocommerce My Account page - billing address and also in My Account - account details tab. My sole purpose of these changes is to have a consistent error notice in all Woocommerce fields (Please see attach images below).
Checkout page
My Account - billing address
My Account - account details
To replace all fields validation errors by a unique custom one from Account Billing and Shipping Address, and also Account details, you will use the following hooked function that uses two validation hooks:
Code goes in function.php file of your active child theme (active theme). Tested and works.
Related: Set a unique validation error notice in Woocommerce checkout page