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

后端 未结 4 1456
北海茫月
北海茫月 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:55

    Try using the size as a key to an extra dimension in your multidimensional array. Your current code only allows you to have one size per item.

    You would end up with something like:

    if ( Input::isPost('add') ) {
    
        $id = Input::get('id');
        $qta = Input::post('qta');
        $size = Input::post('size');
    
        if ( !isset($_SESSION['cart']) ) {
            $_SESSION['cart'] = array();
        }
    
        if ( array_key_exists($_SESSION['cart'][$id][$size]) ) {
            $_SESSION['cart'][$id][$size] += $qta;
        } else {
            $_SESSION['cart'][$id][$size] = $qta;
        }
    }
    

提交回复
热议问题