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

后端 未结 4 1446
北海茫月
北海茫月 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;
    
    0 讨论(0)
  • 2021-01-26 09:43

    The problem is here :

    if ( $_SESSION['cart'][$id][0] == $size ) {
        $_SESSION['cart'][$id][1]+=$qta;
    } else {
        $_SESSION['cart'][$id] = array( $size, $qta );
    }
    

    If the size is different, you replace it by a new array (the else of the bloc). That's your array :

    Array(
        'cart'=>array(
            'id'=>array('size', 'qta')
        )
    )
    

    With that structure, you can have only one size for an id, so if you want to add another instead of replace the old one, you should consider working with that :

    Array(
        'cart'=>array(
            'id'=>array(0=>array('size1'=>'qta1'), 1=>array('size2'=>'qta2'))
        )
    )
    

    Of course, this means you will need to loop over the array to find the right size and, then, update the qta. Not fun.

    This structure may be more interesting to work with :

    Array(
        'cart'=>array(
            'id'=>array('size1'=>'qta1', 'size2'=>'qta2')
        )
    )
    

    So, instead of checking $_SESSION['cart'][$id][0] == $size, you just need to check if $_SESSION['cart'][$id][$size] exists (with an array_key_exists) and go on :)

    0 讨论(0)
  • 2021-01-26 09:54
    $postedQuantity = intval($_POST['quantity']);
    $postedProduct = $_POST['product'];
    $postedSize = $_POST['size'];
    
    $productId = $_POST['product'] . $_POST['size'];// concat the product name/Id and //the size form a new array-key.it will be unique.
    
    if(empty($_SESSION)){
    $_SESSION['cart_items'] = array();
    }
    
    if(empty($_SESSION['cart_items'])) {
        $_SESSION['cart_items'][$productId] = array('name'=> $_POST['product'],
                                                'quantity'=> intval($_POST['quantity']),
                                                'size'=>$_POST['size'],
                                     );
    } elseif(!array_key_exists($productId,$_SESSION['cart_items'])) {
        $_SESSION['cart_items'][$productId] = array('name'=> $_POST['product'],
                                      'quantity'=> intval($_POST['quantity']),
                                      'size'=>$_POST['size'],
                                     );
    }
    else { 
       $_SESSION['cart_items'][$productId]['quantity'] += $postedQuantity;
    }
    
    0 讨论(0)
  • 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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题