0

I am making an e-commerce web app and I am trying to write code to handle if the user clicks the add to basket button on an item that is already in the basket but it doesn't seem to work correctly. If the button is clicked for a second time then the first item quantity is overridden by the second

if ($action === 'add_product') {
    $id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : null;
    $price = isset($_REQUEST['price']) ? (float) $_REQUEST['price'] : null;
    $quantity = isset($_REQUEST['quantity']) ? (int) $_REQUEST['quantity'] : null;
    $name = isset($_REQUEST['name']) ? trim($_REQUEST['name']) : null;

    if ($id && $price && $quantity) {

        $_SESSION['cart'][$id] = [
            'id' => $id,
            'price' => $price,
            'quantity' => $quantity,
            'name' => $name
        ];
    } // if the action is increment, increse the quantity of the item.
} elseif ($action === 'increment' and array_key_exists('id', $_SESSION['cart'][$_REQUEST['id']])) {

    $_SESSION['cart'][$_REQUEST['id']]['quantity'] += 1;

    //if the action is decrement, decrease the quantity of item by 1 
} elseif(array_key_exists('id',$_SESSION['cart'][$_REQUEST['id']))//here is where it is supposed to handle adding the same item twice {
    $_SESSION['cart'][$_REQUEST['id']]['quantity'] += 1;
} elseif ($action === 'decrement' and array_key_exists('id', $_SESSION['cart'][$_REQUEST['id']])) {
    $_SESSION['cart'][$_REQUEST['id']]['quantity'] -= 1;
    if ($_SESSION['cart'][$_REQUEST['id']]['quantity'] <= 0) {
        unset($_SESSION['cart'][$_REQUEST['id']]);
    }
} elseif ($action === 'remove' and array_key_exists('id', $_SESSION['cart'][$_REQUEST['id']])) {
    unset($_SESSION['cart'][$_REQUEST['id']]);
}

I have commented where it is supposed to be adding to the quantity, but it does not add to the quantity and update it simply overrides the old quality and I don't know why. I have also tried if(in_array($id,$_SESSION['cart'][$_REQUEST['id'])) but that also did not work. I don't quite understand why it won't be working so any help is greatly appreciated.

burnie5749
  • 21
  • 3
  • Assuming `$id`, `$price` and `$quantity` all have non-falsey values - then it never goes past your initial `if`, so it doesn’t even get to _any_ of the else-ifs below, that could try and check what `$action` contains. – CBroe Apr 14 '21 at 13:53
  • Sorry that was a typo from when I had copied and pasted the code over. there is another if statement before that. I have edited the question now to change it – burnie5749 Apr 14 '21 at 14:07
  • I can’t see the condition `array_key_exists('id', $_SESSION['cart'][$_REQUEST['id']])` make sense. You are checking here, whether `$_SESSION['cart'][$_REQUEST['id']]` is an array, and has an element with that key. But I don’t think you actually set `$_SESSION['cart'][$_REQUEST['id']]['id']` anywhere? This would either need to be `array_key_exists($_REQUEST['id']], $_SESSION['cart'])` - with the _specific_ `$_REQUEST['id']` you want to handle here - or just `isset($_SESSION['cart'][$_REQUEST['id']])` (Main differences between the two - see https://stackoverflow.com/q/3210935/1427878) – CBroe Apr 15 '21 at 06:15

0 Answers0