Search code examples
phparraysmultidimensional-arrayshopping-cart

PHP Session Array - store same item with different values


i'm working on a PHP cart system and i have got a problem.

The Problem:

When a user adds an item and then again, adds that same item but with different values like (different size or quantity) the cart updates that entry with new values that's chosen by the user. previous details gets deleted.

Solution i have been looking for

If users adds any item, and then wants to add that same item but with different requirements, should be added as a separate entry int the cart session.(only if the specific variable gets changed, for example: A single product but with different sizes).

How can i do this in my current code?

The Cart

//add item in shopping cart
if(isset($_POST["type"]) && $_POST["type"]=='add')
{
    $product_code   = filter_var($_POST["product_code"], FILTER_SANITIZE_STRING); //product code
    $product_size   = filter_var($_POST["product_size"], FILTER_SANITIZE_NUMBER_INT); //product size
    $product_qty    = filter_var($_POST["product_qty"], FILTER_SANITIZE_NUMBER_INT); //product quantity
    $return_url     = base64_decode($_POST["return_url"]); //return url


    //MySqli query - get details of item from db using product code
    $results = $connection->query("SELECT * FROM products WHERE prod_code='$product_code' LIMIT 1");
    $obj = $results->fetch_object();

    if ($results) { //we have the product info 

        //prepare array for the session variable
        $new_product = array(array('name'=>$obj->product_name, 'code'=>$product_code, 'size'=>$product_size, 'qty'=>$product_qty, 'price'=>$obj->price));

        if(isset($_SESSION["products"])) //if we have the session
        {
            $found = false; //set found item to false

            foreach ($_SESSION["products"] as $cart_itm) //loop through session array
            {
                if($cart_itm["code"] == $product_code){ //the item exist in array

                    $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'size'=>$product_size, 'qty'=>$product_qty, 'price'=>$cart_itm["price"]);
                    $found = true;
                }else{
                    //item doesn't exist in the list, just retrieve old info and prepare array for session var
                    $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'size'=>$cart_itm["size"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]);
                }
            }

            if($found == false) //we didn't find item in array
            {
                //add new user item in array
                $_SESSION["products"] = array_merge($product, $new_product);
            }else{
                //found user item in array list, and increased the quantity
                $_SESSION["products"] = $product;
            }

        }else{
            //create a new session var if does not exist
            $_SESSION["products"] = $new_product;
        }

    }

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

Looking forward for some helpful answers.

Thanks

***** UPDATE ******

Code to remove Product From Cart:

//remove item from shopping cart
if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["products"]))
{
    $product_code   = $_GET["removep"]; //get the product code to remove
    $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){ //item does,t exist in the list
        if($cart_itm["code"] != $product_code && $cart_itm["size"] != $product_size){
            $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);
}

Solution

  • The if condition that you are using is the mistake that you did. Try this one

    if($cart_itm["code"] == $product_code && $cart_itm["size"] == $product_size)
    

    instead of

    if($cart_itm["code"] == $product_code)
    

    The method of checking with the quantity is not good practice since you can edit the quantity alone in the already existing entry.