You want your e-commerce to use most supplier-efficient and cost friendly delivering process. At the same time, you want to keep calculations and thus memory outbursts to a minimum.
In our case, an e-commerce making use of several suppliers for its products, needed an optimal solution. We had to take into account:
- product availability;
- shipping costs per supplier;
- using as little suppliers as possible, to reduce amount of shippings as well as total shipping costs.
Using Magento's PHP collections, I did my job and developed a function that listed al possible combinations, excluding combinations when one of the products per supplier was not in stock.
Combinations and high memory
However, after creating a list of combinations, the totals of product- and shippingcosts had to be calculated. This resulted in the best product-supplier shipping situation, but potentially needed a lot of server memory.
For example; 20 products over 5 possible suppliers would result in 3.2 million combinations. Having 6 suppliers available, would result in 64 million combinations! This required quite some Gigabytes of memory by the server, per order/calculation. Not that well for carbon emission!
This could (and should) be improved. But only being a developer, I was a bit stuck.
Mathematical approach
So I called in the help of a friend, having a Master's Degree in Applied Mathematics. And you could tell, as it was striking how easily he solved the puzzle.
Thanks to a more mathematical approach to PHP's while loops, he managed to improve my implementation by initially writing it out on paper, after which we converted his pseudo-code into PHP.
Clauses
The best combination had to comply with some clauses or calculation rules. The price deviation per product for the most optimal delivery process, for example, should not exceed 10% compared to the cheapest product to be delivered. Delivery costs per unique supplier also had to be taken into account, as well as any free delivery costs per supplier from a certain amount.
As a result, of the possibly millions of combinations, we had to keep at least a list of contenders in order to compare them again in a second phase in order to unleash the PHP calculation rules. The result was that for this list (PHP array) in itself, a lot of PHP memory would be needed.
We have also managed to reduce this, whereby the list can only be as large as the number of possible suppliers. As a result, the required length / size of the list is already known in advance, so you know in advance whether it will entail memory issues. At one million suppliers, this would only require 50 MB of memory.
However, needing that amount of suppliers for an incoming order, such required memory would be your least concern!