Calculation Issue

A

Anonymous

Guest
O.k. I'm having some issues as far as calculating a shipping cost. I want it to calculate a cost on 3 items of say $7.00 +.75 per item, but it is calculating it at $7.75 an item anyone have a clue where I'm going wrong? First it looks to my line items

$module = fetchDbConfig("Per_Category");


$countryISO = countryIso($basket['delInf']['country']);


$zones['n'] = explode(",",str_replace(" ","",strtoupper($module['national'])));
$zones['i'] = explode(",",str_replace(" ","",strtoupper($module['international'])));
$zones['e'] = explode(",",str_replace(" ","",strtoupper($module['excluded'])));


foreach ($zones as $key => $value){

foreach($zones[$key] as $no => $iso){

if($iso == $countryISO){

$shipZone = $key;

}

}

}

if($shipZone == "n"){

$lineShip = $lineShip + ($product[0]['item_ship'] * $quantity);

if(!isset $perShipPrice<$product[0]['per_ship']){
$perShipPrice = $product[0]['per_ship'];
}

} elseif($shipZone == "i"){

$lineShip = $lineShip + ($product[0]['item_int_ship'] * $quantity);

if(!isset $perShipPrice<$product[0]['per_int_ship']){
$perShipPrice = $product[0]['per_int_ship'];
}

} elseif($shipZone == "e" || !isset($shipZone)){
// do nothing?!
}

?>

Then it is supposed to calculate them as I said before at $7.00 + $0.75 per item and give a total

$module = fetchDbConfig("Per_Category");
if($module['status']==1){

$sum = $perShipPrice + ($lineShip);


$taxVal = taxRate($module['tax']);

if($taxVal>0){

$val = ($taxVal / 100) * $sum;
$sum = $sum + $val;
}

if($sum == 0){
$sum = 0.00;
}

$shippingPrice .= "<option value='".$shipKey."'";

if($shipKey ==$basket['shipKey']){
$shippingPrice .= " selected='selected'";

if($shipZone == "n"){

$worldZone = $lang['misc']['national'];

} elseif($shipZone == "i"){

$worldZone = $lang['misc']['international'];

}

$basket = $cart->setVar($lang['misc']['byCategory']." (".$worldZone.")","shipMethod");
$basket = $cart->setVar(sprintf("%.2f",$sum),"shipCost");
}

$shippingPrice .= ">".priceFormat($sum)."</option>\r\n";
$shippingAvailable = TRUE;

$shipKey++;

unset($module, $taxVal);
}
?>

And it is giveing me a total of like $45.60 for 6 items which is WAY more than I want it to charge for shipping as it is over half what they spent in the first place assuming I had this up and running to accept orders...
 
Ehm, 6 items of $7 + 6 times shipping of $0.75 = $42 + $4.50 = $46.50 ..

So what's the problem?
 
The problem is that I want a sliding scale of $7.00 + ($0.75 x 5) on the 6 items.
 
if you give us what rules a calculation should follow: we can meake it work :)
so we know that its 0.75 per item but 6 items only require 5x0.75
what more? how should it work for 7,8,9 and so one items?
 
I guess my wording isn't all that great as I go back and look. What I want it to do is give a base shipping of $7.00 ($perShipPrice) and $9.75 ($lineShip) for each additional item purchased over the first item. (eg. 5 items = $7.00 + 4 x $0.75 ($3.00) so $10.00 or 6 items $7.00 + (5 x $0.75) ($3.75) or $10.75 etc. so on and so forth.) The shipping that I'm coming out with as it sits is about 4 times what I want for the shipping to come out at.
 
I don't really understand your code.. I see some errors and it's really bad to read without indentation... Maybe you can post the actual code with indentation...

But as an example:
Code:
<?php
$quantity = 6; // would be set by other code like reading the database

$shippingCost = 0;
if ($quantity) {
   $shippingCost = $perShipPrice + $lineShip * ($quantity - 1);
}
?>

