I have a very big dilemma in a virtual store that I am developing for a customer about freight. I have seen several examples of freight calculations with PHP, however the same are only for one product, but when the user increases the quantity of a product and / or does it include another product in the cart?
Certainly the values required by the Post are: Width, Height, Weight and Length. Based on this link , I thought I found the solution, so I did it this way:
// Calculo o peso
$multP = $jmProdutos->PesoProduto * $jmSomarQ->QuantidadeProd;
$cubP = pow($multP,1/3);
$parametros['nVlPeso'] = round($cubP);
// Calculo o Comprimento
$multC = $jmProdutos->Comprimento * $jmSomarQ->QuantidadeProd;
$cubC = pow($multC,1/3);
$parametros['nVlComprimento'] = round($cubC);
// Calculo a Altura
$multA = $jmProdutos->Altura * $jmSomarQ->QuantidadeProd;
$cubA = pow($multA,1/3);
$parametros['nVlAltura'] = round($cubA);
// Calculo a Largura
$multL = $jmProdutos->Largura * $jmSomarQ->QuantidadeProd;
$cubL = pow($multL,1/3);
$parametros['nVlLargura'] = round($cubL);
In some research, I saw that PHP has a native pow () function that does the cubic calculation. In the test below:
IhadthisanswerfromthePostOffice:
Thewidthcannotbelessthan11cm.Thewidthcannotbe lessthan11cm.
Ontheotherhand,Ihaveadapted our colleague's answer in another forum in my code:
$frete = array();
$multP = $jmProdutos->PesoProduto * $jmSomarQ->QuantidadeProd;
$multC = $jmProdutos->Comprimento * $jmSomarQ->QuantidadeProd;
$multA = $jmProdutos->Altura * $jmSomarQ->QuantidadeProd;
$multL = $jmProdutos->Largura * $jmSomarQ->QuantidadeProd;
$somarCubagem = $multP + $multC + $multA + $multL;
$raizCubica = round(pow($somarCubagem,(1/3)));
$frete['peso'] = $multP;
if($raizCubica < 16){
$frete['comprimento'] = floatval(16);
}else{
$frete['comprimento'] = floatval($raizCubica);
}// em centimetros
if($raizCubica < 11){
$frete['largura'] = floatval(11);
}else{
$frete['largura'] = floatval($raizCubica);
}// em centimetros
$frete['altura'] = round($somaCubagem / ($frete['cumprimento']*$frete['largura'])); // em centimetros
$frete = $frete;
$parametros['nVlPeso'] = $frete['peso'];
$parametros['nVlComprimento'] = $frete['comprimento'];
$parametros['nVlAltura'] = $frete['largura'];
$parametros['nVlLargura'] = $frete['largura'];
It did not bring me any errors, see:
But I am in doubt as to whether this calculation is correct and what is the correct way to calculate freight if there are multiple quantities of products in a cart.
Does anyone know how the post calculations work in case the cart has more than one product and / or quantity or would it have an example?