Last active
June 24, 2023 09:33
-
-
Save FrancoStino/ce21d8d537b1e4230de233a5f1730bfd to your computer and use it in GitHub Desktop.
Insert suffix Jquery if tax is exempt or scorporate IVA on price - Woocommerce UniCPO Builder
This file contains hidden or 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
<?php | |
/** | |
* Insert suffix Jquery if tax is exempt Woocommerce UniCPO Builder | |
*/ | |
add_filter( 'wp_footer', 'bbloomer_price_free_zero'); | |
function bbloomer_price_free_zero() { | |
if ( ! is_product() ) { | |
return; | |
} | |
global $product; | |
$product_id = $product->get_id(); | |
if ( has_term( 'pavimenti', 'product_cat', $product_id ) ) { | |
?> | |
<script> | |
jQuery(function($){ | |
// Click automatico pulsante "Calcola" | |
$(".uni-cpo-calculate-btn").trigger('click'); | |
// Se selezionata la dimensione click automatico pulsante "Calcola" | |
$('.uni_cpo_dimensione-field').click(function() { | |
$('.uni-cpo-calculate-btn').trigger('click'); | |
}); | |
// Inserimento suffisso automatico iva al 22% (Woocommerce IVA esente) | |
$('.woocommerce-Price-amount bdi').after(' <small>IVA Inclusa al 22%</small>'); | |
// Condizione posa inserimento testo suffisso automatico | |
$('.uni_cpo_scelta_posa-field').click(function() { | |
if ( $(this).attr('value') == 'si' ) { | |
$('.woocommerce-Price-amount bdi').next().remove(); | |
$('.woocommerce-Price-amount bdi').after(' <small>IVA Inclusa al 10%</small>'); | |
} else { | |
$('.woocommerce-Price-amount bdi').next().remove(); | |
$('.woocommerce-Price-amount bdi').after(' <small>IVA Inclusa al 22%</small>'); | |
} | |
}); | |
// Gestione dell'evento "uni_cpo_set_price_event" | |
$(document).on('uni_cpo_set_price_event', function(event, price) { | |
// Questa funzione viene eseguita quando viene innescato l'evento "uni_cpo_set_price_event" | |
// Rimuovi i caratteri non numerici dalla stringa | |
var numericString = ""; | |
for (var i = 0; i < price.length; i++) { | |
// Verifica se il carattere corrente è un numero o una virgola | |
if (!isNaN(price[i]) || price[i] === ",") { | |
numericString += price[i]; // Aggiungi il carattere numerico o la virgola alla stringa numerica | |
} | |
} | |
// Converte la stringa numerica in un valore float | |
var result = parseFloat(numericString.replace(",", ".")) / 1.22; | |
// Verifica se il risultato è NaN | |
if (isNaN(result)) { | |
// Mostra un messaggio di errore | |
$('.woocommerce-Price-amount.amount bdi').text("Calcolando..."); | |
} else { | |
// Formatta il risultato come una stringa di valuta in euro | |
var formattedResult = result.toLocaleString('it-IT', { minimumFractionDigits: 2, style: 'currency', currency: 'EUR' }); | |
// Aggiorna il contenuto dell'elemento HTML con il risultato formattato | |
$('.woocommerce-Price-amount.amount bdi').text(formattedResult); | |
} | |
}); | |
}); | |
</script> | |
<?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment