Change page Divs and specific Ids with calculations

Asked

Viewed 39 times

1

Speak guys, I have the following situation on my site, on the carts page have a div I need to change the content of it and put some calculations according to the total value, let go of the parameters:

The code will stay within the following if:

 {% if pages.current == 'checkout_cart' %}

 {% endif %}

The complete div is as follows:

<div class="board">
  <h2 class="color" id="topo">Frete</h2>
</div>

The div of the total value is as follows:

<div id="mostra_total" class="valores_carrinho">40,00</div>

What I need to change:

Get the top ID: <h2 class="color" id="topo">Frete</h2>, where ta written freight need to change to the following variable:

If the total value is less than 200, show the message instead of Freight: Missing X real for your freight stay Free! The calculation would be 200 - TOTAL VALUE, because from 200 real the shipping would be free.

If the total amount is more than 200, show message: Free Shipping!

Editing, a variable appeared for subtraction:

<div id="frete7" class="valores_carrinho">30</div>

This variable has to be subtracted from the total value in the sum, because it will be added independently of the products.

It would look something like this:

id="mostra_total" Valores do carrinho = 40,00
id="frete7" Valores do carrinho = 30,00

It will add up the 40 + 30, but the calculation for the remaining 200 must be only of the show_total, or need to subtract the value of freight

200 -(mostra_total - frete7)
Valor total para frete grátis = 200

1 answer

2


Below is an example of how to do:

$(function() {
   try {
       var valor = parseFloat($('#mostra_total').html().replace(',','.'));
       var frete7 = parseFloat($('#frete7').html().replace(',','.'));

       if((valor - frete7) > 200) {
            $('#topo').html('Frete grátis');
       } else {
            $('#topo').html('Faltam R$' + (200 - (valor  -frete7)).toFixed(2)+''.replace('.',',') + ' para você obter frete grátis!');
       }
   } catch (e) {
       $('#topo').html('Erro verifique se o valor exibido está no formato "41,00"! Detalhes técnicos: ' + e);
   }
});

Example in Jsfiddler

https://jsfiddle.net/9e94ayvx/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.