将商品添加到购物车时,相同商品的商品尺寸不同

时间:2016-03-14 17:15:02

标签: javascript php jquery cart

我正在创建在线商店,但我在购物车产品方面存在一些问题。

如果我添加2个产品相同且不同的产品尺寸 此产品仅显示最后添加到购物车。

示例我想要这样

  • 产品:ABC

    尺寸:XL

  • 产品:SKSD

    尺寸:L

我想要这样。

这是我的代码

order.php

<?php 
    include "connection.php";
    $link = $_REQUEST['link_goods'];
    $data_pr = mysqli_query($con,"select * from goods where link_goods='".$link."' ");
    $pr = mysqli_fetch_object($data_pr);
?>
  <section class="main-container col1-layout">
    <div class="main container">
      <div class="col-main">
        <div class="row">
          <div class="product-view">
            <div class="product-essential">
              <form action="#" method="post" id="product_addtocart_form">   
                <div class="product-shop col-lg-12 col-sm-12 col-xs-12">
                  <div class="product-name">
                    <h4><?php echo $pr->goods ?></h4>
                  </div>
                  <p class="availability in-stock">Availability: <span><?php echo $pr->stock ?> stock</span></p>
                  <div class="price-block">
                    <div class="price-box">
                      <p class="special-price"> <span class="price-label">Special Price</span> <span class="price"> Rp <?php echo $pr->price ?> </span> </p>
                    </div>
                  </div>
                  <div class="add-to-box">
                    <div class="add-to-cart">
                      <label for="qty">Quantity & Size</label>
                      <div class="pull-left">
                        <div class="custom pull-left">
                          <button onClick="var result = document.getElementById('qty'); var qty = result.value; if( !isNaN( qty ) &amp;&amp; qty &gt; 1 ) result.value--;return false;" class="reduced items-count" type="button"><i class="icon-minus">&nbsp;</i></button>
                          <input type="text"  class="input-text qty" readonly style="color:black"  value="1" maxlength="5" id="qty" name="qty">
                          <button onClick="var result = document.getElementById('qty'); var qty = result.value;   if(qty>=<?php echo $pr->stock ?>) return false; else if( !isNaN( qty )) result.value++; return false;" class="increase items-count" type="button"><i class="icon-plus">&nbsp;</i></button>
                        </div>

                        <div class="custom pull-left">
                            <select style="color:black" name="size" id="size" class="input-text qty">
                                <option value="M">M</option>
                                <option value="L">L</option>
                                <option value="XL">XL</option>
                            </select>
                        </div>
                        <?php
                            echo "<input type='hidden' id='price' name='price' value='".$pr->price."'>";
                            echo "<input type='hidden' id='id_goods' name='id_goods' value='".$pr->id_goods."'>";
                        ?>
                      </div>
                      <?php 
                        if ($pr->stock=="0")
                        { 

                        }
                        else 
                        { 
                      ?>
                        <button  class="button btn-cart" title="Add to Cart" type="submit"><span><i class="icon-basket"></i> Add to Cart</span></button>
                        <?php } ?>
                     </div>
                  </div>
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </section>

<script src="js/jquery.js"></script>
<script>
$(document).ready(function(){   
        $("form").submit(function(e){
            var form_data = $(this).serialize();
            var button_content = $(this).find('button[type=submit]');
            button_content.html(' ADDING...'); //Loading button text 

            $.ajax({ 
                url: "proses.php",
                type: "POST",
                dataType:"json", 
                data: form_data
            }).done(function(data){ 
                $("#myinfo").html(data.items); 
                button_content.html(' ADD TO CART'); 
                if($(".shopping-cart-box").css("display") == "block"){
                    $(".mycart-box").trigger( "click" ); 
                }
                $(".mycart-box").trigger( "click" ); 
            })
            e.preventDefault();
        });
});
</script>

