?
Solved

Working out discount on custom cart PHP

Posted on 2010-08-26
7
Medium Priority
?
13 Views
Last Modified: 2016-05-12
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

0
Comment
Question by:mupparion
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:Snarfles
ID: 33529183
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
 
LVL 9

Expert Comment

by:Snarfles
ID: 33529185
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
 

Author Comment

by:mupparion
ID: 33529360
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
 

Accepted Solution

by:
mupparion earned 0 total points
ID: 33531279
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
 
LVL 9

Expert Comment

by:Snarfles
ID: 33531649
Glad to see the round helped with part of it and you got the rest worked out :)

Glad to help.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question