Woocommerce checkout custom select field

纵饮孤独 提交于 2021-01-21 10:12:05

问题


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?


回答1:


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!



来源:https://stackoverflow.com/questions/40480587/woocommerce-checkout-custom-select-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!