简体   繁体   English

php购物车无法停止将相同产品添加到购物车数组

[英]php shopping cart unable to stop adding same product to cart array

This is just a picture of the result I got. 这只是我得到的结果的图片。 As you can see, item 4 can be added again and again. 如您所见,可以一次又一次添加项目4。 What I want in my shopping cart is that, for each color, item 4 can only be added once. 我要在购物车中显示的是,对于每种颜色,项目4只能添加一次。

在此处输入图片说明

if( isset($_SESSION['cart']) ){

    ################## Do looping to check array cart if already has item with same id and color ##########################

    $i = 0; 
    $j = 1; // set to index [1] //
    $found = '';

    foreach( $_SESSION['cart'] as $cart ){

        ######## Check if product chosen already exist in the cart #######

        if( $cart[$i]['id'] == $new_product['id'] && $cart[$i]['color'] == $new_product['color'] ){

            $found = true; // Found existing item in the array cart //

        }
        else{
            $found = false;
            $j++; // No item found in array cart, increase to index [2] //
        }

        ####### If no same item is found in cart, add the new product to the cart array ###############



        $i++; // Increase array index to check second array and so on //
    }

    if(!$found){ // No item found in array cart, add item into cart //

        $_SESSION['cart'][$j]['id'] = $new_product['id'];
        $_SESSION['cart'][$j]['product_name'] = $new_product['product_name'];
        $_SESSION['cart'][$j]['discount'] = $new_product['discount'];
        $_SESSION['cart'][$j]['qty'] = $new_product['qty'];
        $_SESSION['cart'][$j]['color'] = $new_product['color'];
        $_SESSION['cart'][$j]['shipping_fee'] = $new_product['shipping_fee'];

    }

}

else{

    $_SESSION['cart'][0]['id'] = $product['id'];
    $_SESSION['cart'][0]['product_name'] = $product['product_name'];
    $_SESSION['cart'][0]['discount'] = $product['discount'];
    $_SESSION['cart'][0]['qty'] = $qty;
    $_SESSION['cart'][0]['color'] = $color;
    $_SESSION['cart'][0]['shipping_fee'] = $shipping_fee;

}

How could I have my codes changed? 如何更改密码?

try this ... 尝试这个 ...

if($found){ // item found in array cart

    $_SESSION['cart'][0]['id'] = $product['id'];
    $_SESSION['cart'][0]['product_name'] = $product['product_name'];
    $_SESSION['cart'][0]['discount'] = $product['discount'];
    $_SESSION['cart'][0]['qty'] = $qty;
    $_SESSION['cart'][0]['color'] = $color;
    $_SESSION['cart'][0]['shipping_fee'] = $shipping_fee;   

    }

}

else{ // no item found

     $_SESSION['cart'][$j]['id'] = $new_product['id'];
        $_SESSION['cart'][$j]['product_name'] = $new_product['product_name'];
        $_SESSION['cart'][$j]['discount'] = $new_product['discount'];
        $_SESSION['cart'][$j]['qty'] = $new_product['qty'];
        $_SESSION['cart'][$j]['color'] = $new_product['color'];
        $_SESSION['cart'][$j]['shipping_fee'] = $new_product['shipping_fee'];

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM