Calculate total cart in php

0

I have a question I made a shopping cart and the freight calculation using the webservice of the post office. Now I need the freight to add up to the total amount but I can not. Could anyone help me?

Cart code:

foreach($_SESSION['carrinho'] as $id => $quantidade)
            {
                $sql = "SELECT * FROM produtos WHERE id = '$id'";
                $query = mysql_query($sql);

                while($row = mysql_fetch_assoc($query)) 
                {
                    $produto = $row['nome'];
                    $preco = $row['preco'];
                    $id = $row['id'];
                    $altura = $row['altura'];
                    $largura = $row['largura'];
                    $comprimento = $row['comprimento'];
                    $peso = $row['peso'];
                    $cepOrigem = $row['cepOrigem'];

                    $sub = $preco * $quantidade;
                    $total = $total + $sub;

                    $alturaTotal = $alturaTotal + $altura;
                    $pesoTotal = $pesoTotal + $peso;

                    if($maiorLargura < $largura = $row['largura'])
                    $maiorLargura = $largura = $row['largura'];

                    if($maiorComprimento < $comprimento = $row['comprimento'])
                    $maiorComprimento = $comprimento = $row['comprimento'];

Postal code:

require_once("carrinho.php");

// post variaveis
    $data['sCepDestino'] = $_POST['sCepDestino'];
    $data['nCdServico'] = $_POST['nCdServico'];

// variaveis de tamanho
    $data['nVlAltura'] = $_POST['nVlAltura'];
    $data['nVlLargura'] = $_POST['nVlLargura'];
    $data['nVlComprimento'] = $_POST['nVlComprimento'];
    $data['nVlPeso'] = $_POST['nVlPeso'];

// variaveis obrigatórias
    $data['sCepOrigem'] = $_GET['sCepOrigem'] = $cepOrigem;
    $data['nCdEmpresa'] = '';
    $data['sDsSenha'] = '';
    $data['nCdformato'] = '1';
    $data['nVlDiametro'] = '0';
    $data['sCdMaoPropria'] = 'n';
    $data['nVlValorDeclarado'] = '0';
    $data['sCdAvisoRecebimento'] = 'n';
    $data['StrRetorno'] = 'xml';

// código do correio para enviar e receber dados  
    $data = http_build_query($data);

    $url = 'http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx';
    $curl = curl_init($url . '?' . $data);
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);

    $result = curl_exec($curl);
    $result = simplexml_load_string($result);

    foreach($result -> cServico as $row)
    {
        if($row -> Erro == 0)
        {
            $frete = $row -> Valor;
            $entrega = $row -> PrazoEntrega;
            echo "<table id='FreteTable'>";
                echo "<tr>";
                    echo "<td>Frete</td>";
                    echo "<td>$frete</td>";
                echo "</tr>";

                echo "<tr>";
                    echo "<td>Prazo de entrega</td>";
                    echo "<td>$entrega</td>";
                echo "</tr>";   
            echo "</table>";
        }
        else
        {
            echo $row -> MsgErro;   
        }
    } 
       ?>
    
asked by anonymous 16.07.2015 / 22:12

1 answer

4

To use Post Api , the best way is to build a function that will only bring information from arrays , without html involved.

Then do the loop to list in% with% as% as generated by the query.

As soon as the user selects what kind of freight he wants, checkbox , array etc, save the service code to SEDEX , PAC or variável de sessão and to calculate, make the query again sending the cep, kg, dimensions etc, the array will always be the same. The difference to know which freight was selected, use the service code as a filter, and to make this filter, use the in_array to keep the freight selected, then add, because the way you are doing, there is no possibility to control this information.

I hope I have helped! ;)

    
20.07.2015 / 13:36