how to make a cart items counter

Asked

Viewed 940 times

2

I want him to take my little cart and see how many products there are there he makes a kind of it that will run while he has products in the cart, more specific it will run by the quantity of the cart if I buy 3 products it will run 3x and so on and want to pick up this variable $i and add as the numbers of the pagseguro ex the pagseguro names the items like this

<input name="itemId1" type="hidden" value="id_produto"> 

where this name itemId1 he has to count gets like this:

<input name="itemId<?php echo $i; ?>" type="hidden" value="id_produto">

I did more or less like this and he ends up going into infinite loop and giving dick in the browser someone can help me?

the name of my Séssion and SESSION['carrinho'].

here and where I create and make the shares for it from add, delete etc

session_start();

      if(!isset($_SESSION['carrinho'])){
         $_SESSION['carrinho'] = array();
      }

      //adiciona produto

      if(isset($_GET['acao'])){

         //ADICIONAR CARRINHO
         if($_GET['acao'] == 'add'){
            $id = intval($_GET['id']);
            if(!isset($_SESSION['carrinho'][$id])){
               $_SESSION['carrinho'][$id] = 1;
            }else{
               $_SESSION['carrinho'][$id] += 1;
            }
         }

         //REMOVER CARRINHO
         if($_GET['acao'] == 'del'){
            $id = intval($_GET['id']);
            if(isset($_SESSION['carrinho'][$id])){
               unset($_SESSION['carrinho'][$id]);
            }
         }

         //ALTERAR QUANTIDADE
         if($_GET['acao'] == 'up'){
            if(is_array($_POST['prod'])){
               foreach($_POST['prod'] as $id => $qtd){
                  $id  = intval($id);
                  $qtd = intval($qtd);
                  if(!empty($qtd) || $qtd <> 0){
                     $_SESSION['carrinho'][$id] = $qtd;
                  }else{
                     unset($_SESSION['carrinho'][$id]);
                  }
               }
            }
         }

      } 

that part and his foreach

foreach($_SESSION['carrinho'] as $id => $qtd){
                              $sql   = "SELECT *  FROM produtos WHERE id_produto= '$id'";
                              $qr    = mysql_query($sql) or die(mysql_error());
                              $ln    = mysql_fetch_assoc($qr);

                              $nome  = $ln['nome'];
                              $preco = $ln['preco'];
                              $sub   = $ln['preco'] * $qtd;
                              $img   = $ln['img'];
                              $desc  = $ln['descricao'];
                              $id    = $ln['id_produto']; 
                              $total += $ln['preco'] * $qtd;

and here’s what I tried to do

<form target="pagseguro" method="post" action="https://pagseguro.uol.com.br/checkout/checkout.jhtml">
          <input type="hidden" name="email_cobranca" value="[email protected]">
          <input type="hidden" name="tipo" value="CP">
          <input type="hidden" name="moeda" value="BRL">
         <?php 
            for($i = 1; count($_SESSION['carrinho']) > 0; $i++){
         ?>
        <input name="itemId<?php echo $i; ?>" type="hidden" value="<?php echo $id[$i]; ?>">  
        <input name="itemDescription<?echo $i; ?>" type="hidden" value="<?php echo $nome[$i]; ?>">  
        <input name="itemAmount<?php echo $i; ?>" type="hidden" value="<?php echo $sub[$i];?>">  
        <input id="leo" name="itemQuantity<?php echo $i; ?>" type="hidden" value="1">  
        <input name="itemWeight<?php echo $i; ?>" type="hidden" value="1000">


          <button  class="btn btn-default check_out" id="comprar">Finalizar Compra</button>
          <?php } ?>
        </form>
  • Post your cart code and how it is stored in the session.

  • I’ve already edited from one look and see if Voce can help me

  • 1 item at a time ?

  • What do you mean, one item at a time? type i have the cart ready as I posted the code there so that when I send pro pagseguro he picks only 1 ai i read in the documentation that if Voce put other inputs hiden with the number 2 will be as I can tell the second product to be add. but this way I tried to do and he always repeats the first product so I wanted to make more dynamic as I’m trying to do now with foreach or for but I’m not getting this giving cart empty.

2 answers

0

Alter:

for($i = 1; count($_SESSION['carrinho']) > 0; $i++){

To:

for($i = 1; $i<=count($_SESSION['carrinho']); $i++){

That way, as long as it is less than or equal to the cart count, it will loop.

  • here also continues with the same error as mine of empty cart

-1

Do it like this:

<form target="pagseguro" method="post" action="https://pagseguro.uol.com.br/checkout/checkout.jhtml">
<input type="hidden" name="email_cobranca" value="[email protected]">
<input type="hidden" name="tipo" value="CP">
<input type="hidden" name="moeda" value="BRL">
<?php 
if($_SESSION['carrinho']){
foreach ($_SESSION['carrinho'] as $key => $value){    
?>
    <input name="itemId<?php echo $key; ?>" type="hidden" value="<?php echo $value['id']; ?>">  
    <input name="itemDescription<?echo $key; ?>" type="hidden" value="<?php echo $value['nome']; ?>">  
    <input name="itemAmount<?php echo $key; ?>" type="hidden" value="<?php echo $value['sub'];?>">  
    <input id="leo" name="itemQuantity<?php echo $key; ?>" type="hidden" value="1">  
    <input name="itemWeight<?php echo $key; ?>" type="hidden" value="1000">
<?php } ?>

<button  class="btn btn-default check_out" id="comprar">Finalizar Compra</button>
<?php } ?>
</form>
  • error it in pay safe that the cart is empty

Browser other questions tagged

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