I am trying to generate the alert window if condition is already set. But when the add button is click, the code automatically generate the Alert Window. I am expecting that the window prompt should happen after the item is already added to the cart. In which, if the ID exist in the array, the window prompt is populated upon the session being set. Can someone help me identify what I may be doing wrong with the code shown below?
if (isset($_SESSION['cart'])) {
$item_array_id = array_column($_SESSION['cart'], 'id'); //Check if item is already added to cart.
if (in_array($row['id'], $item_array_id)){//execute if item is already added to cart.
echo "<script> alert('Product is already added to cart');</script>";
echo "<script> alert('window.location='index.php'</script>";
}
}
}else{
$item_array = array('id' => $row['id']);
$_SESSION ['cart'][0] = $item_array;
}