PHP Sessions shopping cart: update product if it's already id the session

后端 未结 4 1444
北海茫月
北海茫月 2021-01-26 09:22

I have struggled to find a solution to a simple shopping cart with $_SESSION. I kept it very simple and this is my code right now

if ( Input::isPos         


        
4条回答
  •  北海茫月
    2021-01-26 09:37

    A better way of doing it would be to create another array called sizes and store sizes there. So your code would look something like

    if (in_array($size, $_SESSION['cart'][$id]['sizes'])) {
        //The size has been added to the cart
    } else {
        //The size isn't in the cart so add it
        $_SESSION['cart'][$id]['sizes'][] = $size;
    }
    

    Also rather than store the quantity like

    $_SESSION['cart'][$id][1]+=$qta;
    

    Why don't you have more meaningful keys so you can understand the contents of the array better. For example

    $_SESSION['cart'][$id]['qty']+=$qta;
    

提交回复
热议问题