Solved

Working out discount on custom cart PHP

Posted on 2010-08-26
7
2 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
7 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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 look for a specific file type in a local or remote server directory using PHP.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now