Add payment type dropdown to checkout form

 

// Add a new checkout field
add_filter( 'woocommerce_checkout_fields', 'kia_filter_checkout_fields' );

function kia_filter_checkout_fields($fields){
    $fields['extra_fields'] = array(
            'payment_type' => array(
                'type' => 'select',
				'class' => array( 'form-row-first' ),
                'options' => array( ''    		=> __( 'Please select' ), 'Credit Card' => __( 'Credit Card' ), 'Net 30' => __( 'Net 30' ) ),
                'required'      => true,
				'default' => '',
                'label' => __( 'Payment Type' )
                )
            );

    return $fields;
}

//-----------------------
// display the extra field on the checkout form
add_action( 'woocommerce_after_checkout_billing_form' ,'kia_extra_checkout_fields' );

function kia_extra_checkout_fields(){ 

    $checkout = WC()->checkout(); ?>
	
    <?php 
    // because of this foreach, everything added to the array in the previous function will display automagically
    foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : ?>

            <?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>

        <?php endforeach; ?>

<?php }

//-----------------------
// Saving the data
add_action( 'woocommerce_checkout_create_order', 'kia_save_extra_checkout_fields', 10, 2 );

function kia_save_extra_checkout_fields( $order, $data ){

    // don't forget appropriate sanitization if you are using a different field type
    if( isset( $data['some_field'] ) ) {
        $order->update_meta_data( '_some_field', sanitize_text_field( $data['some_field'] ) );
    }
    if( isset( $data['payment_type'] ) && in_array( $data['payment_type'], array( 'Credit Card', 'Net 30' ) ) ) {
        $order->update_meta_data( '_payment_type', $data['payment_type'] );
    } 
}

//-----------------------
// display the extra data in the order admin panel
add_action( 'woocommerce_admin_order_data_after_billing_address', 'kia_display_order_data_in_admin' );

function kia_display_order_data_in_admin( $order ){  ?>
        <?php 
            echo '<p><strong>' . __( 'Payment Type' ) . ':</strong>' . $order->get_meta( '_payment_type' ) . '</p>'; ?>
<?php }