Possuo o seguinte loop
:
<?php foreach($order_details as $v_order): ?>
<?php $others_price = $v_order->others_price; ?>
<?php
$adicional = ($v_order->others_price * count($order_details));
$vlr_adc = ($adicional / count($order_details));
?>
<tr>
<td><?php echo $counter ?></td>
<td><?php echo $v_order->product_name ?></td>
<td class="media_price_<?php echo $v_order->product_code; ?>"><?php echo $this->localization->currencyFormat($v_order->buying_price); ?></td>
<td><input type="number" name="product_quantity" value="<?php echo $v_order->product_quantity ?>" class="form-control product_quantity" data-id="<?php echo $v_order->product_code; ?>" style="width: 75%"></td>
<td class="total_price_<?php echo $v_order->product_code; ?> linhas"><?php echo $this->localization->currencyFormat($v_order->sub_total) ?></td>
<td><?php if($counter>1){ ?><button class="btn btn-primary remove_product_<?php echo $v_order->product_code; ?>"><i class="fa fa-minus"></i></button><?php } ?></td>
</tr>
<?php $counter ++?>
<?php endforeach; ?>
E o seguinte jQuery:
$(".product_quantity").keyup(function(){
var product_code = $(".product_quantity").data("id");
var product_quantity = $('.product_quantity').val();
var media_price = $('.media_price_'+product_code).html();
var total_price = $('.total_price_'+product_code).html();
var media_price_r = media_price.replace("R$ ", "");
var media_price_r2 = media_price_r.replace(/\./g, "").replace(",", ".") || 0;
var calc = ((parseFloat(media_price_r2))*product_quantity).toFixed(3);
calc = calc.substr(0, calc.indexOf(".")) + calc.substr(calc.indexOf("."),3);
calc = parseFloat(calc);
$('.total_price_'+product_code).html(calc.toLocaleString("pt-BR", { style: "currency" , currency:"BRL"}));
$(".linhas").each(function(){
var linhas = $(".linhas").val()
var linhas_r = linhas.replace("R$ ", "");
var linhas_r2 = linhas_r.replace(/\./g, "").replace(",", ".") || 0;
var soma += parseFloat(linhas_r2);
})
$(".fechado").html(soma);
});
O problema é: Ao modificar o campo product_quantity
, deveria calcular e mostrar ao lado, no total_price
. Isto é feito somente na primeira linha, nas demais, não é calculado.
E o segundo problema, seria a soma completa de todos os valores contidos na class
linhas, para apresentar o valor total logo mais abaixo:
<h3 class="text-right fechado">Total: <?php echo $this->localization->currencyFormat($order_info->grand_total) ?></h3>
O que tem de errado?