woocommerce_add_to_cart_validation
passes the product id as the second argument to the callback function, so you can access it like this:
function custom_fields_validation( $passed_validation, $product_id ) {
if( isRecurring( $product_id ) ) {
if ( empty( $_POST['inputday']) ){
wc_add_notice( __( 'Please select field 1;', 'woocommerce' ), 'error' );
return false;
}
if ( empty( $_POST['inputtime'] )){
wc_add_notice( __( 'Please select field 2;', 'woocommerce' ), 'error' );
return false;
}
}
return $passed_validation;
}
add_filter( 'woocommerce_add_to_cart_validation', 'custom_fields_validation', 10, 2 );
Also, note that woocommerce_add_to_cart_validation
is a filter, not an action, so I’ve used apply_filters
instead of add_action
. I also fixed a syntax error, where you had an extra parentheses after 'custom_fields_validation'
.
I also updated the function to return the current validation state. Otherwise you could be returning true
even if a previous validation had failed.