yeshuadesign

Carrinho.class.php

Aug 24th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.75 KB | None | 0 0
  1. <?php class Carrinho{
  2.     private $pref = 'media_';
  3.  
  4.     private function existe($id){
  5.         if(!isset($_SESSION[$this->pref.'produto'])){
  6.            $_SESSION[$this->pref.'produto'] = array();
  7.         }
  8.  
  9.         if(!isset($_SESSION[$this->pref.'produto'][$id])){
  10.             return false;
  11.         }else{
  12.             return true;
  13.         }
  14.     }//verifica a existencia do produto e da session como um array
  15.  
  16.     public function verificaAdiciona($id){
  17.         if(!$this->existe($id)){
  18.              $_SESSION[$this->pref.'produto'][$id] = 1;
  19.         }else{
  20.              $_SESSION[$this->pref.'produto'][$id] += 1;
  21.         }
  22.     }//verifica e adiciona mais um produto ao carrinho
  23.  
  24.     private function prodExiste($id){
  25.          if(isset($_SESSION[$this->pref.'produto'][$id])){
  26.               return true;
  27.          }else{
  28.               return false;
  29.          }
  30.     }//verifica se o produto existe
  31.  
  32.     public function deletarProduto($id){
  33.           if(!$this->prodExiste($id)){
  34.                return false;
  35.           }else{
  36.                unset($_SESSION[$this->pref.'produto'][$id]);
  37.                return true;
  38.           }
  39.     }//deletar produto do carrinho de compras
  40.  
  41.     private function isArray($post){
  42.            if(is_array($post)){
  43.                 return true;  
  44.            }else{
  45.                 return false;
  46.            }
  47.     }//verifica se o post passado por parametro é ou não um array
  48.    
  49.     public function qtdProdutos(){
  50.        return count($_SESSION[$this->pref.'produto']);
  51.     }
  52.      
  53.     public function atualizarQuantidades($post){
  54.            if($this->isArray($post)){
  55.                  foreach($post as $id => $qtd){
  56.                      $id = (int)$id;
  57.                      $qtd = (int)$qtd;
  58.                  
  59.                      if($qtd != ''){
  60.                          $_SESSION[$this->pref.'produto'][$id] = $qtd;
  61.                      }else{
  62.                          unset($_SESSION[$this->pref.'produto'][$id]);
  63.                      }
  64.                  }
  65.                  return true;
  66.            }else{
  67.                  return false;
  68.            }//se não for um array
  69.     }//deleta ou atualiza a quantidade referente a um produto no nosso carrinho  
  70.  
  71.     public function setarByPost($post){
  72.            if($this->isArray($post)){
  73.                  foreach($post as $id => $qtd){
  74.                      $id = (int)$id;
  75.                      $qtd = (int)$qtd;
  76.                  
  77.                      if(!isset($_SESSION[$this->pref.'produto'][$id])){
  78.                          $_SESSION[$this->pref.'produto'][$id] = $qtd;
  79.                      }else{
  80.                          $_SESSION[$this->pref.'produto'][$id] += $qtd;
  81.                      }
  82.                  }
  83.                  return true;
  84.            }else{
  85.                  return false;
  86.            }//se não for um array
  87.     }
  88.  
  89.  
  90. public function calcular_frete($cep_origem,
  91.     $cep_destino,
  92.     $peso,
  93.     $valor,
  94.     $tipo_do_frete,
  95.     $altura = 6,
  96.     $largura = 20,
  97.     $comprimento = 20){
  98.  
  99.  
  100.     $url = "http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?";
  101.     $url .= "nCdEmpresa=";
  102.     $url .= "&sDsSenha=";
  103.     $url .= "&sCepOrigem=" . $cep_origem;
  104.     $url .= "&sCepDestino=" . $cep_destino;
  105.     $url .= "&nVlPeso=" . $peso;
  106.     $url .= "&nVlLargura=" . $largura;
  107.     $url .= "&nVlAltura=" . $altura;
  108.     $url .= "&nCdFormato=1";
  109.     $url .= "&nVlComprimento=" . $comprimento;
  110.     $url .= "&sCdMaoProria=n";
  111.     $url .= "&nVlValorDeclarado=" . $valor;
  112.     $url .= "&sCdAvisoRecebimento=n";
  113.     $url .= "&nCdServico=" . $tipo_do_frete;
  114.     $url .= "&nVlDiametro=0";
  115.     $url .= "&StrRetorno=xml";
  116.  
  117.     //Sedex: 40010
  118.     //Pac: 41106
  119.  
  120.     $xml = simplexml_load_file($url);
  121.  
  122.     return $xml->cServico;
  123.  
  124. }
  125.  
  126.    
  127.  
  128. }
  129. ?>
Advertisement
Add Comment
Please, Sign In to add comment