我有这个代码将产品保存在购物车中。 我需要保存产品和尺寸 - 数量x价格。 在这个项目中存储产品名称,大小,数量和价格,但它显示相同的行。 如果我添加'product1'大小M quanity 1,如果我添加'product1'大小S quanitity 1,在购物车中我得到'product1'大小S(最后添加的大小)数量1(但在tquan我得到2个产品)。 那么我怎样才能为每个尺寸的产品做另外一行。
$_SESSION['CART']['order_token'] = $form_token;
$arrCart['id'] = $pID;
$arrCart['quantity'] = $_SESSION['CART']['PRODUCT'][$pID]['size']['quantity'] + $_POST['quantity'];
$chkQuan = mysqli_fetch_assoc(mysqli_query($link, "SELECT quantity FROM table WHERE id = '".$pID."'"));
$total =$_SESSION['CART']['tquan']+$_POST['quantity'];
$arrCart['pret'] = $_POST['product_price'];
$arrCart['size'] = $_POST['size'];
$_SESSION['CART']['tquan'] = $total;
$_SESSION['CART']['PRODUCT'][$pID] = $arrCart;
此部分用于显示购物车
if(isset($_SESSION['CART']) && $_SESSION['CART']!='')
{
$endsumm = '';
$product_total_price = '';
foreach($_SESSION['CART']['PRODUCT'] as $pID=>$pArr)
{
$prod = mysqli_fetch_assoc(mysqli_query($link, "SELECT * FROM table WHERE id='".$pID."'"));
$product_total_price = $pArr['quantity']*$pArr['pret'];
$endsumm += $pArr['quantity']*$pArr['pret'];
答案 0 :(得分:0)
在将产品插入购物车之前,您应检查产品是否已装入购物车。
$total =$_SESSION['CART']['tquan']+$_POST['quantity'];
$arrCart['pret']= $_POST['product_price'];
$arrCart['size']= $_POST['size'];
$_SESSION['CART']['tquan'] = $total;
$_SESSION['CART']['PRODUCT'][$pID] = $arrCart
if(!in_array($pID, array_map(function($id){return $id[$pID];}, $_SESSION['CART']))){
if(!in_array($_POST['size'], array_map(function($size){return $size['size'];}, $_SESSION['CART']))){
//Here insert the product into the cart as you were inserted in the question
}
}