proses.php

 <?php 
    session_start(); //start session
    $mysqli_conn = new mysqli("localhost", "root", "", "databaseshop"); 
    if ($mysqli_conn->connect_error) {
        die('Error : ('. $mysqli_conn->connect_errno .') '. $mysqli_conn->connect_error);
    }


    if(isset($_POST["id_goods"]))
    {
        // i think's using condtion in here when different size product
        foreach($_POST as $key => $value){
            $new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING); 
        }

        $statement = $mysqli_conn->prepare("SELECT goods, images_goods, price FROM goods_table WHERE id_goods=? LIMIT 1");
        $statement->bind_param('s', $new_product['id_goods']);
        $statement->execute();
        $statement->bind_result($goods, $images_goods, $price);


        while($statement->fetch()){ 
            $new_product["goods"] = $goods; 
            $new_product["images_goods"] = $images_goods;  
            $new_product["price"] = $price;     

// i think's using condtion in here when different size product
            $_SESSION["products"][$new_product['id_goods']] = $new_product;     
        }

        $total_items = count($_SESSION["products"]); 
        die(json_encode(array('items'=>$total_items)));

    }

    //################## Loop Item Cart #####################################
    if(isset($_POST["load_cart"]) && $_POST["load_cart"]==1)
    {

        if(isset($_SESSION["products"]) && count($_SESSION["products"])>0){ 
              $cart_box  = '  <div class="top-cart-content arrow_box">';
              $cart_box .= '        <ul id="cart-sidebar" class="mini-products-list">';
              $total = 0;
              foreach($_SESSION["products"] as $product)
              { 
                $product_name  = $product["goods"]; 
                $product_cover = $product["images_goods"];
                $product_price = $product["price"];
                $product_code  = $product["id_goods"];
                $product_qty   = $product["qty"];
                $product_size  = $product["size"];

                    $cart_box .= '       <li class="item even remove-item">';
                    $cart_box .= '              <a class="product-image" href="#" title="'.$product_name.'"><img alt="'.$product_name.'" src="images/barang_sampul/'.$product_cover.'" width="80"></a>';
                    $cart_box .= '                    <div class="detail-item">';
                    $cart_box .= '                          <div class="product-details"><a href="#" data-code="'.$product_code.'" title="Remove This Item"  class="glyphicon glyphicon-remove remove-item">&nbsp;</a>';
                    $cart_box .= '                              <p class="product-name"> <a href="product_detail" title="'.$product_name.'">'.$product_name.'</a> </p>';
                    $cart_box .= '                          </div>';
                    $cart_box .= '                          <div class="product-details-bottom"> <span class="price">'.$product_price.'</span> <span class="title-desc">Qty:</span> <strong>'.$product_qty.'</strong> </div>';
                    $cart_box .= '                          <div class="product-details-bottom"> <span class="title-desc">Ukuran :</span> <strong>'.$product_size.'</strong> </div>';
                    $cart_box .= '                    </div>';
                    $cart_box .= '       </li>';

                $subtotal = ($product_price * $product_qty);
                $total = ($total + $subtotal);
            }
             $cart_box .= '         </ul>';
             $cart_box .= '         <div class="top-subtotal">Subtotal: <span class="price">'.$total.'</span></div>';
             $cart_box .= '         <div class="actions">';
             $cart_box .= '           <center><a href="shopping_cart"><button style="float:none" class="view-cart" type="button"><span>View Cart</span></button></a></center>';
             $cart_box .= '         </div>';
             $cart_box .= '       </div>';

            die($cart_box); 
        }else{
            // die("Null"); 
        }
    }

    ################# Delete Item From Cart ################
    if(isset($_GET["remove_code"]) && isset($_SESSION["products"]))
    {
        $product_code   = filter_var($_GET["remove_code"], FILTER_SANITIZE_STRING);

        if(isset($_SESSION["products"][$product_code]))
        {
            unset($_SESSION["products"][$product_code]);
        }

        $total_items = count($_SESSION["products"]);
        die(json_encode(array('items'=>$total_items)));
    }
    ?>

帮助我谢谢

0 个答案:

没有答案