In functions.php

/**
 * Bedrijf / particulier radio button in checkout
 */

add_action( 'woocommerce_before_checkout_billing_form', 'misha_select_field', 10 );
function misha_select_field( $checkout ){

	// you can also add some custom HTML here

	woocommerce_form_field( 'zakelijkparticulier', array(
		'type'          => 'radio', // text, textarea, select, radio, checkbox, password, about custom validation a little later
		'required'	=> true, // actually this parameter just adds "*" to the field
		'class'         => array('misha-field', 'form-row-wide'), // array only, read more about classes and styling in the previous step
		'label'         => 'Besteld u zakelijk of particulier?',
		'label_class'   => 'inputzp', // sometimes you need to customize labels, both string and arrays are supported
		'options'	=> array( // options for <select> or <input type="radio" />
			'zakelijk'	=> 'Zakelijk', // 'value'=>'Name'
			'particulier'	=> 'Particulier'
			)
		), $checkout->get_value( 'zakelijkparticulier' ) );


	//echo '<div id="content"></div>';

}
add_action( 'woocommerce_checkout_process', 'my_custom_checkout_field_process' );
function my_custom_checkout_field_process( ) {
				global $woocommerce;
				// Check if set, if its not set add an error.
				if ( !$_POST[ 'zakelijkparticulier' ] )
							wc_add_notice( __( '<strong>Soort bestelling</strong> Geef aan of u zakelijk of particulier besteld.' ), 'error' );
}
add_action( 'woocommerce_checkout_update_order_meta', 'misha_save_what_we_added' );
// save field values
function misha_save_what_we_added( $order_id ){

	if( !empty( $_POST['zakelijkparticulier'] ) )
		update_post_meta( $order_id, 'zakelijkparticulier', sanitize_text_field( $_POST['zakelijkparticulier'] ) );


}

In header.php

<style>
    #zakelijkparticulier_field > label {
      display: block;
    }
    .radio {
      display: inline-block!important;
      margin-right: 10px;
    }
    input.input-radio  {
      margin-right: 10px;
    }

    </style>
    <script type="text/javascript">

        jQuery(document).ready( function($) {
          $(".input-radio").change(function () {
            if ($("#zakelijkparticulier_zakelijk").prop("checked")) {
                    $('#billing_company_field').show();
                    $('#woocommerce_eu_vat_number').show();
                    $("#billing_company_field").css({ display: "block" });
                    $("#woocommerce_eu_vat_number").css({ display: "block" });
                }
                else {
                    $('#billing_company_field').hide();
                    $('#woocommerce_eu_vat_number').hide();
                    $("#billing_company_field").css({ display: "none" });
                    $("#woocommerce_eu_vat_number").css({ display: "none" });
                }
            });

        });
    </script>

 

Geef een reactie 0