I want to delete some items from shopping cart, which is stored in a session variable named $_SESSION["products"]
The deleting part is working fine until there are different sizes of products with the same product code, when a request to delete is called for the specific size all the items gets deleted with that same product code.
Instead, it should only be deleted the item that is requested.
This is my PHP code to delete
if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["products"]))
{
$product_code = $_GET["removep"]; //get the product code to remove
$product_size = filter_var($_POST["product_size"], FILTER_SANITIZE_NUMBER_INT); //product size
$return_url = base64_decode($_GET["return_url"]); //get return url
foreach ($_SESSION["products"] as $cart_itm) //loop through session array var
{
if($cart_itm["code"]!=$product_code && $cart_itm["size"]!=$product_size){ //item does,t exist in the list
$product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'size'=>$cart_itm["size"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]);
}
//create a new product list for cart
$_SESSION["products"] = $product;
}
//redirect back to original page
header('Location:'.$return_url);
}
Remove items from cart
<span class="remove-itm">
<a href="'.$site_url.'/cart_update.php?removep='.$cart_itm["code"].'&size='.$cart_itm["size"].'&return_url='.$current_url.'" class="remove" title="Remove this product from cart"> ×</a>
</span>
$_SESSION["products"] = $product;out off the foreach body.$product[] = $cart_itm