Cart

// Output the Custom field in check out
// Output the Custom field in check out
add_action( 'woocommerce_review_order_before_payment', 'checkout_radio_choice', 10,2);
function checkout_radio_choice(){
    $input_key = 'protection_choice';
    $value     = WC()->session->get($input_key);
    $options   = array(
        __("DECLINED", "woocommerce"),
        __("SIGNATURE $4.00", "woocommerce"),
        __("INSURANCE (Calculated)", "woocommerce"),
    );
 echo '<div id="checkout-radio">';
    echo '<h3><font color="red">Optional Product Protection!</h4>';
  
    echo '</div>';
    
    echo '<p class="form-row form-row-wide" id="'.$input_key.'_field"><label for="'.$input_key.'">
        <input type="radio" name="'.$input_key.'" value="" checked="checked"> '.__("I Decline", "woocommerce").'<br />
        <input type="radio" name="'.$input_key.'" value="signature"> '.__("SIGNATURE $4.00", "woocommerce").'<br />
        <input type="radio" name="'.$input_key.'" value="insurance"> '.__("INSURANCE(Calculated)", "woocommerce").'<br />
    </label></p>';
}

// The jQuery Ajax request (client side)
add_action( 'wp_footer', 'checkout_custom_jquery_script' );
function checkout_custom_jquery_script() {
    // Only checkout page
    if( is_checkout() && ! is_wc_endpoint_url() ):

    $session_key = 'protection_choice';

    // Remove "options_choice" custom WC session on load
    if( WC()->session->get($session_key) ){
        WC()->session->__unset($session_key);
    }
    // jQuery Ajax code
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof wc_checkout_params === 'undefined')
            return false;

        var a = 'input[name=<?php echo $session_key; ?>]';

        $('form.checkout').on( 'change', 'input[name=<?php echo $session_key; ?>]', function(){
            var choice = $(this).val();
            $.ajax({
                type: 'POST',
                url: wc_checkout_params.ajax_url,
                data: {
                    'action': 'get_<?php echo $session_key; ?>',
                    'the_choice': choice,
                },
                success: function (response) {
                    $(document.body).trigger('update_checkout');
                    console.log(response); // For testing, to be removed.
                }
            });
        });
    });
    </script>
    <?php
    endif;
}

// The WordPress Ajax PHP receiver (server side)
add_action( 'wp_ajax_get_protection_choice', 'get_checkout_protection_choice' );
add_action( 'wp_ajax_nopriv_get_protection_choice', 'get_checkout_protection_choice' );
function get_checkout_protection_choice() {
    $session_key = 'protection_choice';

    if ( isset($_POST['the_choice']) ){
        WC()->session->set( $session_key, esc_attr( $_POST['the_choice'] ) );
        echo $_POST['the_choice'];
    }
    die();
}

// Add a custom fee
add_action( 'woocommerce_cart_calculate_fees', 'chosen_custom_fee', 20, 1 );
function chosen_custom_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( $choice = WC()->session->get('protection_choice') ) {
        if ( "signature" === $choice ) {
            $fee = 4.00;
        }
        elseif ( "insurance" === $choice ) {
            $subtotal_excl_tax = (float) $cart->get_cart_contents_total();
            $subtotal_incl_tax = $subtotal_excl_tax + (float) $cart->get_cart_contents_tax();
            $fee = ( wc_prices_include_tax() ? $subtotal_incl_tax : $subtotal_excl_tax ) * 0.027 + 4.00;
        }

        $cart->add_fee( __("Protection", "woocommerce"), $fee );
    }
}