skip to Main Content

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.

What could be the problem?

2

Answers


  1. Use this

    <?php 
    add_filter( 'woocommerce_before_add_to_cart_form', 'spoiler', 15 ); 
    function spoiler() {
    
    $regprice = 1000;
    $saleprice = 2000;
    
    ?>
    <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>
            <?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>'; 
    }
    }

    and put it in your code

    Login or Signup to reply.
  2. 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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search