jquery - Adding product quantity to cart in php only if product is available in cart with same color and size -
i've created php ekart site testing purpose. query when click on add cart should firstly validate whether 1 color , 1 size selected, if both have been selected adds product cart works fine if add same product different color or size overlaps same product added , products quantity turns 1 if select product works same thing goes when select different size or color. since beginner @ php, i'm not sure going wrong here.
php code of product add cart given below:
$sql = "select * allprods listing = '$listing' , id = '$id'"; $data = mysql_query($sql); if (mysql_num_rows($data) == 1) { $row = mysql_fetch_array($data); if (isset($_session['cart'][$id])) { if(isset($_session['cart'][$id][$color]) && isset($_session['cart'][$id][$size])) { $_session['cart'][$id]['quantity']++; echo "<script>alert('".$row['product_name']." has been added cart.');</script>"; } else { $_session['cart'][$id] = array('quantity' = >1, 'price' => $row['product_special_price'], 'cat' => $cat, 'id' => $id, 'size' => $size, 'color' => $color, 'name' => $row['product_name']); echo "<script>alert('" . $row['product_name'] . " has been added cart.');</script>"; } } else { $_session['cart'][$id] = array('quantity' => 1, 'price' => $row['product_special_price'], 'cat' => $cat, 'id' => $id, 'size' => $size, 'color' => $color, 'name' => $row['product_name']); echo "<script>alert('" . $row['product_name'] . " has been added cart.');</script>"; } }
cart program code
<?php if(isset($_post['qty'])) { foreach($_post['qty'] $product_id=>$item_qty) { $id=(int)$product_id; $qty=(int)$item_qty; if($qty==0) { unset($_session['cart'][$id]); } elseif($qty>0) { $_session['cart'][$id]['quantity']=$qty; } } } else { echo ""; } if(!empty($_session['cart'])) { require "../link_db.php"; $sql="select * allprods id in("; foreach($_session['cart'] $id=>$value) { $sql.=$id.','; } $sql=substr($sql,0,-1).') order product_id asc'; $data=mysql_query($sql); while($row=mysql_fetch_array($data)) { ?> table row , data goes here!! <?php } else { ?> <tr> <th colspan='4' class='noprodavailablewrap'> there no products in cart. </th> </tr> <?php } ?>
use code
<?php $sql = "select * allprods listing = '$listing' , id = '$id'"; $data = mysql_query($sql); if (mysql_num_rows($data) == 1) { $row = mysql_fetch_array($data); $index = md5($id.$color.$size); if( isset($_session['cart'][$index]) && isset($_session['cart'][$index]['color']) && $_session['cart'][$index]['color'] == $color && isset($_session['cart'][$index]['size']) && $_session['cart'][$index]['size'] == $size){ $_session['cart'][$index]['quantity']++; echo "<script>alert('".$row['product_name']." has been added cart.');</script>"; }else{ $_session['cart'][$index] = array('quantity' => 1, 'price' => $row['product_special_price'], 'cat' => $cat, 'id' => $id, 'size' => $size, 'color' => $color, 'name' => $row['product_name']); echo "<script>alert('" . $row['product_name'] . " has been added cart.');</script>"; } } ?>
Comments
Post a Comment