I use Wordpress, I place the code in functions, the code:
add_filter( 'woocommerce_before_add_to_cart_form', 'spoiler', 15 );
function spoiler() {
$regprice = 1000;
$saleprice = 2000;
?>
<script type="text/javascript">
function kalkulyator(){
var rezultat2 = '<?php echo htmlspecialchars ($saleprice); ?>';
if (document.getElementById('option1').checked)
rezultat2 -= parseFloat(document.getElementById('option1').value);
document.getElementById('rezultat2').innerHTML = rezultat2;
if (document.getElementById('option2').checked)
rezultat2 -= parseFloat(document.getElementById('option2').value);
document.getElementById('rezultat2').innerHTML = rezultat2;
if (document.getElementById('option3').checked)
rezultat2 -= parseFloat(document.getElementById('option3').value);
document.getElementById('rezultat2').innerHTML = rezultat2;
if (document.getElementById('option4').checked)
rezultat2 -= parseFloat(document.getElementById('option4').value);
document.getElementById('rezultat2').innerHTML = rezultat2;
}
</script>
<?php
if (...) {
echo '<form onsubmit="return false;">';
echo '<input id="price1" type="hidden" value="'. $saleprice . '" oninput="kalkulyator()">';
echo '<label><input type="checkbox" id="option1" value="'. round ($regprice * 0.05). '" onclick="kalkulyator()"/></label>';
echo '<label><input type="checkbox" id="option2" value="'. round ($regprice * 0.05). '" onclick="kalkulyator()"/></label>';
echo '<label><input type="checkbox" id="option3" value="'. round ($regprice * 0.05). '" onclick="kalkulyator()"/></label>';
echo '<label><input type="checkbox" id="option4" value="'. round ($regprice * 0.05). '" onclick="kalkulyator()"/></label>';
echo '<div style="text-align: center; font-weight: bold; margin-top: 20px;">You Price:</div>';
echo '<output id="rezultat2"> ' . $saleprice . '</output> ₽ <span style="color: black; font-weight: normal; margin-left: 5px;"><del>'. $regprice .'</del></span></div>';
echo '</form>';
}
}
The code works when I use only one condition. But when I use elseif and prescribe another condition (the same form, only with changes), the first if
condition works, and all subsequent else if
do not work. For some reason, the variables $saleprice
and
$regprice
are not inserted, zeros are used instead.
Tell me, what could be the problem?
Thanks!
CodePudding user response:
may be use
var rezultat2 = parseInt('<?php echo htmlspecialchars ($saleprice); ?>');
and then try
CodePudding user response:
for JavaScript the code look like
<script type="text/javascript">
function kalkulyator(){
var rezultat2 = parseInt('<?php echo htmlspecialchars ($saleprice); ?>');
if (document.getElementById('option1').checked)
rezultat2 -= parseFloat(document.getElementById('option1').value);
document.getElementById('rezultat2').innerHTML = rezultat2;
if (document.getElementById('option2').checked)
rezultat2 -= parseFloat(document.getElementById('option2').value);
document.getElementById('rezultat2').innerHTML = rezultat2;
if (document.getElementById('option3').checked)
rezultat2 -= parseFloat(document.getElementById('option3').value);
document.getElementById('rezultat2').innerHTML = rezultat2;
if (document.getElementById('option4').checked)
rezultat2 -= parseFloat(document.getElementById('option4').value);
document.getElementById('rezultat2').innerHTML = rezultat2;
}
</script>