Calculate total cart in php

Asked

Viewed 944 times

0

I’m with a question I made a shopping cart and the freight calculation using the webservice from the post office. Now I need freight to add to the total amount more I’m not getting.Someone could 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;   
        }
    } 
       ?>

1 answer

5


To use Api dos Correios, the best way is to assemble a function that brings only the information of the arrays, without html involved.

After that, do the loop to list in checkbox the array generated by the query.

As soon as the user selects what type of freight he will want, SEDEX, PAC etc., keep the code of the service in a variável de sessão, cookie or banco de dados, and to calculate, make the query again by sending the zip, kg, dimensions etc, the array will always be the same. The difference to know which freight has been selected, use the service code as a filter, and to effect this filter, use the function 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’ve helped! ;)

  • Just like I’m gonna do the consult again?

  • @Leonardosilva Send the ZIP code and dimensions again, with each update in the cart, your filter for the chosen type of freight is the service code, but the array is the same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.