Skip to content

Instantly share code, notes, and snippets.

@dantetesta
Created June 17, 2023 02:54
Show Gist options
  • Save dantetesta/60131b019873a871b1ad39c63c253e6d to your computer and use it in GitHub Desktop.
Save dantetesta/60131b019873a871b1ad39c63c253e6d to your computer and use it in GitHub Desktop.
<script>
jQuery(document).ready(function() {
var taxonomia_id = jQuery('select[name="tipoproduto"]').val();
var query_title = jQuery('input[name="query"]').val();
function fetch_data() {
jQuery.ajax({
url: ajaxurl,
method: 'POST',
data: {
action: 'soma_total_produtos_taxonomia',
taxonomia_id: taxonomia_id,
query_title: query_title
},
success: function(data) {
jQuery('.totalsoma').html(data);
},
error: function(error) {
console.log(error);
}
});
}
jQuery('select[name="tipoproduto"]').change(function() {
taxonomia_id = jQuery(this).val();
fetch_data();
});
jQuery('input[name="query"]').keyup(function() {
query_title = jQuery(this).val();
fetch_data();
});
jQuery('.jet-search-filter__input-clear').click(function() {
jQuery('input[name="query"]').val('');
query_title = '';
fetch_data();
});
});
</script>
function add_ajaxurl_cdata_to_front() {
?>
<script type="text/javascript"> // <![CDATA[
ajaxurl = '<?php echo admin_url( 'admin-ajax.php'); ?>';
// ]]></script>
<?php
}
add_action( 'wp_head', 'add_ajaxurl_cdata_to_front', 1);
function soma_total_produtos() {
if (isset($_GET['elementor-preview']) || wp_doing_ajax()) {
return;
}
// Query para buscar todos os posts do tipo 'produtos'
$args = array(
'post_type' => 'produtos',
'post_status' => 'publish',
'posts_per_page' => -1
);
$query = new WP_Query($args);
// Variável para armazenar a soma total
$soma_total = 0.0;
// Percorre todos os posts encontrados
while($query->have_posts()) : $query->the_post();
// Obtém o valor do metafield e adiciona à soma total
$valor = get_post_meta(get_the_ID(), '_valor', true);
$soma_total += floatval($valor);
endwhile;
// Retorna a soma total formatada em reais do Brasil
return '<span class="totalsoma">R$ ' . number_format($soma_total, 2, ',', '.') . '</span>';
}
add_shortcode('soma_total', 'soma_total_produtos');
function soma_total_produtos_taxonomia() {
$taxonomia_id = $_POST['taxonomia_id'];
$query_title = $_POST['query_title'];
$args = array(
'post_type' => 'produtos',
'post_status' => 'publish',
'posts_per_page' => -1
);
// Se o título não estiver vazio, adicione a consulta do título
if(!empty($query_title)) {
$args['s'] = $query_title;
}
// Se o ID da taxonomia não estiver vazio, adicione a consulta da taxonomia
if(!empty($taxonomia_id)) {
$args['tax_query'] = array(
array(
'taxonomy' => 'tipoproduto',
'field' => 'term_id',
'terms' => $taxonomia_id,
),
);
}
$query = new WP_Query($args);
$soma_total = 0.0;
while($query->have_posts()) : $query->the_post();
$valor = get_post_meta(get_the_ID(), '_valor', true);
$soma_total += floatval($valor);
endwhile;
echo '<span class="totalsoma">R$ ' . number_format($soma_total, 2, ',', '.') . '</span>';
wp_die();
}
add_action('wp_ajax_soma_total_produtos_taxonomia', 'soma_total_produtos_taxonomia');
add_action('wp_ajax_nopriv_soma_total_produtos_taxonomia', 'soma_total_produtos_taxonomia');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment