Woocommerce checkout custom select field

12,053

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!

Share:
12,053
Tanmay Maheshwari
Author by

Tanmay Maheshwari

Updated on June 28, 2022

Comments

  • Tanmay Maheshwari
    Tanmay Maheshwari almost 2 years

    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?