My code:
add_action( 'woocommerce_before_checkout_form', 'fruit_field' );
function fruit_field( $checkout ) {
woocommerce_form_field( 'fruit', array(
'type' => 'select',
'required' => true,
'options' => array(
'apple' => __('Apple'),
'banana' => __('Banana'),
'watermelon' => __('Watermelon'),
'other' => __('Other'),
),
'class' => array('my-class'),
'label' => __('Best fruit?'),
), $checkout->get_value( 'fruit' ));
}
And the validation:
add_action('woocommerce_checkout_process', 'process_checkout');
function process_checkout() {
if ($_POST['fruit'] === null) {
wc_add_notice( __( 'No fruits?' ), 'error' );
}
}
After submitting the form, it always displays my custom error "No fruits?", no matter what was selected. Is $_POST['fruit']
somehow not available in the process_checkout
function?