Show Only One Error Message on Checkout Field Error

How to Show Only One Error Message For WooCommerce Checkout Field?

Their are approximately 7 to 10 default field which are required on WooCommerce checkout field.
In case these all have error then their are so much error in top, you can see in screenshot below. so for removing or we can say replacing all error with only one msg , you can view in screenshot 2.

 

Example :

 

function ShowOneError( $fields, $errors ){
  // if their is any validation errors
  if( !empty( $errors->get_error_codes() ) ) {
    // remove all of Error msg
    foreach( $errors->get_error_codes() as $code ) {
      $errors->remove( $code );
    }
  // our custom Error msg
  $errors->add('validation','There is an error in filed data.');
  } 
}
add_action('woocommerce_after_checkout_validation','ShowOneError',999,2);

 

Please note, that above code removes all types of errors, not only “This Field Is Required” etc.
We can remove them conditionally . For this first we need to type of error.
Types of errors:

  • required-field
  • terms (Terms checkbox)
  • payment

 

So for removing Only “required-field” error:

 

function ShowOneError( $fields, $errors ){
  // if their is any validation errors
  if( !empty( $errors->get_error_codes() ) ) {
    // remove all of Error msg
    foreach( $errors->get_error_codes() as $code ) {
     if( $code == 'required-field') {
      $errors->remove( $code );
     }
    }
  // our custom Error msg
  $errors->add('validation','There is an error in filed data.');
  } 
}
add_action('woocommerce_after_checkout_validation','ShowOneError',999,2);

 

Related Blogs

Add Admin User in WordPress By FTP

Creating an admin user in WordPress through an FTP client is not a direct method since user accounts are managed through the WordPress database and not through files on the server. To add an admin user, you’ll need to use a different approach. Here’s how you can do it:

Request A Quote

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.