Calculate postal freight for various products


I am trying to calculate the freight of several products using the WebService of the mails , however I have doubts / difficulty to carry out the calculation when I have several products.


I'm using the PHP code to call the webservice and get the XML return. This call is correct. I can call and receive the values correctly. My question is more in the logic of how to calculate and process when I have a shopping cart with more than one product.

I know that I must pay attention to the calculation based on the dimensions (height, width, length and weight), I also know that in this case the calculation is made on the basis of cubic weight,

But the parameters that I have to fill in to do the calculation do not provide a field to send more than one product. In this case, how should the calculation be done?

Do I sum up the dimensions "raw"? Eg 2 products of dimensions:

  • A: 10cm, L: 15cm, C: 22cm
  • A: 8cm, L: 31cm, C: 18cm
  • Total: A: 18cm, L: 46cm, C: 40cm

And I'm sending those sums?

When I do this I even get the result, but it does not seem right, and sometimes the freight difference is too great. Ex.:

  • Shipping Product A: $ 18.00
  • Product Freight B: R $ 21,00
  • A + B sum freight: R $ 88,00

Does anyone have experience with this type of service that can help me?

To facilitate the understanding, the data regarding the product (s) that the webservice of the mails receive are:

  • nVlPeso : eg: 4 (for 4Kg);
  • nVl Length : eg 10 (for 10cm);
  • nVlAlture : eg: 15 (for 15cm);
  • nVlLargura : eg: 20 (for 20cm);
  • nVlDiametro : * not required

There is, for example, not an option for more than one item, just this data.

asked by anonymous 17.10.2017 / 16:15

3 answers


About packaging

The main point of difficulty I saw in your question was when sending the dimensions of the package. I'll do it in 2D for simplicity.

Imagine you want to send two Sugar Union:
















See that 5x5 is not the best choice, it is 100% dominated by 4x5 in all dimensions considered. Now, 8x2 has a total area of 16 square fingers, being a smaller total area option than 4x5.


Later I put an algorithm of how to do this packaging in 2D, but I think that for 3D or higher dimensions is very different

17.10.2017 / 17:18

After some time reading a lot online material and thoroughly researching the subject, I think I have managed to come up with a satisfactory result.

As I've discussed here (and also a meta-related question) I know that 2D or 3D packaging is going to be extremely difficult (or impossible), but, as stated in the question, my biggest question was how to calculate the freight for one or more products so that one of the following proposals is not made:

  • Sum of Frete A + Frete B ;
  • Sum of measures of Produto A + Produto B and freight calculation;

Another problem encountered was how to add the information of several products, with the webservice of the Post Office accepting only one value for each of the fields.

In this way I have been able to arrive at a calculation where, to date, I believe that you are returning values much closer to the actual values of the freight than with the previously used method.


Note: I did not officially test the code by checking the post office at the time of sending the product.

The logic behind the calculation is as follows:

  • Calculate the value in cm³ for each product (Width x Height x Length x Quantity);
  • Add the value of cm³ and weight of all products;
  • Extraction of the cubic root from the sum of% with% of all products;

For these calculations, I'm using this code:

$total_peso = 0;
$total_cm_cubico = 0;

 * $produto = lista de produtos no carrinho de compras. Deve possuir,
 * obrigatoriamente, os campos largura, altura, comprimento e quantidade.
foreach ($produto as $row) {
    $row_peso = $row['peso'] * $row['quantidade'];
    $row_cm = ($row['altura'] * $row['largura'] * $row['comprimento']) * $row['quantidade'];

    $total_peso += $row_peso;
    $total_cm_cubico += $row_cm;

$raiz_cubica = round(pow($total_cm_cubico, 1/3), 2);
$resposta = array(
    'total_peso' => $total_peso,
    'raiz_cubica' => $raiz_cubica,

In this way, when filling in the data to be sent to the Post Office, I fill in the following way:

// Os valores 16, 2, 11 e 0.3 são os valores mínimos determinados pelo serviço dos Correios
$comprimento =  $raiz_cubica < 16 ? 16 : $raiz_cubica;
$altura = $raiz_cubica < 2 ? 2 : $raiz_cubica;
$largura = $raiz_cubica < 11 ? 11 : $raiz_cubica;
$peso = $total_peso < 0.3 ? 0.3 : $total_peso;
$diametro = hypot($comprimento, $largura); // Calculando a hipotenusa pois minhas encomendas são retangulares

'nVlPeso'        => $peso,
'nVlComprimento' => $comprimento,
'nVlAltura'      => $altura,
'nVlLargura'     => $largura,
'nVlDiametro'    => $diametro, // não obrigatório

With this calculation I was able to achieve results where calculating the individual freight value of 2 products I got the values:

  • Product A: R $ 17.10
  • Product B: R $ 20.40

And when I made the two products together, I got R $ 23.60 (considering that by the measures and type of package I did not get a value lower than R $ 17.10 - it should be a minimal fee).

This type of result was repeated (similarly) in several different products and different quantities, that is, there was no freight sum but a gradual increase according to the measurements and weight of the products.

Remembering that there are some outstanding features, such as validating the measurements so that they do not exceed the maximum values and, if you exceed, divide the freight into more than one "box".

22.02.2018 / 23:09

You need to add only one dimension, for example height, and get the larger value from the other two.

A: 10cm, L: 15cm, C: 22cm

A: 8cm, L: 31cm, C: 18cm

Total: A: 18cm, L: 31cm, C: 22cm

Suppose you pack one box on top of the other. The height will be added, but the maximum width and the maximum length remain the same. To sum it all up, as in the initial reasoning, the volume of products rises to the cube. Instead of calculating the 2 packages, you would be calculating the volume of 8 packages.

25.02.2018 / 05:23