skip to Main Content

I use a php function in wordpress to add a custom selectfield to my woocommerce checkout.
I want to make the first option (named blank) unselectable. How can i achieve this?


     woocommerce_form_field( 'cstm_leeftijd'.$i, array(
            'type'          => 'select',
            'class'         => array('my-field-class form-row-first'),
            'label'         => __('Leeftijd bij start Summer Camp'),
            'required' => true,
            'options'       => array(
                 'blank'     => __( 'Selecteer', 'njengah' ),
                 '4'         => __( '4', 'njengah' ),
                 '5'         => __( '5', 'njengah' ),
                 '6'         => __( '6', 'njengah' ),
                 '7'         => __( '7', 'njengah' ),
                 '8'         => __( '8', 'njengah' ),
                 '9'         => __( '9', 'njengah' ),
                 '10'        => __( '10', 'njengah' ),
                 '11'        => __( '11', 'njengah' )
        )), $checkout->get_value( 'cstm_leeftijd'.$i ));

2

Answers


  1. you can use a placholder variable like this:

     woocommerce_form_field( 'cstm_leeftijd'.$i, array(
                'type'          => 'select',
                'class'         => array('my-field-class form-row-first'),
                'label'         => __('Leeftijd bij start Summer Camp'),
                'required' => true,
                'placeholder'   => __( 'Selecteer', 'njengah' ),
                'options'       => array(
                     '4'         => __( '4', 'njengah' ),
                     '5'         => __( '5', 'njengah' ),
                     '6'         => __( '6', 'njengah' ),
                     '7'         => __( '7', 'njengah' ),
                     '8'         => __( '8', 'njengah' ),
                     '9'         => __( '9', 'njengah' ),
                     '10'        => __( '10', 'njengah' ),
                     '11'        => __( '11', 'njengah' )
            )), $checkout->get_value( 'cstm_leeftijd'.$i ));
    
    Login or Signup to reply.
  2. Passing disabled attribute to the first option would do the trick.
    You can check the woocommerce_form_field() function mechanism to check in which way it supports passing the disabled attribute.

    You can even do this by jquery. Through your custom class my-field-class

    $(".my-field-class select option:first").attr("disabled", "true"); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search