Woocommerce checkout custom select field

Tanmay Maheshwari picture Tanmay Maheshwari · Nov 8, 2016 · Viewed 8.5k times · Source

I have the following function adding a select list to the woo-commerce checkout form:

woocommerce_form_field( 'airport_pickup', array(
        'type'          => 'select',
        'class'         => array('airport_pickup form-row-wide'),
        'label'         => __('Would you like us to arrange transportation from the airport to your starting hotel?'),
        'required'    => true,
        'options'     => array(
        'Y' => __('YES'),
        'N' => __('NO')
        )), $checkout->get_value( 'airport_pickup' ));

I would like to make the 'NO' option selected by default.Please suggest. How to do that?

Answer

Benoti picture Benoti · Nov 8, 2016

Here is the default arguments ($args) for the woocommerce_form_field($key, $args, $value) function :

$defaults = array( 
    'type' => 'text',  
    'label' => '',  
    'description' => '',  
    'placeholder' => '',  
    'maxlength' => false,  
    'required' => false,  
    'autocomplete' => false,  
    'id' => $key,  
    'class' => array(),  
    'label_class' => array(),  
    'input_class' => array(),  
    'return' => false,  
    'options' => array(),  
    'custom_attributes' => array(),  
    'validate' => array(),  
    'default' => '',  
);

In your case you just need to modify like this:

woocommerce_form_field( 'airport_pickup', array(
    'type'          => 'select',
    'class'         => array('airport_pickup form-row-wide'),
    'label'         => __('Would you like us to arrange transportation from the airport to your starting hotel?'),
    'required'    => true,
    'options'     => array(
                      'Y' => __('YES'),
                      'N' => __('NO')
    ),
    'default' => 'N'), 
    $checkout->get_value( 'airport_pickup' ));

Hope it helps!