Woocommerce结帐自定义选择字段

Tan*_*ari 2 php wordpress woocommerce hook-woocommerce

我有以下功能将选择列表添加到woo-commerce结帐表单:

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' ));
Run Code Online (Sandbox Code Playgroud)

我想默认选择'NO'选项.请建议.怎么做?

Ben*_*oti 11

以下是woocommerce_form_field($ key,$ args,$ value)函数的默认参数($ args):

$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' => '',  
);
Run Code Online (Sandbox Code Playgroud)

在您的情况下,您只需要像这样修改:

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' ));
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!

  • 好酷!不要忘记将问题标记为已解决。 (2认同)