문제

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::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($id, $_SESSION['cart']) ) {
        if ( $_SESSION['cart'][$id][0] == $size ) {
            $_SESSION['cart'][$id][1]+=$qta;
        } else {
            $_SESSION['cart'][$id] = array( $size, $qta );
        }
    } else {
        $_SESSION['cart'][$id] = array( $size, $qta );
    }

}

So, what this code does ?

1) If the $_SESSION['cart'] does not exist, create it, otherwise add the new item. 2)When you add to cart a item, you must choose a size and a quantity for that item. 3) If that item already exists in the cart array, check if the size is the same, if so just update the quantity. Here is the problem, if the item already exists(checks for the $_SESSION['cart'][$id]) BUT the size is different, do not update the current one, but instead create a new item. The problem is that the current one is being replaced instead of adding one, so instead of 2 products with the same id but different size, I only have the most recent one.

If you could help me solve it I'll be very thankful !

Thank you in advance.

도움이 되었습니까?

해결책

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;
    }
}

다른 팁

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;

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 :)

$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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top