button我们已经完成了添加到购物车功能,产品已成功添加到我的购物车,但问题是要选择产品尺寸,产品尺寸不同,如鞋类:1,2,3, 4.如果所选尺寸为2,则应将其添加到我的购物车中。 我们使用下面的代码:
//Size
<table><tr><td>Size:</td>
<?php
for($i=0; $i<$sizecount; $i++) { ?>
<td><button onclick="selectsize(<?php echo $arr[$i];?>)" class="add-size-cart" hef="<?php echo $arr[$i];?>" pef="<?php echo $products['product_id'];?>"><?php echo $arr[$i];?></button></td> <?php } ?></tr></table>
//Addtocart button
<a href="addcart.php" class="btn btn-primary add-cart-button" ref="<?php echo $products['product_id'];?>"><i class="fa fa-user"></i> Add to cart</a>
$(document).ready(function(){
//set cookie
//setCookie('sids','1,2,3,4,5','7');
//deleteCookie('sids');
//deleteCookie('sids');
var sids=getCookie('sids');
var size=getCookie('size');
if(sids== null){
setCookie('sids','',7);
sids='';
}
$('.add-cart-button').click(function(){
var id = $(this).attr('ref');
var aids = []; //stores array
if(sids == ''){
aids.push(id);
setCookie('sids',aids.toString(),7);
}else{
aids = sids.split(',');
//remove duplicates
for(var i=0; i< aids.length; i++){
if(id == aids[i]){
return;
}
}
aids.push(id);
setCookie('sids',aids.toString(),7);
}
});
});
//query
if( isset($_COOKIE['sids']) && $_COOKIE['sids']!=''){
$aids = explode(',',$_COOKIE['sids']);
//validate data
foreach($aids as $k => $val){
if(!is_numeric($val)){
unset($aids[$k]);
}
//echo "select product_id,product_name,price,actual_price,description,index_image from mc_products_tbl WHERE status=1 AND product_id='$val'";
$sqlpro=$con->query("select product_id,product_name,price,actual_price,description,index_image from mc_products_tbl WHERE status=1 AND product_id='$val'");
$products = $sqlpro->fetch_assoc();
$sqlcount=mysqli_num_rows($sqlpro);
}
if(!isset($_SESSION['cart_info'][$products['product_id']])){
$temp['id'] = $products['product_id'];
$temp['name'] = $products['product_name'];
$temp['cost'] = $products['price'];
$temp['image'] = $products['index_image'];
$temp['quantity'] = 1;
$_SESSION['cart_info'][$products['product_id']] = $temp;
}
}
此代码仅用于将产品添加到购物车但不是尺寸,我们需要向购物车添加尺寸,我可以请求您建议添加尺寸到购物车。