Creating rule to create time based on delivery rate

Asked

Viewed 69 times

0

I have a condition in my form that calculates the delivery time of orders, follows below the rule:

date_default_timezone_set('America/Sao_Paulo');
$Agora = date('H:i:s');
$HoraServico = date('H:i:s', strtotime('+69 minute', strtotime($Agora)));


if ( $Agora < '11:00:00'){
    $HoraServico = '12:00:00';
}

else if ( $Agora > '11:00:00' ){
    $HoraServico == $HoraServico;

};

Today all orders made before 11am arrive at 12pm at the customer, and orders made after 11am arrive 69 minutes later. That is, current time ($Now) + 69 minutes.

What I need is to create a rule where $Horaservico is based on the delivery amount $delivery

Example: If the delivery fee costs R$ 7,00 then the delivery time will be the sum of the current time ($Now) + 120 minutes.

If the delivery fee costs R$ 2,00 then the delivery time will be the sum of the current time ($Now) + 30 minutes. And so on.

The idea is, the more expensive the delivery fee (pq is far) the more time it will cost to deliver.

I don’t know if it would be like this:

date_default_timezone_set('America/Sao_Paulo');
$Agora = date('H:i:s');
$HoraServico = date('H:i:s', strtotime('+69 minute', strtotime($Agora)));

if ( $entrega = '5.00'){
    $HoraServico = date('H:i:s', strtotime('+120 minute', strtotime($Agora)));
}

if ( $entrega = '2.00'){
    $HoraServico = date('H:i:s', strtotime('+30 minute', strtotime($Agora)));

}

else if  ( $entrega = '7.00'){
    $HoraServico = date('H:i:s', strtotime('+20 minute', strtotime($Agora)));

};

I tested so, but it’s not going! It’s always adding with the middle option, where the $entrega is equal to R $ 2,00 - staying in the case 30 minutes to add with current time.

From what I understand, he’s not correctly computing the delivery rate ($entrega = $_POST["taxadeentrega"];)

In short, $entregaread the form field:

inserir a descrição da imagem aqui

The code for this field in the form is:

<tr>
    <td>Taxa de Entrega:</td>
    <td><span id="idTaxa"></span></td>
  </tr>
  • Okay, what’s your problem? It’s not clear.

  • And in the case of $Agora be exactly 11:00:00?

  • You know if the value of the field idTaxa of the form appears in the $_POST["taxadeentrega"]? What is attributed to $entrega after all?

2 answers

1

Hello,

He is "always adding up the middle option" as part of the declarations if, if e else if you are performing an assignment operation and not comparing between values.

Do the following:

date_default_timezone_set('America/Sao_Paulo');
$Agora = date('H:i:s');
$HoraServico = date('H:i:s', strtotime('+69 minute', strtotime($Agora)));

if ( $entrega == '5.00'){
    $HoraServico = date('H:i:s', strtotime('+120 minute', strtotime($Agora)));
}

if ( $entrega == '2.00'){
    $HoraServico = date('H:i:s', strtotime('+30 minute', strtotime($Agora)));

}

else if  ( $entrega == '7.00'){
    $HoraServico = date('H:i:s', strtotime('+20 minute', strtotime($Agora))); 
}
  • Unfortunately it didn’t work that way! You’re not recognizing the value of $delivery. It’s just adding up the minutes of that line $HoraServico = date('H:i:s', strtotime('+69 minute', strtotime($Agora)));

  • You perform a comparison $entrega == '5.00' - is to compare the variable $entrega with a string.

  • Something else... $entrega is defined?

  • The $entregais generated through another JS file. This instance actually pulls information from the form field: $entrega = $_POST["taxadeentrega"]. I don’t know if it would be interesting, or easier to create this rule directly in this JS file. Thanks for your attention, but I’m a layman, I’m studying PHP on the web.

  • How can I compare the variable $entrega with a string? You could make an example?

0


Solved!! Follow correct code:

var arrayTempoEntrega = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110];
var arrTaxaEntrega = [4.80, 6.50, 6.00, 5.80, 5.30, 4.30, 3.80, 3.30, 2.50, 2.00, 1.50];
var taxadeentrega = total - taxa;

On the part of the Ceps

//aqui eu pego o cep
        var cep = document.getElementById("cep").value;


} if (cep == "20010-090" || cep == "20020-100" || cep == "20021-130" || cep == "20021-315" || cep == "20030-001" || cep == "20031-000" || cep == "20031-003" || cep == "20031-010" || cep == "20031-050" || cep == "20031-141" || cep == "20031-143" || cep == "20031-005" || cep == "20031-001" || cep == "20020-903" || cep == "20031-144" || cep == "20030-080" || cep == "20031-142" || cep == "20031-120" || cep == "20031-007" || cep == "20010-009" || cep == "20031-913" || cep == "20021-370" || cep == "20200-100" || cep == "20030-901" || cep == "20030-021" || cep == "20210-030" || cep == "24220-280") {

            //se for um dos ceps acima, incrementa 1.2 no valor final
            taxa = arrTaxaEntrega[9];
            tempoEntrega = arrayTempoEntrega[9];

        } if (cep == "20020-010" || cep == "20020-040" || cep == "20020-080" || cep == "20021-060" || cep == "20021-120" || cep == "20021-900" || cep == "20021-903" || cep == "20030-002" || cep == "20030-015" || cep == "20030-013" || cep == "20030-020" || cep == "20030-021" || cep == "20030-060" || cep == "20030-070" || cep == "20030-120" || cep == "20002-080" || cep == "20002-008" || cep == "20003-021" || cep == "20030-905" || cep == "24220-031" || cep == "20002-010" || cep == "20030-015") {

            //se for um dos ceps acima, incrementa 0.7 no valor final
            taxa = arrTaxaEntrega[10];
            tempoEntrega = arrayTempoEntrega[10];

        }

        total += taxa;

        if (taxa != 0) {
            //caso a taxa seja diferente de 0, mostra ao usuário
            document.getElementById("idTaxa").innerHTML = "Custo adicional: R$ " + taxa;
        }
        document.getElementById("idTempoEntrega").innerHTML = tempoEntrega + " min.";
        document.getElementById("TempoEntrega").value = tempoEntrega;
        $("div#total").html("Valor total da sua<br>encomenda: R$ " + String(total.toFixed(2)).replace(".", ","));
        $("div#total").css("display", "block");
        $("#total2").html("R$ " + String(total.toFixed(2)).replace(".", ","));
    }) //click
    $("#total2").html("R$ " + String(total.toFixed(2)).replace(".", ","));

Browser other questions tagged

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