Get MetaBoxes from a product or post in jetformbuilder, and use them for a calculated field

Juan Armando Ferrer Sanchez
Free
Get MetaBoxes from a product or post in jetformbuilder, and use them for a calculated field
Advanced Customizations
Description :

A cotinuacion share a small solution that I found apartir of several problems to obtain metacampos of a product woocomcerce or a post, where they managed to get the necessary data but these did not work in the calculated fields, digging a little discovered that did not take them because it did not detect the user insertion event, so I created a code that emulates that event and thus worked correctly

Solution :

function get_tour_prices( $tour_id ) {
$precio_adulto = get_post_meta( $tour_id, 'precio-adultos', true );
$precio_nino = get_post_meta( $tour_id, 'precio-ninos', true );

$prices = array(
'adulto' => ($precio_adulto) ? $precio_adulto : 'Error: No se pudo obtener el precio para adultos.',
'nino'   => ($precio_nino) ? $precio_nino : 'Error: No se pudo obtener el precio para niños.'
);
return $prices;
}

function fetch_tour_prices() {
if( isset($_POST['tour_id']) ) {
$tour_id = intval($_POST['tour_id']);
$prices = get_tour_prices( $tour_id );

$diagnostic_info = array(
'tour_id_sent' => $_POST['tour_id'],
'tour_id_intval' => $tour_id,
'all_meta_data' => get_post_meta( $tour_id )
);

wp_send_json_success( array('prices' => $prices, 'diagnostic' => $diagnostic_info) );
} else {
wp_send_json_error( 'No tour ID provided' );
}
}

add_action( 'wp_ajax_fetch_tour_prices', 'fetch_tour_prices' );
add_action( 'wp_ajax_nopriv_fetch_tour_prices', 'fetch_tour_prices' );

function enqueue_tour_price_script() {
wp_enqueue_script('jquery');
?>

jQuery(document).ready(function($) {
function fetchAndDisplayPrices() {
// $('#precio_adulto_display, #precio_nino_display').text('Cargando...');
var tour_id = $('#select_tour').find('option:selected').val();

$.ajax({
url: '',
type: 'POST',
data: {
action: 'fetch_tour_prices',
tour_id: tour_id
},
success: function(response) {
console.log(response);
// Validación para asegurarse de que los valores sean números
var adultosPrecio = parseFloat(response.data.prices.adulto);
var ninosPrecio = parseFloat(response.data.prices.nino);

// Tu código existente...

if (!isNaN(adultosPrecio)) {
document.getElementById('adultos_precio_tour').value = adultosPrecio;
var event = new Event('input', {
bubbles: true,
cancelable: true,
});
document.getElementById('adultos_precio_tour').dispatchEvent(event);
} else {
console.error('El valor de adultosPrecio no es un número válido');
}

if (!isNaN(ninosPrecio)) {
document.getElementById('ninos_precio_tour').value = ninosPrecio;
var event = new Event('input', {
bubbles: true,
cancelable: true,
});
document.getElementById('ninos_precio_tour').dispatchEvent(event);
} else {
console.error('El valor de ninosPrecio no es un número válido');
}

 

 

 

 

}
});
}

fetchAndDisplayPrices();

$('#select_tour').change(fetchAndDisplayPrices);
});

<?php
}
add_action( 'wp_footer', 'enqueue_tour_price_script' );

Did you want to see this content ?
Scroll to Top