Skip to content

Dropdown Set tax on product page

I’m working on a dropdown where user can select tax.
So I edited addtocart.phtml to put my custom code:

I’ve added an ajax call but I’m stuck how set product tax from 10% to 22%
Can anyone help me?

    <?php if ($_product->getOrigData('tax_class_id') == 2 ):?>
<fieldset class="fieldset">
                <div class="field">
               <label class="label" id="txt">Seleziona l'iva desiderata</label>
                  <select class="select" name="changeiva" id="changeiva" required>
                  <option value="0">Selezionare</option>
                  <option value="1">10%</option>
                  <option value="2">22%</option>
                  </select>
            
<?php 
if (isset($_POST['siv'])) {
ob_clean();           
$iva = $_POST["siv"];
switch ($iva) {
  case "0":
    echo "Non hai selezionato l'IVA!";
    break;
  case "1":
    echo " IVA10%!";
    break;
  case "2":
    echo "IVA22%!";
    break;
}
exit();    
}
?> 
          </div>
</fieldset>
<script>
require([
    'jquery' ],
    function($) {
    $(document).ready(function() {
   $("select[name='changeiva']").on('change', function(){
       var siv = $("#changeiva").val();
  $.ajax({
    type: "POST",
    data:{siv:siv},
    success: function(result){
         $("#test").html(result);
            }
        });
    })
});