Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Saravana Kumar K

    (@mycholan)

    Hi, you could do something like this

    add_filter( 'woocommerce_add_to_cart_validation', 'validate_field', 10, 2 );
    function validate_field( $passed, $pid = null ) {
    	$res = true;
    	if( isset( $pid ) ) {
    		$all_fields = apply_filters( 'wccpf/load/all_fields', $pid );
    		foreach ( $all_fields as $fields ) {
    			foreach ( $fields as $field ) {
    				if( $field["name"] == "your_field_name" && isset( $_REQUEST[ $field["name"] ] ) ) {
    					$val = $_REQUEST[ $field["name"] ];
    					if( $val != "M" || $val != "F" ) {
    						$res = false;
    					}
    				}
    			}
    			if( !$res ) {
    				wc_add_notice( 'Validation Failed', 'error' );
    			}
    		}
    	}
    	return $res;
    }
    Thread Starter djuniverss

    (@djuniverss)

    Thank you for your quick reply.
    That works perfectly!!

    It could be a useful feature to included in the future.

    Cheers

    Thread Starter djuniverss

    (@djuniverss)

    The validation code works but when a field fails validation, all input fields get cleared.

    The user will have to re-enter all the input values again in order to add to cart.

    Is there a way around this?
    I have 10 input fields making it unpleasant for the user experience to re-enter.

    Plugin Author Saravana Kumar K

    (@mycholan)

    Hi, this option is under progress, will be available with my next release.

    Thread Starter djuniverss

    (@djuniverss)

    Thanks! much appreciated!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Validate Input Fields’ is closed to new replies.