Solved

Working out discount on custom cart PHP

Posted on 2010-08-26
7
6 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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.

749 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