yendrrek
yendrrek

Reputation: 331

Problem with adding items to a shopping cart again after one of them has been removed

I have four items in a shop. Adding them to the basket works without problems. If I remove the last added item from the basket, all works fine, I can add the same item again, but If I remove any other item from the basket which is not the last one then when I readd it, it overwrites the last one. Basically, readding an item works only when I remove the last item (readded item appears again at the bottom of the list).

My php code: I have omitted session_start() and configuration from the top of the code. The products are all fetched from a mysql database.

// Adding products to the basket:

<?php
if (isset($_POST['id'])) {
    if (isset($_SESSION['shop'])) {
        $item_array_id = array_column($_SESSION['shop'], 'product_id');
        if (!in_array($_POST['id'], $item_array_id)) {
            $count = count($_SESSION['shop']);
            $item_array = array(
                'product_id' => $_POST['id'],
                'item_photo' => $_POST['hidden_photo'],
                'item_photo_alt' => $_POST['hidden_photo_alt'],
                'item_name' => $_POST['hidden_name'],
                'product_price' => $_POST['hidden_price'],
                'item_quantity' => $_POST['quantity'],
            );
            $_SESSION['shop'][$count] = $item_array;
        }
    } else {
            $item_array = array(
                'product_id' => $_POST['id'],
                'item_photo' => $_POST['hidden_photo'],
                'item_photo_alt' => $_POST['hidden_photo_alt'],
                'item_name' => $_POST['hidden_name'],
                'product_price' => $_POST['hidden_price'],
                'item_quantity' => $_POST['quantity'],
            );
            $_SESSION['shop'][0] = $item_array;
    }
}


// Removing products from the basket:

if (isset($_GET['action']) && $_GET['action'] == 'delete') {
    foreach ($_SESSION['shop'] as $key => $value) {
        if ($value['product_id'] == $_GET['id']) {
            unset($_SESSION['shop'][$key]);
        }
    }

}
?>

Upvotes: 1

Views: 661

Answers (1)

dehart
dehart

Reputation: 1678

This should work, it uses the ID of the product as the key of the map. and replaces the item if it already exists, the delete function doesn't have to iterate over the items anymore:

<?php
if (!isset($_SESSION['shop'])) {
    $_SESSION['shop'] = array();
}
// Adding products to the basket:
if (isset($_POST['id'])) {
    $_SESSION['shop'][$_POST['id']] = array(
        'product_id' => $_POST['id'],
        'item_photo' => $_POST['hidden_photo'],
        'item_photo_alt' => $_POST['hidden_photo_alt'],
        'item_name' => $_POST['hidden_name'],
        'product_price' => $_POST['hidden_price'],
        'item_quantity' => $_POST['quantity'],
    );
}

// Removing products from the basket:
if (isset($_GET['action']) && $_GET['action'] == 'delete') {
    if(isset($_SESSION['shop'][$_GET['id']])) {
        unset($_SESSION['shop'][$_GET['id']]);
    }
}

Upvotes: 2

Related Questions