I’m having a problem, a need to apply a filter when user changes the selection of an input radio, I did this code, but it only works if I reload the page, what can I do to avoid reload and the page charge the filter whithout reloading?
` add_action( ‘wp_footer’, ‘wpmc_set_session_on_button_click’ );
function wpmc_set_session_on_button_click() {
session_start();
$array_selectores = $_SESSION['array_selectores'];
$json = json_encode($array_selectores);
?>
<script>
document.querySelectorAll('input[name="horario-liga"]').forEach(function(input) {
input.addEventListener("change", function(e) {
var jsArray = <?php echo $json; ?>;
var horarioLigaValue = document.querySelector('input[name="horario-liga"]:checked').value;
var data = {
action: 'wpmc_set_session',
dto_manana: 'dto'
};
var data2 = {
action: 'wpmc_set_session',
dto_manana: 'no'
};
if (jsArray.includes(horarioLigaValue)) {
jQuery.post(
'<?php echo admin_url('admin-ajax.php'); ?>',
data,
function(response) {
console.log(response);
//console.log('dto');
}
);
}
else {
jQuery.post(
'<?php echo admin_url('admin-ajax.php'); ?>',
data2,
function(response) {
console.log(response);
//console.log('no');
}
);
}
});
});
</script>
<?php
}
// Manejador de petición AJAX para establecer la variable de sesión
add_action( 'wp_ajax_wpmc_set_session', 'wpmc_ajax_set_session' );
add_action( 'wp_ajax_nopriv_wpmc_set_session', 'wpmc_ajax_set_session' );
function wpmc_ajax_set_session() {
error_log(print_r($_POST, true));
echo $_POST['dto_manana'];
WC()->session->set( 'dto_manana', $_POST['dto_manana'] );
wp_die();
}
add_action( 'woocommerce_review_order_before_payment', 'detectar_radio_button_seleccionado' );
function detectar_radio_button_seleccionado() {
$dto_manana = WC()->session->get('dto_manana');
if (isset($dto_manana)) {
echo $dto_manana;
}
}
add_filter('woocommerce_cart_calculate_fees', 'add_recurring_postage_fees',10,1);
function add_recurring_postage_fees( $cart ) {
$dto_manana = WC()->session->get('dto_manana');
if ( (! empty( $cart->recurring_cart_key )) && ($dto_manana == 'dto') ) {
$cart->add_fee( 'Postage', 5 );
}
}`
If I go to checkout page I get the value ‘dto’ or ‘no’ but in order review only show the value of the last session, not the actual. I need to apply add_filter(‘woocommerce_cart_calculate_fees’, ‘add_recurring_postage_fees’,10,1); when I select in horario-liga any value include in the array