Hilly_2004 Posted April 3, 2006 Share Posted April 3, 2006 Hiya guys, Ive built my own shopping cart and there are certain rules here in UK regarding clothing for children (your not allowed to charge VAT - 17.5%). Anyway this is the code I have:[code]for($i=0; $i<count($myCart); $i++){$query = "SELECT * FROM Items WHERE Item_ID = '".$myCart[$i]['Item_ID']."'";$queryresult = mysql_query($query, $conn);$MainRow = mysql_fetch_array($queryresult);$VatInfo = $MainRow["VAT"];}if ($VatInfo > 0) { //echo $amountTotal;$vat = (0); // $price_wovat=round($amountTotal+$price_wovat1, 2); //$vat = $amountTotal-$price_wovat1;} else {$vat = ($amountTotal/100) * 17.5;}[/code]It works when I have one item in the shopping cart that is exempt from VAT charges, however when I mix an item that is exempt from VAT and one that isn't, the shopping cart charges VAT on both the items. Can anyone see why? Quote Link to comment https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/ Share on other sites More sharing options...
toplay Posted April 3, 2006 Share Posted April 3, 2006 You got to calculate VAT when necessary at each individual cart item and not at the end using the total.Right now, your $VatInfo variable is getting overridden with every query. So, whatever the last cart item VAT status is, seems to determine whether to apply VAT on the whole order or not. Quote Link to comment https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23320 Share on other sites More sharing options...
Hilly_2004 Posted April 3, 2006 Author Share Posted April 3, 2006 Cheers mate, at least now I know why its doing it. Do you have any suggestions as to how I can adapt my query so that it applies it to every item rather the last item entered into the basket? Quote Link to comment https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23324 Share on other sites More sharing options...
earl_dc10 Posted April 3, 2006 Share Posted April 3, 2006 I would recommend having[code]if ($VatInfo > 0) { //echo $amountTotal;$vat = (0); // $price_wovat=round($amountTotal+$price_wovat1, 2); //$vat = $amountTotal-$price_wovat1;} else {$vat = ($amountTotal/100) * 17.5;}[/code]inside your for() loop but add something like[code]$total += <price of item>;[/code]so everytime it runs and calculates whether it'a VAT or not, it adds that value to $total Quote Link to comment https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23325 Share on other sites More sharing options...
Hilly_2004 Posted April 3, 2006 Author Share Posted April 3, 2006 Ive put it in the for loop however it's still adding VAT to both items. I think its the query thats doing it, just need to figure out how it should go... Quote Link to comment https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23327 Share on other sites More sharing options...
earl_dc10 Posted April 3, 2006 Share Posted April 3, 2006 is $amountTotal calculated before this query? try changing $amountTotal to the amount of the individual item for inside the loop so that the VAT calculation only applies to the individual price, not the whole. Quote Link to comment https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23330 Share on other sites More sharing options...
Hilly_2004 Posted April 3, 2006 Author Share Posted April 3, 2006 Heres what I changed it to mate, and it seemed like it was working, however when I added another couple of items and checked out what the VAT should be I found that it ended up charging VAT on the last item instead of adding all the items together.[code]for($i=0; $i<count($myCart); $i++){$query = "SELECT * FROM Items WHERE Item_ID = '".$myCart[$i]['Item_ID']."'";$queryresult = mysql_query($query, $conn);$MainRow = mysql_fetch_array($queryresult);$Price = $MainRow["UK_Price"];$VatInfo = $MainRow["VAT"];if ($VatInfo > 0) { //echo $amountTotal;$vat = (0); // $price_wovat=round($amountTotal+$price_wovat1, 2); //$vat = $amountTotal-$price_wovat1;} else {$vat = ($Price/100) * 17.5;}}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23333 Share on other sites More sharing options...
earl_dc10 Posted April 3, 2006 Share Posted April 3, 2006 can I see how you are adding these values together? it may be in there I just can't see where $amoutTotal = anything.if this is what you have, sorry, I'll suggest it anyway since Im crashintry this for adding the total[code]$amountTotal += ($Price-$vat); // since if VAT doesn't apply $vat equals 0, shouldn't be a problem // after loop is done and you want to display the total$total = round($amountTotal, 2);echo $total;[/code]and I THINK that this line becomes irrelevent since we calculate VAT individually[code]$vat = $amountTotal-$price_wovat1;[/code]Im sorry if this is entirely irrelevent to your question, Im a bit tired, hope it helps though!-J Quote Link to comment https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23342 Share on other sites More sharing options...
Hilly_2004 Posted April 3, 2006 Author Share Posted April 3, 2006 Hiya mate, the $amountTotal variable looks like this...$amountTotal=$HTTP_SESSION_VARS['total'];and this is in the add item to cart page....if($add){ $itemID = $HTTP_POST_VARS['product']; $isFound = false; for($i =0; $i< count($HTTP_SESSION_VARS['cart']); $i++){ if($HTTP_SESSION_VARS['cart'][$i]['Item_ID'] == $itemID){ $isFound = $i; break; } } if($isFound !== false){ $HTTP_SESSION_VARS['cart'][$isFound]['quantity']++; }else{ $newItem = array(); $newItem['Item_ID'] = $itemID; $newItem['quantity'] =1; array_push($HTTP_SESSION_VARS['cart'], $newItem); } $HTTP_SESSION_VARS['items']++; // array_push($HTTP_SESSION_VARS['cart'], $product); $HTTP_SESSION_VARS['total']+=$price; }$conn = db_connect();$products_sql = "select * from Items where Item_ID= \"$ItemID\"";$products_result = mysql_query($products_sql, $conn);$product = mysql_fetch_array($products_result);$price_wovat1=($product['UK_Price']/100)*17.5;$price_wovat=round($product['UK_Price']-$price_wovat1, 2); Quote Link to comment https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23344 Share on other sites More sharing options...
Hilly_2004 Posted April 3, 2006 Author Share Posted April 3, 2006 Any help guys? Quote Link to comment https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23476 Share on other sites More sharing options...
Hilly_2004 Posted April 3, 2006 Author Share Posted April 3, 2006 Pretty please with a cherry on top? :) Quote Link to comment https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23613 Share on other sites More sharing options...
Hilly_2004 Posted April 4, 2006 Author Share Posted April 4, 2006 I tried calculating the VAT in the lib file, however thats still doing the same as before. Quote Link to comment https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23677 Share on other sites More sharing options...
Hilly_2004 Posted April 4, 2006 Author Share Posted April 4, 2006 Where for art thou earl?! lol Can't believe how hard this is...My end total is calculated using this:echo money_format($fmt, $amountTotal+$delivery+$vat); Quote Link to comment https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23697 Share on other sites More sharing options...
earl_dc10 Posted April 4, 2006 Share Posted April 4, 2006 sorry, Im still thinkin about this one, ummm..... just for clarification, if we are calculating the price and vat of each item in a loop, what does this do?[code]$price_wovat1=($product['UK_Price']/100)*17.5;$price_wovat=round($product['UK_Price']-$price_wovat1, 2);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23705 Share on other sites More sharing options...
Hilly_2004 Posted April 4, 2006 Author Share Posted April 4, 2006 Cheers for helping mate, I really appreciate it...I think it was just part of my old way of coding...One of my variables calls it further down...[code]$subtotal = 0;$subtotal+=($price_wovat*$myCart[$i]['quantity']);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23707 Share on other sites More sharing options...
Hilly_2004 Posted April 4, 2006 Author Share Posted April 4, 2006 Basically the problem is that the $vat variable is set by the last item entered into the cart, instead of checking each individual item and then adding all the prices together. Quote Link to comment https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23910 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.