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;
}
Thank you for your quick reply.
That works perfectly!!
It could be a useful feature to included in the future.
Cheers
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.
Hi, this option is under progress, will be available with my next release.
Thanks! much appreciated!