2013-03-07 73 views
0

我想在購物車頁面添加不同類型的產品?如何將相同的產品添加到購物車頁面,但產品尺寸不同?如何將相同的商品添加到購物車頁面,但商品尺寸不同?

我要像這個購物車

1)產品1950年的小 2)產品1950年的大 3)產品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>"; 
    } 
    ?> 
+0

這取決於你'addtocart()'函數和數據結構。然而,似乎奇怪的是,不同大小的產品將具有相同的ID。 – jeroen 2013-03-07 12:18:25

回答

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/

相關問題