Add a custom field to checkout form in Woocommerce

I want to add a custom field to checkout form of Woocommerce. The field is showing how I want it but, the name and id attribute are wrong. Here’s my function to create my field.

// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
     $fields['billing']['billing_infos'] = array(
        'type'      => 'textarea',
        'label'     => __('Notes de la commande', 'woocommerce'),
    'placeholder'   => _x('Commentaires concernant votre commande', 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     return $fields;
}

Here’s how I call it in the form:

Read More
<?php woocommerce_form_field( $checkout->checkout_fields['billing'], $checkout->checkout_fields['billing']['billing_infos'], $checkout->get_value( 'billing_infos' ) ); ?>

When I inspect my field this is what I get:

<p class="form-row form-row-wide woocommerce-validated" id="Array_field"><label for="Array" class="">Notes de la commande</label><textarea name="Array" class="input-text " id="Array" placeholder="Commentaires concernant votre commande" rows="2" cols="5"></textarea>
                </p>

Related posts

Leave a Reply

2 comments

  1. I solved my problem. The first argument of the function I was calling was wrong.

    Here’s how I should call it:

    <?php woocommerce_form_field( 'billing_infos', $checkout->checkout_fields['billing']['billing_infos'], $checkout->get_value( 'billing_infos' ) ); ?>