Coditor
 
Here it is hopefully with the indentation and some explanations for my maddness which is still probably not all correct, but close as I can get it with my limited knowledge of PHP
Code:
<?php
// per category shipping module
$module = fetchDbConfig("Per_Category");

// get the delivery ISO
$countryISO = countryIso($basket['delInf']['country']);

// build array of ISO Codes
$zones['n'] = explode(",",str_replace(" ","",strtoupper($module['national'])));
$zones['i'] = explode(",",str_replace(" ","",strtoupper($module['international'])));
$zones['e'] = explode(",",str_replace(" ","",strtoupper($module['excluded'])));

// find the country
foreach ($zones as $key => $value){

	foreach($zones[$key] as $no => $iso){
	
		if($iso == $countryISO){
		
			$shipZone = $key;
		
		}
	
	}

}

if($shipZone == "n"){

	$lineShip = $lineShip + ($product[0]['item_ship'] * $quantity);
	
	if(!isset($perShipPrice) OR $perShipPrice<$product[0]['per_ship']){
		$perShipPrice = $product[0]['per_ship'];
	}

} elseif($shipZone == "i"){
	
	$lineShip = $lineShip + ($product[0]['item_int_ship'] * $quantity);
	
	if(!isset($perShipPrice) OR $perShipPrice<$product[0]['per_int_ship']){
		$perShipPrice = $product[0]['per_int_ship'];
	}

} elseif($shipZone == "e" || !isset($shipZone)){
// do nothing?!
}

?>

And the Calculation file...

Code:
<?php
// per category shipping module
$module = fetchDbConfig("Per_Category");
if($module['status']==1){

$sum =  $perShipPrice + $lineShip;


$taxVal = taxRate($module['tax']);

if($taxVal>0){

	$val = ($taxVal / 100) * $sum;
	$sum = $sum + $val;
}

if($sum == 0){
	$sum = 0.00;
}

$shippingPrice .= "<option value='".$shipKey."'";

if($shipKey ==$basket['shipKey']){
	$shippingPrice .= " selected='selected'";
	
	if($shipZone == "n"){

		$worldZone = $lang['misc']['national'];

	} elseif($shipZone == "i"){
	
		$worldZone = $lang['misc']['international'];
	
	}
	
	$basket = $cart->setVar($lang['misc']['byCategory']." (".$worldZone.")","shipMethod");
	$basket = $cart->setVar(sprintf("%.2f",$sum),"shipCost");
}

$shippingPrice .= ">".priceFormat($sum)."</option>\r\n";
$shippingAvailable = TRUE;

$shipKey++;

unset($module, $taxVal);
}
?>

And I'm looking at your explination there coditor and I'm looking at my code and thinking the easiest way to go about it is to make

Code:
	$lineShip = $lineShip + ($product[0]['item_int_ship'] * $quantity);

into

Code:
	$lineShip = $lineShip * ($quantity - 1);

Or am I compleatly off base there?
 
Well, I have no idea what you're doing here:
Code:
<?php
   $lineShip = $lineShip + ($product[0]['item_ship'] * $quantity);
   
   if(!isset($perShipPrice) OR $perShipPrice<$product[0]['per_ship']){
      $perShipPrice = $product[0]['per_ship'];
   }
?>
If $lineShip is what you charge per extra product ($ 0.75), why are you adding other cost to it?

Coditor
 
Code:
<?php
   $lineShip = $lineShip + ($product[0]['item_ship'] * $quantity);

    if(!isset($perShipPrice) OR $perShipPrice<$product[0]['per_ship']){
      $perShipPrice = $product[0]['per_ship'];
   }
?>

Is for international shipping as it is on the internet that I'm going to be putting this cart and as it is extra to ship it out of the country I want it to add some for that purpose if it is in a specific Range of countries like canada and mexico just for example.
 
Back
Top