After some time reading much online material and researching thoroughly on the subject, I think I managed to arrive at a satisfactory result.
As already discussed here (and also a question concerning the goal) I know that the packaging 2D or 3D will be something extremely difficult (or impossible), but, as stated in the question, my biggest doubt was given to the fact of how to calculate the freight for one or more products so that it is not made one of the following proposals:
- Sum of Frete A+Frete B;
- Sum of the measures of Produto A+Produto Band calculation of freight;
Another problem found was how to add the information of various products and the webservice Post accepts only one value for each of the fields.
In this way I managed to arrive at a calculation where, until now, I believe I am returning values much closer to the actual values of freight than with the method previously used.
Note: I did not get to test officially by performing the calculation by code and checking at 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 up the value of cm³and weight of all products;
- Extraction of the cubic root from the sum of cm³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,
);
So, 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 obtained the values:
- Product A: R$17,10
- Product B: R$20,40
And when running the two products together I got R $ 23,60 (considering that the measures and type of package did not get less than R $ 17,10 - should 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.
Recalling that there are some pending functionalities, such as the validation of the measures so that it does not exceed the maximum values and, if it exceeds, divide the freight in more than one "box".
							
							
						 
The best is to try to assemble in 3D (or 2D for didactic purposes) as the product would look together. You will see that adding up in the three dimensions is having too much empty space
– Jefferson Quesado
Is using mail packaging or own?
– Randrade
@Randrade At first using only own packaging. Some products already have factory packaging.
– celsomtrindade
What happens when the product already has its own packaging? You would group it into a single package or send it in separate packages?
– Randrade
@Randrade is difficult to answer, because it depends a little. Some cases can only be "tied up", or, for example, wrapped with brown paper. Some, from what I’ve seen, don’t go by the post office rules, so it would have to be separate packages. Already others are small and you can put several in one box.
– celsomtrindade
@Randrade to put in context, are petshop products. From medicines, toys, feed bags (1,3,5,10kg), etc..
– celsomtrindade