如何在购物车中添加所选产品的尺寸和颜色?

时间:2019-04-07 16:13:38

标签: php mysql html5 session

我正在在线服装店的电子商务网站上工作,现在我试图在购物车中添加选定的产品选项(颜色,尺寸,数量等),但它会显示尺寸的最后选择和颜色,即使我没有从页面中选择尺寸或颜色也是如此。

您能帮我吗?

single_product.php

if (isset($_GET['id'])) {
    require 'includes/productdb.inc.php';
    $id = $_GET['id'];
    $sql = mysqli_query($con,"SELECT * FROM prodcutitemsub WHERE id='$id'");
    $productcount = mysqli_num_rows($sql);
    if($productcount > 0 ) {
        //GET ALL PRODUCT DETAILS.
        while($row = mysqli_fetch_array($sql)){
            $product_name = $row['description_en'];
            $brand = $row['brand'];
            $material = $row['material'];
            $price = $row['price'];
        }
    } else {
        echo " That item doesn't exist";
        exit();
    }
} else {
    echo "Data to render this page is missing";
    exit();
    }

$product_size = $con->query("SELECT distinct size FROM `product_options` WHERE productitemsub_id = '$id' GROUP BY size");
$product_color = $con->query("SELECT distinct color FROM `product_options` WHERE productitemsub_id = '$id' GROUP BY color");
$res = "SELECT distinct id FROM `prodcutitemsub` WHERE id = '$id' ";
if(isset($_GET['size']) && $_GET['size']!="") :
    $size = $_GET['size'];
    $res.=" AND size IN ('".implode("','",$size)."')";
endif;

if(isset($_GET['color']) && $_GET['color']!="") :
    $color = $_GET['color'];
    $res.=" AND color IN ('".implode("','",$color)."')";
endif;  

html代码:

    <form method="post" action="single.php?action=add&id=<?php echo $id; ?>">
<h3><?php echo $product_name; ?></h3>
<p><span class="item_price"><?php echo $price; ?></span></p>


<div class="color-quality">
    <div class="color-quality-right">
    <h5>Quantity :</h5>
        <input type="text" name="quantity" class="text-center" value="1" />
    </div>
</div>
<div class="occasional">
    <h5>Color: </h5>
       <div class="colr ert">
        <?php
        foreach ($product_color as $key => $new_color) :
                                if(isset($_GET['color'])) :
                                    if(in_array(data_clean($new_color['color']),$_GET['color'])) :
                                        $check='checked="checked"';
                                        else : $check="";
                                    endif;
                                endif;
                        ?>
                        <label class="radio" for="color[]"><input type="radio" name="color[]" ><i></i><?=ucfirst($new_color['color']); ?></label>
                            <?php endforeach; ?>
                    </div>
                    <!-- <div class="colr">
                        <label class="radio"><input type="radio" name="radio"><i></i>Sneakers </label>
                    </div>
                    <div class="colr">
                        <label class="radio"><input type="radio" name="radio"><i></i>Formal Shoes</label>
                    </div> -->
                    <div class="clearfix"> </div>
                </div>
                <div class="occasional">
                    <h5>Size: </h5>
                    <div class="colr ert">
                        <?php
          // get the sizes and display them here.
                            foreach ($product_size as $key => $new_size) :
                                if(isset($_GET['size'])) :
                                    if(in_array($new_size['size'],$_GET['size'])) :
                                        $check='checked="checked"';
                                        else : $check="";
                                    endif;
                                endif;
                        ?>
                        <label class="radio" for="size[]"><input type="radio" name="size[]" value="<?=ucfirst($new_size['size']); ?>" <?=@$check;?>><i></i><?=ucfirst($new_size['size']); ?></label>
                            <?php endforeach; ?>
                    </div>
                    <div class="clearfix"> </div>
                </div>
                <div class="occasion-cart">
                    <div class="snipcart-details top_brand_home_details item_add single-item hvr-outline-out button2">
                        <form action="#" method="post">
                            <fieldset>
                                <input type="hidden" name="cmd" value="_cart">
                                <input type="hidden" name="description_en" value="<?php echo $product_name; ?>">
                                <input type="hidden" name="price" value="<?php echo $price; ?>">
                    <!--Display the selected size and color in the shopping cart-->           <input type="hidden" name="size" value="<?=ucfirst($new_size['size']);?>">
                                <input type="hidden" name="img" value="<?php echo $img; ?>">
                                 <input type="hidden" name="color" value="<?php echo $new_color['color']; ?>"> 

                                <input type="submit" name="add" value="Add to cart" class="button">
                            </fieldset>
                        </form>
                    </div>                                                      
                </div>
            </form>

cart.php

       require 'includes/cart.inc.php';
            if (!empty($_SESSION["cart"])) {
                $total = 0;
                foreach($_SESSION["cart"] as $key => $value){
            ?>
        <div class="row" id="products">
        <div class="cart-content">
            <div class="col-lg-3 col-md-3 col-sm-3 text-center border-right">
                <a href="cart.php?action=delete&barcode=<?php echo $value["barcode"]; ?>" class="popup-items-close">&times;</a>
                <img src="<?php echo $value["img"]; ?>" alt="Tour photo" class="popup__img">
            </div>
            <div class="col-lg-3 col-md-3 col-sm-3 text-center border-right height">
                <h4 class="cart-title text-center vertical-center"><a href="#" class="item-link"><?php echo $value["description_en"]; ?></a></h4>
                <p class="desc">Size: <?php echo $value["size"]; ?></p>
                <p class="desc">color: <?php echo $value["color"] ?></p>
            </div>
            <div class="col-lg-3 col-md-3 col-md-3 col-sm-3 text-center border-right height">
                <p class="quantity-title">Quantity</p>
                <span class="quantity-num"><?php echo $value["quantity"]; ?></span>
            </div>
            <div class="col-lg-3 col-md-3 col-md-3 col-sm-3 text-center height">
                <p class="quantity-title">Price</p>
                <span class="quantity-num">$<?php echo number_format($value["quantity"] * $value["price"], 2); ?></span>

0 个答案:

没有答案
相关问题