簡體   English   中英

使用php將產品添加到購物車后如何更改添加到購物車按鈕

[英]how to change add to cart button after adding product to the cart using php

我不知道該怎么做,在用戶將特定產品添加到購物車后,我想更改添加到購物車按鈕,我的購物車在會話中工作

  <?php require'admin/dist/db.php';
  $sql = " SELECT * FROM products";
  $res = $conn->query($sql);
  if ($res->num_rows > 0) {
  while ($row = $res->fetch_assoc()) {
  $id=$row['id'];
  $_SESSION['product_id']=$id;?>
  <div class="cards ">
    <div class="wrapper">
      <div class="colorProd"></div>
      <div class="imgProd" style="background-image: url(img/product/<?echo $row['img_path'];?>);"></div>
      <div class="infoProd">
        <p class="nombreProd"><?echo $row['product_name'];?></p>
        <p class="extraInfo"></p>
        <div class="actions">
          <div class="preciosGrupo">
            <p class="precio precioOferta">9,999</p>
            <p class="precio precioProd"><?echo $row['product_price'];?>
            </p>
          </div>
          <div class="icono action aFavs" title="Add TO Wishlist">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
              <path d="M47 5c-6.5 0-12.9 4.2-15 10-2.1-5.8-8.5-10-15-10A15 15 0 0 0 2 20c0 13 11 26 30 39 19-13 30-26 30-39A15 15 0 0 0 47 5z">
              </path>
            </svg>
          </div>
           <form action="" method="post">
          <label >
            <button  class="  pointer addtocart " id="<? echo $row['id'];?>" type="submit" >
            </button>
            <div class="icono action alCarrito">
              <!-- alCarrito -->
              <svg class="inCart" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
                <title>Added To Cart !</title>
              <path d="M30 22H12M2 6h6l10 40h32l3.2-9.7"></path>
              <circle cx="20" cy="54" r="4"></circle>
              <circle cx="46" cy="54" r="4"></circle>
              <circle cx="46" cy="22" r="16"></circle>
            <path d="M53 18l-8 9-5-5"></path>
          </svg>
          <svg class="outCart" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
            <title>Add To Cart</title>
          <path d="M2 6h10l10 40h32l8-24H16"></path>
          <circle cx="23" cy="54" r="4"></circle>
          <circle cx="49" cy="54" r="4"></circle>
        </svg>
      </div>
    </label>
    <input type="hidden" name="id" value="<?echo $row['id'];?>">
    <input type="hidden" name="name" value="<?echo $row['product_name'];?>">
    <input type="hidden" name="price" value="<?echo $row['product_price'];?>">
  </form>
</div>
</div>
</div>
</div>
<? }}?>
</div>
<?if(isset($_POST['id']))
{$name = $_POST['name'];
$price = $_POST['price'];
$id = $_POST['id'];
$cart  = array('id' => $id,'name' => $name,'price' => $price);
$_SESSION['cart'][$id]= $cart;
}?>

我想要做的只是告訴 php 已經添加的產品的購物車按鈕更改了我數據庫中的這 3 張卡片輸出,在用戶將產品添加到購物車后,我想顯示添加到購物車的按鈕,就像這樣看到購物車 svg 我將產品添加到購物車后,wana 像這樣更改它

如果我在<div class="cards enCarrito">添加這個類,我有這個 css enCarrito類然后購物車 svg 改變了,就像我在第二張圖片中上傳的一樣

謝謝 !

這個答案基於很多猜想,並假設您在購物車中添加了一些東西后頁面會重新加載。

我假設您將在會話中存儲您的購物車,如: $_SESSION['cart'][] = $_POST['id']

現在,在(重新)構建頁面時,您可以使用in_array函數來確定當前正在構建的項目是否已經在購物車中。

while ($row = $res->fetch_assoc()) {
  $id=$row['id'];
  $inCart = (in_array($id, $_SESSION['cart']) ? 'enCarrito' : '';
?>
  <div class="cards <?= $inCart ?>">

假設您希望使用 JQuery 執行此操作,您應該為您的 div 提供一個正確的 id,如<div id='product_<?= $id ?>'> ,然后使用$('#product' + ID).addClass('enCarrito');添加類$('#product' + ID).addClass('enCarrito'); https://api.jquery.com/addClass/ 所述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM