0

我想在购物车页面中添加不同类型的产品?如何将相同的产品添加到购物车页面但产品尺寸不同?

我想要这个购物车

1) 产品 A 1950 小 2) 产品 A 1950 大 3) 产品 A 1950 XL

* javascript *

<script language="javascript">
function addtocart(id){
document.form1.id.value=id;
document.form1.command.value='add';
document.form1.submit();
}
</script>

php代码

$sizes=$_POST['size'];
include("cart/functions.php");
if($_REQUEST['command']=='add' && $_REQUEST['id']>0){
$id=$_REQUEST['id'];
addtocart($id,1);
header('location:shoppingcart.php?size='.$sizes.'');    
exit();
}

加入购物车的表格 1

<form name="form1" method="post">
<input type="hidden" name="id" />
<input type="hidden" name="command" />
</form>

尺寸表

<form action="" id="size1" name="size1" method="post">
<div class="details clearfix">
<p class="item">
<label style="font-family:Arial,Helvetica,sans-serif; font-size:12px; font-weight:bold; ">Choose your size:</label>
<select style="background-color:#CCC" name="size" id="size" >
<option>Select</option>
<option value="Small">Small</option>
<option value="Medium">Medium</option>
<option value="Large">Large</option>
<option value="XL">XL</option>
</select>
</div>

添加到购物车按钮

<div class="buy">
<?php 
$query3=mysql_query("select * from products WHERE id='$id' ");
$row3=mysql_fetch_array($query3);
?>
<input type="button" class="button1" value="Add To Cart" >
</div>
</form>

购物车页面

<?php
if(is_array($_SESSION['cart'])){
echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td align="center"><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Product Name</font></td>
<td align="center"><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Product Image</font></td>
<td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Price</font></td>
<td><div><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Size</font></div></td>
<td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Amount</font></td>
<td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Options</font></td></tr>';

$max=count($_SESSION['cart']);
or($i=0;$i<$max;$i++){
$id=$_SESSION['cart'][$i]['id'];
$q=$_SESSION['cart'][$i]['qty'];
$product=get_product_name($id);
$image=get_product_image($id);
$ids=get_id($id);
$itemcode=get_itemcode($id);
$size=get_size($id);
if($q==0) continue;
?>

<tr bgcolor="#FFFFFF"><td align="center"><font style="font-family:Arial,Helvetica,sans-serif;  font-size:15px; color:#000;"><input type="hidden" name="itemcode[]" value="<?php echo $itemcode?>" /><input type="hidden" name="ids[]" value="<?php echo $ids?>" /><input type="hidden" name="product[]" value="<?php echo $product?>" /><?php echo $product?></font></td><td align="center"><input type="hidden" name="image[]" id="image"  value="<?php echo $image?>"  /><img name="image" id="image" src="admin/uploads/small0_<?php echo $image?>" width="150" height="150"></td>



   <td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:15px; color:#000;">  <input type="hidden" name="price[]" id="price" value="<?php echo get_price($id)?>"/>Rs.<?php echo get_price($id)?></font></td>
  <td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:15px; color:#000;"><?php echo $size ?></font><input type="hidden" name="size" value="<?php echo $size?>" /></td>                    
 <td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:15px; color:#000;"><input type="hidden" name="amt[]"  value="<?php echo get_price($id)*$q?>"/> Rs.<?php echo get_price($id)*$q?></font></td>
   <td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:15px; color:#000;"><a href="javascript:del(<?php echo $id?>)"><input type="button" class="button5" value="Remove" /></a></font></td></tr>
  <?php                 
  }
  ?>

  <tr><td colspan="6" align="right"><b><font style="font-family:Arial,Helvetica,sans-serif;  font-size:15px; color:#000;"><input type="hidden" name="total" id="total" value="<?php echo get_order_total()?>"/>Order Total:Rs.<?php echo get_order_total()?></font></b></td></tr>        
  <tr><td colspan="6" align="right"><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000; !important">Cash On Delivery & Free Shipping</font></td></tr>  
 <tr><td colspan="6" align="right">

 <?php  $query1=mysql_query("SELECT * FROM category ORDER BY id ") or die ('Product Query Problem');
  $row4=mysql_fetch_array($query1);$ids=$row4['id'];$cat_name=$row4['categories'];?><input type="button" class="button1" value="Continue Shopping"  onclick="window.location='product.php'" /><input type="button" class="button2"  value="Clear Cart" onclick="clear_cart()"><input type="button" class="button3" value="Update Cart" onclick="update_cart()"><input type="submit" class="button4" name="order" id="order" value="Place Order"></td></tr>

   <?php
   }
  else{
  echo "<tr bgColor='#FFFFFF'><td><font style='font-family:Arial,Helvetica,sans-serif; font-size:15px; color:#000;'>There are no items in your shopping cart!</font></td>";
  }
  ?>
4

1 回答 1

0

相反,您可以使用会话来执行此操作,并在成功购买后将其添加到数据库中。

例子

if(isset($_SESSION['items'][$_GET['item_id']])) {
    $_SESSION['items'][$_GET['item_id']]++;
    //multiply price code 
} else {
    $_SESSION['items'][$_GET['item_id']] = 1; //If added for the first time
    //set session for price
}

参考。

http://www.php-shopping-cart-tutorial.com/shopping-cart.htm

http://jameshamilton.eu/content/simple-php-shopping-cart-tutorial

http://v3.thewatchmakerproject.com/journal/276/

于 2013-03-07T12:17:26.617 回答