cwcfp_custom_field_validation

The cwcfp_custom_field_validation filter should return either true or false. Anything else is not valid.

The arguments passed to this filter include:

  • $validates: Default is true
  • $customer_input: The value entered by the customer at checkout.
  • $field_id: The ID of the field found in the Conditional Field's settings.
  • $repeat: The nth time this field has been displayed on the checkout page.

Your custom validation can look like this:

add_filter( 'cwcfp_custom_field_validation', 'your_custom_function', 10, 4 );
function your_custom_function( $validates, $customer_input, $field_id, $repeat ){
	// do something to check if $customer_input matches what you want it to be for $field_id and/or $repeat
	switch( $field_id ){
		// Custom validation for $field_id 1
		case 1:
			// Check to see if $customer_input is at least 5 characters long
			if( strlen( $customer_input ) < 5 ){
				$validates = false;
			}
			break;
		// Custom validation for $field_id 2
		case 2:
			// Check to make sure $customer_input is a date that is earlier than January 1, 2000
			$date_check = strtotime( '2000/1/1' );
			if( $date_check < strtotime( $customer_input) ){
				$validates = false;
			}
			break;
		// Default validation of all other fields, which can be left blank if no further validation is necessary.
		// Custom validation for $field_id 3
		case 3:
			// Check to make sure $customer_input contains any prohibited words.
			$prohibited_words = array( 'foo', 'bar' );
			foreach ( $prohibited_words as $word ) {
				if( strpos( $customer_input, $word ) ){
					$validates = false;
				}
			}
			break;

		default:
			break;
	}
	return $validates;
}

Note: Returning false will trigger the field's "Required Field Error Message" to be displayed to the customer on the checkout page. This means that the field should be required for this to work, otherwise no validation checks will be triggered.