I have a custom code for custom fees on woocommerce checkout. Its for extra insurance of the package. But this "insurance" field is always visible in "checkout review" even if i clik "no insurance" radio button - by default there is checked "no insurance". How would i make this work so that when the "no insurance" radio button is checked there would be no insurance text on "checkout review" the insurance text and its price would be only visible if i click "i want insurance"?
no_option defines no insurance option_1 defines this insurance and fee
// Part 1
// Display Radio Buttons
// Uses woocommerce_form_field()
add_action( 'woocommerce_after_order_notes','bbloomer_checkout_radio_choice' );
function bbloomer_checkout_radio_choice() {
$chosen = WC()->session->get('radio_chosen');
$chosen = empty( $chosen ) ? WC()->checkout->get_value('radio_choice') : $chosen;
$chosen = empty( $chosen ) ? 'no_option' : $chosen;
$args = array(
'type' => 'radio',
'class' => array( 'form-row-wide' ),
'options' => array(
'no_option' => 'Brez zavarovanja paketa',
'option_1' => 'Zavaruj svoj paket za samo 0,99€ (<a href="test"_blank">VEČ</a>)',
),
'default' => $chosen
);
echo '<div id="checkout-radio">';
echo '<img src="https://www.kupko.si/wp-
content/uploads/2019/07/shield.png" alt="Zavarovanje"> <h3>Zavaruj svoj paket</h3>';
woocommerce_form_field( 'radio_choice', $args, $chosen );
echo '</div>';
}
// Part 2
// Add Fee and Calculate Total
// Based on session's "radio_chosen"
#2 Calculate New Total
add_action( 'woocommerce_cart_calculate_fees','bbloomer_checkout_radio_choice_fee', 20, 1
);
function bbloomer_checkout_radio_choice_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$radio = WC()->session->get( 'radio_chosen' );
if ( "option_1" == $radio ) {
$fee = 0.99;
}
$cart->add_fee( __('Zavarovanje paketa', 'woocommerce'), $fee );
}