PHP Session Array - store the same element with different values

I am working on a PHP bucket system and I have a problem.

Problem:

When a user adds an item and then adds the same item again, but with different values ​​such as (different size or quantity), the cart updates are entered with new values ​​selected by the user. the previous information is deleted.

The solution I was looking for

If the user adds any item and then wants to add the same item, but with different requirements, it must be added as a separate entry in the cart session. (only if a specific variable changes, for example: one product, but with different sizes).

How can I do this in my current code?

Basket

//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 to some helpful answers.

thank

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

Code for removing a product from the 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);
}

      

+3


source to share


3 answers


The condition if

you are using is a mistake you made. Try this

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

      

instead



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

      

The check with quantity method is not good practice as you can only edit quantity in an existing record.

+2


source


You need to design your cart item code that is unique to make it work. Either you can combine a product code with a size or quantity, or you can use something like a GUID to store a single cart item.



The combo key has a problem when changing size or quantity. Using a GUID requires a little work, but is a viable and reliable solution.

+2


source


try to declare the $product

as array at the beginning.

$product = array();

      

Hope it helps.

0


source







All Articles