Quantity of products to be differentiated in the shopping cart

Asked

Viewed 306 times

0

Colleagues.

Forgive me if I couldn’t express what I really want in the title, because I couldn’t find a correct definition unless I explained it here.

I have a shopping cart where the products are listed according to the customer’s purchase and are stored in a BD table, but I am using a feature via Jquery that shows the quantity of products and changes by clicking the + or -button. See the image below:

inserir a descrição da imagem aqui

The code is this for each product:

HTML

 <div class="quantity">
       <div class="quantity-select">
           <div class="entry value-minus">&nbsp;</div>
           <div class="entry value"><span>1</span></div>
           <div class="entry value-plus active">&nbsp;</div>
       </div>
    </div>

JQUERY

$('.value-plus').on('click', function(){
var divUpd = $(this).parent().find('.value'), newVal = parseInt(divUpd.text(), 10)+1;
if(newVal < 8) divUpd.text(newVal);
  var valor = $(this).closest('tr').find('td[data-nome]').data('nome'); // Funcional
  trocar = valor.replace(",",".");  
  valorTTotal =  document.getElementById("total").innerHTML;
  valorTotal = trocar + 1;
  vv = parseFloat(valorTotal) + parseFloat(valorTTotal);  
  document.getElementById("total").innerHTML = vv.toFixed(2);
  document.getElementById("subtotal").innerHTML = vv.toFixed(2);
  $.ajax({
      type: "GET",
      dataType: 'json',
       data:{ valor: vv.toFixed(2) },
      url: "atualizar-carrinho.php",
      success: function(resposta){
     }
   });


  var divUpd = $(this).parent().find('.value'), newVal1 = parseInt(divUpd.text(), 10)+1;
if(newVal1 < <?php echo $numero; ?>) divUpd.text(newVal);
  var valor = $(this).closest('tr').find('td[data-nome]').data('nome'); // Funcional
  trocar = valor.replace(",",".");  
  valorTTotal =  document.getElementById("total").innerHTML;
  valorTotal = trocar + 1;
  vv = parseFloat(valorTotal) + parseFloat(valorTTotal);  
  document.getElementById("total").innerHTML = vv.toFixed(2);
  document.getElementById("subtotal").innerHTML = vv.toFixed(2);
  $.ajax({
      type: "GET",
      dataType: 'json',
       data:{ valor: vv.toFixed(2) },
      url: "atualizar-carrinho.php",
      success: function(resposta){
     }
   });       
});

The line:

if(newVal < 8) divUpd.text(newVal);

It shows the amount that the fields will have, but here comes my challenge. Each product coming from the BD has its own quantity. How would I integrate into this code? The integration would be the results coming from the database gets PHP.

<?php
 while($jmProdutos = mysqli_fetch_object($sqlProdutos)){
    .....
   $qtdProduto = $jmProdutos->Quantidade;
}

1 answer

1


From what I understand you are storing in the database the status of each customer’s cart, correct?

You need to include in HTML the primary key of the purchase and pass it on Ajax that you are sending to the server, then just update. I only recommend using the method POST in the Ajax for the sake of good practice (GET not to be used to modify the database).

Example:

HTML

<div class="produto" data-id="123">
   [Nome do produto, preço, imagem etc]
   <input name="qtd" value="1" /> 
   <button id="qtdup">+</button>
</div>

JS

$('#qtdup').on('click', function(ev){
   [aumenta o número no input qtd e pega o número, como já foi feito na pergunta]
   var id_do_produto = $('#contaup').parent().data('id'); // aqui basta navegar pelo DOM e encontrar a div, tr ou o que você estiver usando para guardar o id do produto no carrinho
   $.post({
      type: "POST",
      dataType: 'json',
       data:{ id: id_do_produto, qtd: valor_de_qtd },
      url: "atualizar-carrinho.php",
      success: function(resposta){
     }
   });
});

PHP

<?php
 $produto_id = mysqli_real_escape_string($_POST['id']);
 $produto_qtd = mysqli_real_escape_string($_POST['qtd']);
 $sqlProdutos = $mysqli_query($conn, "SELECT * FROM carrinho_produtos WHERE id_carrinho = $produto_id");
 while($produtos = mysqli_fetch_object($sqlProdutos)){
    .....
   $qtdProduto = $produtos->Quantidade;
   $qtdProduto = $produto_qtd;
}
 mysql_query($conn, "UPDATE carrinho_produtos SET Quantidade = $produto_qtd WHERE id_carrinho = $produto_id");
  • Hello Daniel. Thank you for the return, but unfortunately I could not understand it. Could you give me an example?

  • Of course, just tell me one thing: what columns do you have in the order database? Have a ID or something similar?

  • Well, I made the example thinking of things that everyone has in the database and using mysqli as you already seem to be used to.

  • Thank you Daniel

Browser other questions tagged

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