Jump to content

Recommended Posts

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?
Link to comment
https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23320
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23325
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23333
Share on other sites

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 crashin

try 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
Link to comment
https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23342
Share on other sites

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);
Link to comment
https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23344
Share on other sites

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]

Link to comment
https://forums.phpfreaks.com/topic/6436-vat-on-certain-items/#findComment-23705
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.