Required custom WooCommerce checkout fields don’t validate entered value

I’m adding WooCommerce custom checkout fields in a Storefront child theme functions.php file.
They have a “required” attribute.
The aim is to have these fields appear at the top of the page, before the billing fields.

when clicking the submit button to proceed to payment, I’m getting the required custom field validation error (‘please fill in your name’) and can’t continue with payment, even though filling the field with valid data.

Any clue how to fix this or where to start debugging ?

here is the code in functions.php:

add_action( 'woocommerce_before_checkout_form', 'my_custom_checkout_fields' );

function my_custom_checkout_fields( $checkout ) {

    echo '<div id="my_custom_checkout_field" class="col4-set"><h2>' . __('name') . '</h2>';

    woocommerce_form_field( 'developer_name', array(
        'type'          => 'text',
        'class'         => array('developer_name-class form-row form-row-first'),
        'label'         => __('name'),
        'placeholder'   => __('fill in your name'), 
        'required'      => true,
        ), $checkout->get_value( 'developer_name' ));                               

    echo '</div>';

}

/**
 * Process the checkout
 */
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
if ( ! $_POST['developer_name'] )
        wc_add_notice( __( 'please fill in your name' ), 'error' );  
}

I tried the following but none of them helped:

1. changing:

if ( ! $_POST['developer_name'] ) 

to

if ( empty( $_POST['developer_name']) )

2. changing the trigger from:

 add_action( 'woocommerce_before_checkout_form', 'my_custom_checkout_fields' ); 

to

add_action( 'woocommerce_after_checkout_form', 'my_custom_checkout_fields' );

3. updating to latest Woocomerce 3.0.5 version

I’m running WordPress 4.7.4
additional related active plugins:
Uni CPO – WooCommerce Options and Price Calculation Formulas

Read more here: Required custom WooCommerce checkout fields don’t validate entered value

Leave a Reply

Your email address will not be published. Required fields are marked *