-1
I cannot take the value of the type of freight that the user chooses and display it on the same page in the checkout box.
This is the page the user enters the freight
<div class="delivery">
<div class="section_title">Calcule o frete</div>
<div class="section_subtitle">Entregue pelos Correios</div>
<div class="coupon_form_container">
<div class="coupon_form">
<input type="text" class="coupon_input" placeholder="Digite seu CEP" id="frete_cart" name="frete_cart" required="required">
<button class="button coupon_button" ><span>Calcular</span></button>
</div>
</div>
<div class="resposta" id='resposta'></div>
</div>
<li class="d-flex flex-row align-items-center justify-content-start">
<div class="cart_total_title">Frete</div>
<div class="cart_total_value ml-auto">AQUI VAI O VALOR DO FRETE ESCOLHIDO</div>
</li>
<script> //calculo frete
var frete_cart = $("#frete_cart");
frete_cart.change(function() {
$.ajax({
url: 'calcularFrete.php?peso=<?php echo $peso ?>&total=<?php echo $total ?>',
type: 'POST',
data:{"frete_cart" : frete_cart.val()},
success: function(data) {
console.log(data);
data = $.parseJSON(data);
// $("#resposta").text(data.email);
//$("#submit").attr("disabled", true);
$("#resposta").html(data.frete_cart);
}
});
});
calculate Payment.php
if(isset($_POST['frete_cart'])){
include('conn.php');
$peso = $_GET['peso'];
$valor = (int)$_GET['total']; // transforma o valor do produto em inteiro
#Recebe o cep de destino
$cepDestino = $_POST['frete_cart'];
if(isset($cepDestino)){ // se existir o cep de pesquisa faz a consulta
$sedex = (calcular_frete('88960000', $cepDestino, $peso,$valor,'04014'));
$pac = (calcular_frete('88960000', $cepDestino,$peso,$valor,'04510'));
// $exibe ="<p>Receba em até " . $sedex->PrazoEntrega . " dias úteis: " . $sedex->Valor . "</p> <p> Receba em até " . $pac->PrazoEntrega . " dias úteis: " . $pac->Valor . "</p>" ;
$exibe ="<div class='delivery_options'>
<label class='delivery_option clearfix'>Receba em até " . $sedex->PrazoEntrega . " dias úteis.
<input type='radio' name='frete' id='frete' value='". $sedex->Valor ."'>
<span class='checkmark'></span>
<span class='delivery_price'> R$ ". $sedex->Valor . "</span>
</label>
<label class='delivery_option clearfix'>Receba em até " . $pac->PrazoEntrega . " dias úteis:
<input type='radio' name='frete' id='frete' value='". $pac->Valor ."'>
<span class='checkmark'></span>
<span class='delivery_price'> R$ " . $pac->Valor . "</span>
</label>
</div>
";
echo json_encode(array('frete_cart' => $exibe));
}
}
How do I when the user chooses one of the shipping options or the value will be displayed on the same page without needing to update? .................................................. ..................................................
It worked properly thanks! But how do I store the data in a variable? to be able together with subtotal to make the total purchase. Thank you!
– iasmim
You can do it like this:
let frete = 0; $(document).on("click", ".delivery_options :radio", function(){ frete = parseFloat(this.value.replace(",", "."));
 $("#frete").html(this.value);
});
. The variablefrete
will be, for example,36.80
(a point in place of the comma so it can be added later with another value).– Sam