3

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);
}

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

12 Comments

Now the items adding separately, which is the answer i was looking. But there's an issue with the deletion. all items gets deleted because The $product_code is same how to do this? i applied this same condition if($cart_itm["code"] !== $product_code && $cart_itm["size"] !== $product_size){
@yaqoob your condition in the comment is neither the same nor the opposite of sujivasagam's condition. The code you are referring to are also not shown. Please ask a new question on that part
use the same condition in negative format as 'if($cart_itm["code"] != $product_code && $cart_itm["size"] != $product_size)'
@sujivasagam: i didn't work, i'm adding the remove product code in question, please check.
need more explanation. When can you delete the item from cart? While clicking the delete button corresponding to that?
|
2

You need to work out a cart item code which is unique to make it work. Either you could combine the product code with size or quantity, or use something like a GUID to store separate cart item.

A combined key has an issue when changing size or quantity. Using GUIDs requires a little work, but is a viable and solid solution.

1 Comment

updated my question with the code for "item deletion"
0

try declaring the $product as array in the beginning.

$product = array();

Hope this helps.

Comments

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.