cwcfp_word_fee_length

The cwcfp_word_fee_length filter allows you to modify the length of a text string based on custom rules that you can set in your own code.

By default, the filter will return a value that is equal to count( explode( ' ', trim( $customer_input ) ) );

The filter is passed four parameters:

  • $customer_input_word_length: This is equal to the value that count( explode( ' ', trim( $customer_input ) ) ); would provide.
  • $customer_input: The value that the customer has entered into the text box or text area field.
  • $field_id: The conditional field's ID number
  • $i: The number that this field has repeated. If there are three of the same field shown on the checkout page, this value could be 1, 2, or 3.

If for some reason the $customer_input_word_length is not generating the correct count for your purposes, you can use this filter to apply some other logic to count the number of letters in the customer's input.

For example, by default this will count special characters as a word (i.e. Jack & Jill would be counted as three words). If you want to remove any special characters from the string before counting the number of words, you can do something like this:

	add_filter( 'cwcfp_word_fee_length', 'no_special_characters_in_fee', 10, 4 );
	function no_special_characters_in_fee( $customer_input_word_length, $customer_input, $field_id, $i ) {
		$new_str = str_replace( '&', '', $customer_input); //remove all ampersands (&) from a string
		$customer_input_word_length = count( explode( ' ', trim( $new_str ) ) ); // get the number of words in the new string
		return $customer_input_word_length; // return the length of the new string
	}

You may also only want to do this when $field_id = 7, which you can do as well.

	add_filter( 'cwcfp_word_fee_length', 'no_special_characters_in_fee', 10, 4 );
	function no_special_characters_in_fee( $customer_input_word_length, $customer_input, $field_id, $i ) {
		if ( 7 == $field_id ) {
			$new_str = str_replace( '&', '', $customer_input); //remove all ampersands (&) from a string
			$customer_input_word_length = count( explode( ' ', trim( $new_str ) ) ); // get the number of words in the new string
		}
		return $customer_input_word_length; // return the length of the new string, or the original string if this is not $field_id 7
	}