1

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"> &times;</a>
</span>
4
  • 1
    Your have to be carefull, do not change array, that you looping through in foreach. Change your code, use temp array to store the $products, instead of using $_SESSION["products"]. And move this line: $_SESSION["products"] = $product; out off the foreach body. Commented Jun 13, 2015 at 22:04
  • @StanislavTerletskyi Why? and how? Commented Jun 13, 2015 at 22:05
  • @StanislavTerletskyi i did move that line outside the foreach loop, but my problem its un resolved.... Commented Jun 13, 2015 at 22:12
  • You should do refactoring for the condition. It can be less complicated. And You can use $product[] = $cart_itm Commented Jun 13, 2015 at 22:18

2 Answers 2

2

You are overwriting $_SESSION["products"] in each loop iteration. You need to move that outside of the loop:

    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;
Sign up to request clarification or add additional context in comments.

1 Comment

its still deleting all the items with same product_code
1

Try this logic:

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($_GET["size"], FILTER_SANITIZE_NUMBER_INT); //product size
    $return_url     = base64_decode($_GET["return_url"]); //get return url

    $product = array();

    foreach ($_SESSION["products"] as $cart_itm) 
    {

        if ($cart_itm["code"] == $product_code && $cart_itm["size"] == $product_size) 
        {
            // skip this product, it wont be saved in session
            continue;
        }
        // add this product
        $product[] = $cart_itm;
    }
    // save the updated product list
    $_SESSION["products"] = $product;

    //redirect back to original page
    header('Location:'.$return_url);
}

5 Comments

I've omitted some of your code, did you change only foreach part?
Hm, can you update your question and share the html part, only form.
Please check the updated question... to delete items I'm passing values through QueryStrings and then getting the values by GET method.
So maybe the problem in this part: ...filter_var($_POST["product_size"]... Here you get the value from $_POST array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.