Created
May 9, 2024 14:42
-
-
Save dantetesta/bfe9d245731db70728cfe11b5e887df8 to your computer and use it in GitHub Desktop.
Consulta datas e mostra periodos radio jetformbuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function meu_shortcode_consultar_disponibilidade() { | |
ob_start(); // Inicia a captura de saída para permitir a utilização de echo | |
?> | |
<!-- Botão para consultar disponibilidade --> | |
<button id="botao-consultar-disponibilidade" type="button">Consultar disponibilidade</button> | |
<!-- Div para exibir o resultado --> | |
<div id="resultado-consulta"></div> | |
<!-- JavaScript para manipular o clique do botão e interagir com o AJAX --> | |
<script> | |
document.addEventListener('DOMContentLoaded', function() { | |
document.getElementById('botao-consultar-disponibilidade').addEventListener('click', function(event) { | |
event.preventDefault(); | |
var valorDataEscolhida = document.getElementById('_data-escolhida').value; | |
if (valorDataEscolhida) { | |
jQuery.ajax({ | |
url: '<?php echo admin_url('admin-ajax.php'); ?>', | |
type: 'POST', | |
dataType: 'json', | |
data: { | |
'action': 'verificar_agendamentos', | |
'data': valorDataEscolhida | |
}, | |
success: function(response) { | |
if (response.success) { | |
var output = ''; | |
if (response.data.manha < 10) { | |
output += '<label><input type="radio" name="_periodo-escolhido" value="Período da Manhã" class="radio-periodo"> Período da Manhã</label><br>'; | |
} | |
if (response.data.tarde < 10) { | |
output += '<label><input type="radio" name="_periodo-escolhido" value="Período da Tarde" class="radio-periodo"> Período da Tarde</label>'; | |
} | |
document.getElementById('resultado-consulta').innerHTML = output || 'Todos os períodos estão cheios para esta data.'; | |
// Adiciona um ouvinte a todos os rádio buttons que foram adicionados | |
Array.from(document.getElementsByClassName('radio-periodo')).forEach(function(radio) { | |
radio.addEventListener('click', function() { | |
document.querySelector('input[name="_periodo-escolhido-hf"]').value = this.value; | |
}); | |
}); | |
} else { | |
document.getElementById('resultado-consulta').innerText = 'Erro ao consultar disponibilidade.'; | |
} | |
} | |
}); | |
} | |
}); | |
}); | |
</script> | |
<?php | |
return ob_get_clean(); // Retorna o conteúdo capturado | |
} | |
add_shortcode('consultar_disponibilidade', 'meu_shortcode_consultar_disponibilidade'); | |
//_______________ | |
function verificar_agendamentos() { | |
// Garantindo que o POST 'data' foi enviado | |
if (isset($_POST['data'])) { | |
$data_escolhida = sanitize_text_field($_POST['data']); | |
$args = array( | |
'post_type' => 'agendamentos', | |
'meta_query' => array( | |
array( | |
'key' => '_data-escolhida', | |
'value' => $data_escolhida, | |
'compare' => '=' | |
) | |
), | |
'posts_per_page' => -1 // Busca todos os posts que correspondem | |
); | |
$query = new WP_Query($args); | |
$manha_count = 0; | |
$tarde_count = 0; | |
if ($query->have_posts()) { | |
while ($query->have_posts()) { | |
$query->the_post(); | |
$periodo = get_post_meta(get_the_ID(), '_periodo-escolhido', true); | |
if ($periodo === 'Período da Manhã') { | |
$manha_count++; | |
} elseif ($periodo === 'Período da Tarde') { | |
$tarde_count++; | |
} | |
} | |
} | |
// Retorna a contagem como uma resposta JSON | |
wp_send_json_success(['manha' => $manha_count, 'tarde' => $tarde_count]); | |
} else { | |
wp_send_json_error('Data não fornecida.'); | |
} | |
} | |
add_action('wp_ajax_verificar_agendamentos', 'verificar_agendamentos'); | |
add_action('wp_ajax_nopriv_verificar_agendamentos', 'verificar_agendamentos'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment