T
T
TILLhen2022-02-24 20:04:22
WordPress
TILLhen, 2022-02-24 20:04:22

How to add 20% to the total amount in the basket when the checkbox is activated?

Task:
When the client sets the checkbox to "true", then add 20% of the total amount in the cart to "Total". Now I have 20% added regardless of the status of the checkbox.
6217ba52baf6e454714905.png

cart.php file

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {display:none;}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
</style>
</head>
<body>

<label class="switch">
  <input type="checkbox" id="one" onchange="fun1()">
  <span class="slider round"></span>
</label>

</body>
</html>


functions.php file
/** Галочка СРОЧНО */

include 'https://new.kamelot.kz/wp-content/plugins/woocommerce/templates/cart/cart.php';
?>
<script>
function fun1() {
var chbox;
chbox=document.getElementById('one');
  if (chbox.checked) {
    </script>
    <?php
    function prefix_add_discount_line( $cart ) {

  $discount = $cart->subtotal * 0.2;

  $cart->add_fee( __( 'Плата за срочный заказ', 'yourtext-domain' ) , +$discount );

}
add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line' );
    ?>
<script>
  }
 
}
</script>

<?php
/** КОНЕЦ Галочка СРОЧНО */

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question