Solved

# Working out discount on custom cart PHP

Posted on 2010-08-26
8 Views
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!

``````\$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;
``````
0
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
• 3
• 2

LVL 9

Expert Comment

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

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

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

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;
}
``````
0

LVL 9

Expert Comment

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

0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

Why can I INSERT a DATETIME record into mysql but I can't UPDATE it? 15 66
PHP substring 3 50
Securing source code... theoretical question 10 47
Code planning methods/tools? 5 55
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 i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
###### Suggested Courses
Course of the Month6 days, 23 hours left to enroll