Reputation: 2659
I am trying to increase quantity of a product when the id of that product already exists inside an array.
My product array $arr2
looks like this for example:
Array
(
[productid] => 3
[productname] => Eikenhout pallet
[productalias] => eikenhout-pallet
[productcat] => Eikenhout
[catalias] => eikenhout
[quantity] => 1
[kuubkosten] => 150|1 kuub
)
Then I create a session and add all information to it.
if (!isset($_SESSION['cart'])) {
// and turn session into array
$_SESSION['producten'] = array();
}
// If id of product does not exist, add it to session
if(!isset($_SESSION['producten'][$arr2['productid']])){
$_SESSION['producten'][$arr2['productid']] = $arr2;
// Else add to quantity
}else{
$_SESSION['producten'][$arr2['productid']]['quantity'] += $arr2['quantity'];
}
If I then print my session, this is what I get:
Array
(
[3] => Array
(
[productid] => 3
[productname] => Eikenhout pallet
[productalias] => eikenhout-pallet
[productcat] => Eikenhout
[catalias] => eikenhout
[quantity] => 1
[kuubkosten] => 150|1 kuub
)
)
But after adding the product again, the quantity is not increased. What am I doing wrong? I can see in my network tab that all data is correctly posted and there are no errors.
Upvotes: 0
Views: 66
Reputation: 526
As devpro already said, you are checking if the key 'cart' isset
if (!isset($_SESSION['cart'])) {
But you are never setting that variable. Your code might just work when you change
if (!isset($_SESSION['cart'])) {
// and turn session into array
$_SESSION['producten'] = array();
}
to
if (!isset($_SESSION['producten'])) {
// and turn session into array
$_SESSION['producten'] = array();
}
Upvotes: 1