# Optimizing e-commerce supplier and cost efficiency

- 2019-09-17
- 3 min read

Tags:

- [magento](/md/blog/tag/magento/)
- [ecommerce](/md/blog/tag/ecommerce/)
- [performance](/md/blog/tag/performance/)

![device-digital-e-commerce-35550.jpg](/file/upload/img/blog/device-digital-e-commerce-35550.jpg?v=92097ef74885)

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](https://www.linkedin.com/posts/erwinhofman_performance-ecommerce-sustainability-activity-6579665961375408128-wp4X), 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!

## Read next

- ![livestream-tj-gamble-google-pagespeed.jpg](/file/upload/img/events/livestream-tj-gamble-google-pagespeed.jpg?v=aeafa87d7946)
  ### [CSS Critical Path in Magento 1](/md/blog/css-critical-path-in-magento-1/)
  22 January 2020
- ![magento-bytes.jpg](/file/upload/img/blog/magento-bytes.jpg?v=f60b48331561)
  ### [Improved pagespeed: Critical CSS introduced in Magento](/md/blog/improved-pagespeed-critical-css-magento/)
  9 November 2019
- ![poc-truckntow.png](/file/upload/img/poc/poc-truckntow.png?v=1bd10e949d6f)
  ### [TrucknTow.com - Trucking and towing supplier](/md/proof-of-concepts/truckntowcom-trucking-and-towing-supplier/)
  3 January 2020
