Forum Moderators: coopster

Message Too Old, No Replies

need help with a calculation

         

michlcamp

10:18 am on Dec 15, 2005 (gmt 0)

10+ Year Member



I've got a shipping calculation that goes like this:

[2]Shipping: <?php if ($total > 81.75) {printf ("%01.2f", $total * .11);}else {echo "8.99 ";}?>[/2]

but I need to add another calculation, IF the order is from a country other that the U.S. - something like,

if ($country is NOT USA, * .25)

I still need the other two parts to work the same as they have been, but need to multiply $total by 25% if outside of the US...

so, in non-php terms... IF $country is USA, AND $total > 81.75, printf $total * .11,

but IF $ country is NOT USA, printf $total * .25

Don't know how to write multiple IFs...
Any/all help appreciated...
thanks in advance.
mc

LeChuck

1:10 pm on Dec 15, 2005 (gmt 0)

10+ Year Member



Here you go:

if ($total>81.75) {
$final=$total*1.11;
} else {
$final=8.99;
}

if ($country!="USA") {
$final=$final*1.25;
}
printf ("%01.2f",$final);

It's not that complicated.. :)

Are you doing this for a client or is this your online store?

If you are developing this I'd suggest moving the 8.99 and 81.75 to variables that you include from a config.php since the users may want to change these without hunting for them in the main script.

michlcamp

1:40 pm on Dec 15, 2005 (gmt 0)

10+ Year Member



Thanks much...almost there...
now..
how would I put that all in a hidden field to pass to the next page?

[2]<input type="hidden" name="shipping" value="{printf ("%01.2f", $final );}">[/2]
?

LeChuck

1:44 pm on Dec 15, 2005 (gmt 0)

10+ Year Member



ooops, made a mistake.

if ($total>81.75) {
$shipping=$total*.11;
} else {
$shipping=8.99;
}

if ($country!="USA") {
$shipping=$total*1.25;
}
$total=$total+$shipping;

printf ("%01.2f",$total);

I'd advise against passing it to the next page as hidden post fields, it's quite easy to fake the value. Next thing you know you'd be accepting $0.50 orders for stuff worth $500... keep track of the numbers serverside.

LeChuck

2:54 pm on Dec 15, 2005 (gmt 0)

10+ Year Member



last mistake, i hope..

if ($total>81.75) {
$shipping=$total*.11;
} else {
$shipping=8.99;
}

if ($country!="USA") {
$shipping=$total*.25;
}
$total=$total+$shipping;

printf ("%01.2f",$total);

LeChuck

3:32 pm on Dec 15, 2005 (gmt 0)

10+ Year Member



if ($country=="USA") {
if ($total>81.75) {
$shipping=$total*.11;
} else {
$shipping=8.99;
}
} else {
if ($total>81.75) {
$shipping=$total*.25;
} else {
$shipping=8.99*1.25;
}
}
$total=$total+$shipping;

printf ("%01.2f",$total);

michlcamp

4:22 pm on Dec 15, 2005 (gmt 0)

10+ Year Member



man, thanks a bunch. I'll plug it in today and let you know how it works...
mc