Math question (calculating dimensional weight with code)

I am developing a shipping calculator and am unsure of how to calculate the given billable weight of a total shipment (multiple items). The guidelines for determining the billable weight are as follows:

1
2
3
4
5
6
7
8
9
Dimensional weight = L * W * H

If package dimensional weight is greater than 5,184 inches, divide by 166.

If package dimensional weight is less than 5,184 inches, use actual weight of package.

Billable weight = greater of dimensional weight and actual weight.

For multiple package shipment, total the billable weight of all packages.


What I'm confused about is, if there is 3 quantity of one particular item, lets say the item is 100 lbs actual weight and 40x30x20 (144 dimensional weight), than would I perform the calculation of dimension weight and actual weight for just one quantity of the item and then multiple the greater of those two by quantity (3)? This just seems to be a tad high of a price with those calculations I'm not sure if my logic is correct. Can somebody help me figure this out?

Edit*

For Example, I have a for loop that loops through all the items in the cart. Inside the loop, i have the following calculation to get dimensional weight for each seperate item, and add them to a running total in dimWeight
 
dimWeight += ((length*width*height)*quantity);

This will give me the dimensional weight for each item in the cart as a whole. But I don't know if I need the dimensional weight of the cart as a whole. Maybe I need the dimensional weight * quantity of each individual item that is in the cart, and store each in separate array elements and then determine if the actual weight is greater than the dimensional weight for each package this way? I think just breaking this problem down cleared it up for me a bit, but I'm still not positive.
Last edited on
You're just calculating volume, see weight calculations section http://en.wikipedia.org/wiki/Dimensional_weight

and 40x30x20 (144 dimensional weight)
40*30*20 is 24000, divide that by 144 (assuming this dimensional weight is correct) you get 166.6 and from the link 6000 cm3/kg = 166.667 kg/m3 so try using DW = V/6000.

EDIT: I should have read your post a second time, you're already doing that
dimWeight += ((length*width*height)*quantity);
This doesn't look right to me. Calculate the DW of the package first then multiply by quantity.
Last edited on
I have the calculation, I just need to place them with logical accuracy in my loops and code.

btw. $_SESSION['item'][$key][1] is that items quantity in the cart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
for (this loops through the shopping cart, one item at a time) {
    // Get weight of each item times quantity //
$actualWeight += $weight*$_SESSION['item'][$key][1];

// Get Dimensional Weight //
$dimWeight += ($length*$width*$height);

// Find if Dimensional Weight is greater than Actual Weight //
if ($dimWeight > 5184) {
	$dimWeight = ($dimWeight/166);
} else {
	$dimWeight = $actualWeight;
}

if ($dimWeight > $actualWeight) {
	$billWeight = $dimWeight;
} else if ($dimWeight < $actualWeight) {
	$billWeight = $actualWeight;
} else {
	$billWeight = $actualWeight;
}
}


Me and my weakly typed languages :)
Last edited on
1
2
3
4
5
6
7
if ($dimWeight > $actualWeight) {
	$billWeight = $dimWeight;
} else if ($dimWeight < $actualWeight) {
	$billWeight = $actualWeight;
} else {
	$billWeight = $actualWeight;//this seems redundant
}
If this is in a loop you are over writing billWeight each iteration.

Maybe?
1
2
3
4
5
if ($dimWeight > $actualWeight) {
	$billWeight += $dimWeight;
}else {
	$billWeight += $actualWeight;
}
Last edited on
Well as for redundancy, I just want the program to never make a mistake if the dimWeight is == to actualWeight I want the actualWeight to become the billWeight.

Yes, the billWeight is being rewritten every time. Thanks. Now that I fixed that, what else is wrong with the logic compared to the guidelines in the OP?

We've arrived at the the pinnacle of the problem. I know I will have to add the quantity to the dimWeight as well. Should I just apply it through each loop of items in cart as I did with $actualWeight, or should it be calculated after words, outside of the loop, for each separate item in the cart, maybe with an array, or am I just psyching myself out hoping not to cost my client shipping error fees.

I'm getting the feeling if I just add the DW to dimWeight every loop, the program is making an error of calculating one GIANT BOX and the rates will be higher? Is this correct ?
Last edited on
Well as for redundancy, I just want the program to never make a mistake if the dimWeight is == to actualWeight I want the actualWeight to become the billWeight.
See my edit above.

I know I will have to add the quantity to the dimWeight as well.
If each item in the cart has a quantity maybe something like
1
2
3
4
5
6
7
//get quantity of item
...
if ($dimWeight > $actualWeight) {
	$billWeight += $dimWeight * $quantity;
}else {
	$billWeight += $actualWeight * $quantity;
}
Last edited on
Thank you naraku

I'm getting the feeling if I just add the DW to dimWeight every loop, the program is making an error of calculating one GIANT BOX and the rates will be higher? Is this correct, or from my guidelines at the OP would this be accurate for how UPS handles shipping rates?

(I also have to make a separate decision branch all together for if the item will be shipped with AIR, not ground...., O boy_)
Last edited on
Your guidline says
For multiple package shipment, total the billable weight of all packages
ok I'll go with it, I just needed to be sure. Thanks for your help.
Quick side question -> Large Packages are subject to a minimum
billable weight of 90 pounds. - Does this mean anything below 90 pounds billable weight isn't subject to large package condition, or does this mean large packages are charged an additional 90 lbs. weight?
Sounds like if a large package is under 90lbs it will be billed as a 90lb package.
Topic archived. No new replies allowed.