Calculate post office freight for various products

I am trying to calculate the freight of several products using the WebService of the post office , but I am having doubts/difficulty to make the calculation when I have several products.

I am using PHP code to call the webservice and receive the return in XML. That call is correct. I can call and receive the values correctly. My doubt is more in the logic of how to calculate and process when I own a cart of purchases with more than one product.

I know that I must pay attention to the calculation based on dimensions (height, width, length and weight), I also know that in this case the calculation is made based on Cubic weight, as per documentation.

But the parameters that I must fill to perform 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 the dimensions so "raw"? Ex.: 2 products size:

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

And send these summaries?

When I do this I can even get the result, but it does not seem right, and sometimes the difference in shipping is very large. Ex.:

  • product shipping to: R R 18,00
  • shipping product B: R R 21,00
  • shipping sum A + B: R 8 88,00

Someone has experience with this type of service that can help me?


To facilitate understanding, the data regarding the product(s) that the post office webservice receives are:

  • nvlweight : ex.: 4 (for 4Kg);
  • nvllength : ex.: 10 (for 10cm);
  • nvlheight : ex.: 15 (for 15cm);
  • nvll width : ex.: 20 (for 20cm);
  • nvldiameter : * No required

There is, for example, no option for more than one item, only that data.

Author: celsomtrindade, 2017-10-17

4 answers

About packaging

The main point of difficulty I saw in your question was at the time of sending the package dimensions. I will do in 2D to simplify.

Imagine that you want to send two sugar Union:

Union sugar

It has dimension two fingers by five fingers, for 5G content:

two fingers five fingers

Add the dimensions the way you were doing it was to do like this:

putting in the wrong way

Or so:

putting in the wrong way 2

In the first misguided way, of the 49 square fingers (7 Fingers by 7 Fingers) in the package, only 20 square fingers are actually being used. Note The Square without anything greater than 25 square fingers, and the smaller of 4 square fingers.

In the second packaging, the total area is 40 square Fingers (4 Fingers by 10 fingers). Note that for each pack of 10 square fingers, it has an area empty right on the side of the same size.

The best packaging for you would be:

4 Fingers by 5 fingers

Or this:

2 fingers per 10 fingers

I don't know if the Post Office of quadradonia takes into account the area only, or if they take into account the individual dimensions. I would ask the webservice of the Post Office of quadradônia both options if I could send these requests.

Now, imagine you want to send a Union sugar is a Salt Sosal:

sosal

The Salt has measures 2 fingers by 3 fingers for 1g (didactic and approximate measures, I will not post the photo here).

So, you can pack in these four ways the sugar and the salt:

4x5 5x5 8x2 7x3
4 by 5 5 by 5 8 by 2 7 by 3

See that 5x5 is not the best choice, it is 100% dominated by 4x5 in all dimensions considered. Now 8x2 has 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 believe that for 3D or higher dimensions it is very different

 18
Author: Jefferson Quesado, 2021-02-02 00:48:28

After some time reading a lot of material online and thoroughly researching on the subject, I think I was able to come to a satisfactory result.

As already discussed here (and also a related question in meta) I know that 2D or 3D packaging will be something extremely difficult (or impossible), but, as said in the question, my biggest doubt was given to the fact of how to calculate the freight for one or more products so that one of the following is not done proposals:

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

Another problem found was how to add the information of several products and the webservice of The Post Office accepts only one value for each of the fields.

In this way I was able to arrive at a calculation where, so far, I believe it is returning values much closer to the actual values of the freight than with the method used previously.

Note: I did not get to officially test by calculating by code and checking the post office counter 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 of the sum of cm³ of all products;

For these calculations, I am 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,
);

Thus, at the time of filling in the data to be sent to the post office, I fill in as follows:

// 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 managed to achieve results where calculating the value of the individual freight of 2 products I obtained the values:

  • Product A: R R 17,10
  • Product B: R R 20,40

And when performing both products together I obtained R$23,60 (considering that by the measures and type of package I did not get value less than R R 17,10-it must be minimum rate).

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

Remembering that there are some outstanding features, such as validating the measures so that it does not exceed the maximum values and, if it exceeds, divide the freight into more than one"box".

 6
Author: celsomtrindade, 2018-03-01 10:52:52

You need to add only one dimension, for example height, and take the larger value of 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 summed, but the maximum width and maximum length remain the same. Adding everything together, as in the initial reasoning, the volume of the products rises to the cube. Instead of calculating the 2 packages, it would be calculating the volume of 8 packages.

 0
Author: , 2018-02-25 04:23:27

Good evening, resurrecting deceased, rsrsrsrs but I will post my contribution q it may be that it will help someone in the future.

I'm developing a system for a natural products company, they have teas, encapsulated soluble products.. well varied. I managed to solve this difficulty by converting the measurement of each product to cm3 (cubic centimeter height x Width x length) Then I am the measure of each product. At the end I take the square root of the result and step as height, width and length. Hit right

Example: product with c15 A13 l8 so this product has 1560cm3 If send 3 units will be 4680m3 other product with c20 15 l13 it will have 1300cm3 sending 2 units will be 2600m3

Adding the two subtotals we get to 7280 the cubic root of it is 19.38, as the box is always greater than the volume of products, I added 10% to this value that gives the value of 21.32 cm. So I do the calculation about 21.32 in height, 21.32 in width and 21.32 in length.

Obs I put an IF for each measurement to meet the minimum standard of the post office. For example if the result is less than 2, the height variable will receive the value of 2 which is the minimum height required by the mail.

I hope it helps if anyone.

 -1
Author: R MG, 2021-02-02 00:11:14