Page 1 of 1
if when
Posted: Fri Jul 04, 2003 6:04 am
by meandrew
Hi all I am adding some 'basic' code to a system that adds a shipping value based on the price to the total cart $total which will then return a $grand_total
My problem is that I am unsure if I am doing this correctly, if the total amount is less than 45 then 2.50 is added but if it is 45 and over 0 gets added, this is what I have so far but oit isn't passing a value through
Code: Select all
/* Delivery Costs. */
$delivery_costs = "1.50";
$delivery_costs2 = "0.00";
/* Grand Total. */
$grand_total = ($total + $delivery_costs);
if ($total > "45.00") {
print "$grand_total = ($total + $delivery_costs)";
} elseif ($total == "45.00") {
print "$grand_total = ($total + $delivery_costs)";
} else {
print "$grand_total = ($total + $delivery_costs2)";
}
any help would be greatly appreciated
Andrew
Posted: Fri Jul 04, 2003 6:19 am
by m@ndio
do this:
Code: Select all
/* Delivery Costs. */
$delivery_costs = "1.50";
$delivery_costs2 = "0.00";
/* Grand Total. */
$grand_total = $total + $delivery_costs;
//the less than or equal to removes one line of code
if ($total <= "45.00") {
echo "$grand_total = ($total + $delivery_costs)";
}
else {
echo "The grand total is: ".$grand_total;
}
I have tidied up your code for you, but the only problem was that you were using the print statement to display variables... This is fine except you have to use single quotes and not double ones. To save yourself worrying about silly things like this just use the echo statement instead, it does the same without the rubbish.
Posted: Fri Jul 04, 2003 6:34 am
by meandrew
I must be doing soemthing else wrong because this is outputting:
1.5 = ( + 1.50)
the delivery added to the total is 1.50 but I don't think is being added becuse of this code.
Andrew
Posted: Fri Jul 04, 2003 6:45 am
by m@ndio
I just noticed that when you are declaring your variables you are setting them up as strings... i.e. putting them in quotes. Try this:
Code: Select all
/* Delivery Costs. */
$delivery_costs = 1.50;
$delivery_costs2 = 0.00;
/* Grand Total. */
$grand_total = $total + $delivery_costs;
//the less than or equal to removes one line of code
if ($total <= "45.00") {
echo "$grand_total = ".$total + $delivery_costs;
}
else {
echo "The grand total is: ".$grand_total;
}
Posted: Fri Jul 04, 2003 6:46 am
by []InTeR[]
Code: Select all
<?
$delivery_costs = 1.50;
if($total<=45.00){
$grand_total = $total + $delivery_costs;
}
else{
$grand_total = $total;
}
echo $grand_total;
?>
Posted: Fri Jul 04, 2003 6:50 am
by twigletmac
Personally I would do something along the lines of:
Code: Select all
<?php
if ($total <= 45.00) {
$delivery_costs = 1.50;
} else {
$delivery_costs = 0.00;
}
$grand_total = $total + $delivery_costs;
echo 'Grand Total = $'.number_format($grand_total, 2);
?>
Where does $total come from - are you sure it is available to be used? Have you done something like:
to check it?
Mac
Posted: Fri Jul 04, 2003 12:20 pm
by meandrew
thanks for the help this is working now... and I am a happy bunny

:)
there is another part to this where I want text to display that if the order is over £45 the delivery is free so I'll give that a go now
Andrew