简体   繁体   中英

woocommerce custom select option function

I am using woocommerce to add some custom fields.

Everything is working fine when I use the code below and place it in the functions.php file in the Wordpress theme.

woocommerce_wp_select( 
        array( 
            'id'      => '_select_option', 
            'label'   => __( 'Select an Option', 'woocommerce' ), 
            'desc_tip'    => 'true',
            'description' => __( 'Select an option', 'woocommerce' ), 
            'options' => array(

                '1'   => __( 'Option 1', 'woocommerce' ),
                '2'   => __( 'Option 2', 'woocommerce' ),
                '3'   => __( 'Option 3', 'woocommerce' ),



                )
            )
        );

Problem How do I add an ID = 1 ID = 2 ** , **ID = 3 next to each option like the code style below

<select id="_select_option">
      <option value="1" id="1"> Option 1 </option>
      <option value="2" id="2"> Option 2 </option>
      <option value="3" id="3"> Option 3 </option>
</select>

Thanks

Put below code in footer.php or you can inject this script with user wp_footer or wp enqueue script

 jQuery( "#_select_option option" ).each(function(index) { jQuery( this ).attr( "id",index ); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="_select_option"> <option value="1"> Option 1 </option> <option value="2"> Option 2 </option> <option value="3"> Option 3 </option> </select> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM