Working out discount on custom cart PHP

Hi,

What i want is relatively simple yet i seem to be massively over complicating it.
I've never really had to use forums for help before but this is annoying me now lol.

Basically, what i want is when someone orders 3 or more products that aren't in this case, a canvas, they get 20% off the cheapest 3 products.
But where things get complicated is that there is also a quantity field. So if someone orders say 1 item at 1.99 and then 3 at 2.99 id have to be able to get 1.99 + 2 lots of 2.99 and then work out the 20% on that. And obviously i dont know what they're going to order so it needs to be something dynamic and i've kinda written rules for several possibilities but i cant get it right.

$otherdiscount = (($price3['SUM(price)'] + 0.03)  * 3) * 0.2;
This is basically, get the price total, add 3p (as all prices end in .99 so it rounds it off) x 3(as you get 20% off 3 items) then x 0.2 to get the 20% value.

Probably worth starting over but id have know idea where to begin lol so any help with getting this code to work or posting some new code on where to start would be brilliant!

Thanks in advance.
$row3 = mysql_fetch_array(mysql_query("SELECT SUM(qty) FROM `ypc_cart` WHERE `sessid`='$sessid' AND `type`!='canvas' ORDER BY `price` ASC LIMIT 3"));
$price3 = mysql_fetch_array(mysql_query("SELECT SUM(price) FROM `ypc_cart` WHERE `sessid`='$sessid' AND `type`!='canvas' ORDER BY `price` ASC LIMIT 3"));

$row2 = mysql_fetch_array(mysql_query("SELECT SUM(qty) FROM `ypc_cart` WHERE `sessid`='$sessid' AND `type`!='canvas' ORDER BY `price` ASC LIMIT 2"));
$price2 = mysql_fetch_array(mysql_query("SELECT SUM(price) FROM `ypc_cart` WHERE `sessid`='$sessid' AND `type`!='canvas' ORDER BY `price` ASC LIMIT 2"));

$row1 = mysql_fetch_array(mysql_query("SELECT SUM(qty) FROM `ypc_cart` WHERE `sessid`='$sessid' AND `type`!='canvas' ORDER BY `price` ASC LIMIT 1"));
$price1 = mysql_fetch_array(mysql_query("SELECT SUM(price) FROM `ypc_cart` WHERE `sessid`='$sessid' AND `type`!='canvas' ORDER BY `price` ASC LIMIT 1"));

$totalqty3 = $row3['SUM(qty)'];
$totalnonec=mysql_num_rows(mysql_query("SELECT * FROM `ypc_cart` WHERE `sessid`='$sessid' AND `type`!='canvas'"));
/////IF QTY FROM 3 ITEMS IS MORE THAN 3 OR IS 3
if($totalqty3 > 3 && $totalnonec == 3){
echo "3 > 3";
}elseif($totalqty3 == 3  && $totalnonec == 3){
$otherdiscount = (($price3['SUM(price)'] + 0.03)  * 3) * 0.2;


/////IF QTY FROM 2 ITEMS IS MORE THAN 3 OR IS 3
}elseif($row2['SUM(qty)'] > 3  && $totalnonec == 2){
$otherdiscount = (($price2['SUM(price)'] + 0.03)  * 3) * 0.2;
}elseif($row2['SUM(qty)'] == 3  && $totalnonec == 2){
$otherdiscount = (($price2['SUM(price)'] + 0.03)  * 3) * 0.2;

/////IF QTY FROM 1 ITEM IS 3 OR LESS
}elseif($row1['SUM(qty)'] > 2  && $totalnonec == 1){
$otherdiscount = (($price1['SUM(price)'] + 0.03)  * 3) * 0.2;
}else{
$otherdiscount = 0.00;
}

$totaldiscount = $otherdiscount;

Open in new window

mupparionAsked:
Who is Participating?
 
mupparionAuthor Commented:
Rewrote it myself after much stress!
$count = 1;
$queryitems1=mysql_query("SELECT * FROM `ypc_cart` WHERE `sessid`='$sessid' AND `type`!='canvas' ORDER BY `price` ASC LIMIT 3");
while($cool1=mysql_fetch_object($queryitems1)){
$getrow["".$count.""] = $cool1->id;
$count = $count + 1;
}

$fetchid = mysql_fetch_object(mysql_query("SELECT * FROM `ypc_cart` WHERE `id`='".$getrow[1]."'"));
$fetchid2 = mysql_fetch_object(mysql_query("SELECT * FROM `ypc_cart` WHERE `id`='".$getrow[2]."'"));
$fetchid3 = mysql_fetch_object(mysql_query("SELECT * FROM `ypc_cart` WHERE `id`='".$getrow[3]."'"));

$oneplustwo = $fetchid->qty + $fetchid2->qty;
$plusall = $fetchid->qty + $fetchid2->qty + $fetchid3->qty;

	if($fetchid->qty > 2){
		$otherdiscount = round($fetchid->price  * 3, 2) * 0.2;
	}	elseif($plusall == 3){
			$priceofmulti = $fetchid->price + $fetchid2->price + $fetchid3->price;
			$otherdiscount = round($priceofmulti, 2) * 0.2;			
		}
			elseif($oneplustwo > 2){
				$remainingqty = 3 - $fetchid->qty;
				$priceofmulti = $fetchid->qty * $fetchid->price;
				$priceofmulti2 = $fetchid2->price * $remainingqty;
				$compprice = $priceofmulti + $priceofmulti2;
				$otherdiscount = round($compprice, 2) * 0.2;
				//echo $remainingqty." ".$priceofmulti. " ".$priceofmulti2. " ".$compprice;
			}

Open in new window

0
 
SnarflesCommented:
Rather than adding 0.03 why dont you consider using the round function?

$otherdiscount = ((round($price3['SUM(price)']))  * 3) * 0.2;

Besides.. you were adding 3p to the price first and THEN multiplying by 3... so for 3 products you are adding 9p onto the price rather than 3.
0
 
SnarflesCommented:
See round here btw http://php.net/manual/en/function.round.php

Its pretty straight forward.

Does that solve the problem or is it still not outputing correctly?
0
 
mupparionAuthor Commented:
Makes more sense to round it there, didn't think it would be adding 9p!

That solves one problem thanks but not the problem.

When i put in the cart one item worthy of a discount @ 2.99 of £1.80, which is right.
When i put in three items, each with a quantity of 1 each costing 2.99, 7.99 and 17.99 i get a discount of £17.40 which of course is wrong, should be £5.80.
Also, if i have 2 items in the cart, one at 2.99 with a quantity of 2 and one item at 7.99 with a quantity of 1, i get a discount of £6.60 as opposed to the right one, £2.80

So it would seem theres an issue with my logic going about this but im not sure what?
0
 
SnarflesCommented:
Glad to see the round helped with part of it and you got the rest worked out :)

Glad to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.