Forum Moderators: buckworks
In the same situation, I searched for this five years ago and came up empty handed. I had to grow my own, in Perl.
I'll spare the full details, but it requires getting the total volume (easy,) finding out the item with the largest dimension, insuring that the box doesn't exceed the shippers maximum size/weight requirements, fulling up the first box with items, and if it doesn't fit in the maximum non-oversize box for that shipper, splitting it into multiple boxes if necessary, then send that over to the API for price. And it varies with shipper, USPS and UPS have different requirements.
It's not 100% perfect, but pretty darn close. For a person standing at a table and a dozen box sizes in front of them, it's a simple thing - not so simple to automate.
Here is the basic premise:
- get the total volume
- get the longest item in the order, this determines package length
- organize the items in an array from largest volume to smallest
- Begin "stacking" the items into the selected box, which is determined by the first item. Add values horizontally until the box width is reached, then begin stacking them vertically, subtracting volume as you go along, and addin to "total height" as you add "layers."
- Continue until the items' volumes are exhausted. If the box volume is reached before the items volume is exhausted, you know you need a second (or third, or . . . ) box.
There's more to it, as you need to add a percentage for padding and calculate the padding weight as well as box weight into the final values to send to the API. I figured the box weight by weight per square inch, though square foot is easier to "handle," it doesn't help with small parcels.
When all done, your function returns
return ($osvalue,$num_of_pkgs,$ht,$wid,$len,$wt);
representing the final packaged parcel values. In this scenario $osvalue, based on admin settings, always returns 0 so we don't have to pay for oversize packages, multiple parcels works out cheaper.