Reputation: 325
Here are my functions. I have a problem with removing and editing shopping cart.
Earlier I had only two parameters in array $id and $quantity, but I had to add $varianta which stores for example size, etc.
Insert and foreach of $_SESSION['cart'] works like a charm, but as I said removing and updating not.
function addToCart()
public static function addToCart($data) {
$id = $data['id']; //id
$quantity = $data['qty']; //qty
$varianta = $data['varianty']; //varianta
//ověříme, zda $_SESSION['cart']
if(!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
array_push($_SESSION['cart'], array(
'id' => $id,
'quantity' => $quantity,
'varianta' => $varianta
));
}
function editCart()
public static function editCart($data) {
//edit quantity +-
}
function removeFromCart()
public static function removeFromCart($id) {
unset($_SESSION['cart'][$id]); //DOESNT WORK
}
I would be thankful if somebody told me how to approach what I am trying achieve.
Thank you.
Upvotes: 0
Views: 48
Reputation: 9635
because you have not set the $id
as index in $_SESSION['cart']
try this
public static function addToCart($data) {
$id = $data['id']; //id
$quantity = $data['qty']; //qty
$varianta = $data['varianty']; //varianta
//overíme, zda $_SESSION['cart']
if(!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
$_SESSION['cart'][$id] = array(
'quantity' => $quantity,
'varianta' => $varianta
));
}
Upvotes: 2