12

I am coding a custom commerce CMS in PHP.
I have been integrating the UPS XML API for getting Shipping Rates and currently have it working. I am using Mark Sandborn's PHP class http://code.google.com/p/ups-php/

The API requires input of the final package's Length, Width, Height and Weight. My product database has this info for each respective product, so no problem when someone purchases just one item. Slightly different story when multiple products are added to the cart... The final Package Weight is still easy since it I can just grab the sum of all product weights.

However what are best practices for estimating the final package's Length, Width and Height to pass to UPS when multiple products are in the cart?

Amendment
It seems to me that any developer working with the UPS API in a store environment must deal with this same issue. More so than a solution to the knapsack problem I'm hoping I am misunderstanding something about how the API works and that there's actually a simple way to deal with this or even a way to hand off this to UPS to calculate.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
filip
  • 3,036
  • 4
  • 22
  • 20
  • Possible duplicate: http://stackoverflow.com/questions/7648859/given-a-set-of-objects-how-to-calculate-how-many-fit-in-a-given-volume Do you have specific box sizes that you're trying to fit everything into? That would certainly affect the outcome of this algorithm. – Ian Hunter Jul 19 '12 at 03:29
  • Hmm since this will be a CMS used for many different stores, I do not have info on specific box sizes. I believe other CMSs like X-Cart, Magento, Zen Cart, etc also use the UPS API to calculate shipping, so I hoped there would be a common approach to handling this issue. – filip Jul 19 '12 at 03:44
  • I've found this--not sure if it works exactly for what you need: https://github.com/yetzt/boxing – Ian Hunter Jul 19 '12 at 06:09
  • Thank you beanland. Although it isn't exactly what I need it was helpful to look how they handled some of the calculations. – filip Jul 19 '12 at 15:23

1 Answers1

3

You are basically trying to find the same software that business use when designing 2D shapes. They get first a flat material surface, and then, with the objects they want to obtain, optimize the arrangement so no material is wasted.

This is not an easy task; I would say it's a huge task even considering all your objects are perfect boxes.

Also I think you could actually write a whole book about this as some people has done. If you are still willing to do it automatically instead of trying manually, read this Wikipedia article.

And here there is an old but still helpful forum conversation about it for PHP. They also suggest the try-and-see that is optimal, which I think it's your best option.

Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90