Jump to content

shopping cart & sessions


cinek
 Share

Recommended Posts

I'm trying to store data with sessions. Every time a user clicks add to cart button the session should be updated with the quantity, price and product id this is what I have right now

<?php//manage basket            $get_product_id = @$_POST['product'];    $get_product_price = @$_POST['product_price'];        $get_product_quantity = @$_POST['quantity'];                        session_start();        //check if session exists                            if(isset($_SESSION['cart'])){                //scan all session values        foreach($_SESSION['cart'] as $cartItems){            //check if product is already in the basket            echo "scanning <br />";            if(@$cartItems['productID'] == $get_product_id){                            //if product exists increase quantity                $cartItems['qty']++; echo "increased <br />";                break;            } else {            //no duplicates - new item added                $_SESSION['cart'][] = array(                                        "qty" => $get_product_quantity,                                        "productID" => $get_product_id,                                        "price" => $get_product_price                                        ); echo "new item  <br />";            }                        break;        }            } else {    //if session doesn't exist - create one        $_SESSION['cart'][] = array(                                    "qty" => $get_product_quantity,                                    "productID" => $get_product_id,                                    "price" => $get_product_price                                    ); echo "new session <br />";    }        print_r($_SESSION);    //    session_destroy();

can someone help me out with checking if the product is already in the array?

Edited by cinek
Link to comment
Share on other sites

Use a flag to keep track of whether or not the item is in the array. You could use a counter that starts at -1, and if it is another value then it is the index of the item in the array.

$idx = -1;foreach($_SESSION['cart'] as $i => $cartItems){  if($cartItems['productID'] == $get_product_id)  {    $idx = $i;    break;  }} if ($idx == -1){  // not found}else{  // found at $_SESSION[$idx]}

  • Like 1
Link to comment
Share on other sites

this is what I have right now

$i = $_COOKIE['someCount'];	echo  "value of cookies " . $i . "<br />";		if(isset($_SESSION['cart'])){				//check if product exists		$arrayLength = count($_SESSION['cart']);				//check if anything is passed		if($get_product_quantity == ""){						echo "nothing here <br />";			echo $get_product_quantity;			} else {				while ($i <= $arrayLength){				//check if product exists				if($_SESSION['cart'][$i] == $get_product_id){					$_SESSION['cart'][$i]['qty'] +=1;					echo "<br />trying to increase value <br />";					//break;				} else {								//else create new array entry									$_SESSION['cart'][$get_product_id] = array(											"qty" => $get_product_quantity,											"price" => $get_product_price											); echo "new item  <br />";				}				//increase cookie value				setcookie("someCount", $_COOKIE['someCount']+1, time()+3600);				echo $arrayLength ." arr";								if($i > $arrayLength) break;			}											}	} else {			//create new session		$_SESSION['cart'][$get_product_id] = array(										"qty" => $get_product_quantity,										"price" => $get_product_price										); echo "new item  <br />";	}	//ammend	//setcookie("someCount", $_COOKIE['someCount']+1, time()+3600);		print_r($_SESSION['cart']);

nothing is added to the array and it keeps going through the loop adding stuff like mad - not sure where the issue is edit: I've added a break statement which seems to have fixed it. The only issue now is that the quantity is not increased if the item already exists in the array

$i = $_COOKIE['someCount'];    echo  "value of cookies " . $i . "<br />";        if(isset($_SESSION['cart'])){                //check if product exists        $arrayLength = count($_SESSION['cart']);                //check if anything is passed        if($get_product_quantity == ""){                        echo "nothing here <br />";            echo $get_product_quantity;            } else {                 while ($i <= $arrayLength){                //check if product exists                if($_SESSION['cart'][$i] == $get_product_id){                    $_SESSION['cart'][$i]['qty'] +=1;                    echo "<br />trying to increase value <br />";                    break;                } else {                                //else create new array entry                                    $_SESSION['cart'][$get_product_id] = array(                                            "qty" => $get_product_quantity,                                            "price" => $get_product_price                                            ); echo "new item  <br />";                                            break;                }                //increase cookie value                //setcookie("someCount", $_COOKIE['someCount']+1, time()+3600);                echo $arrayLength ." arr";                                if($i > $arrayLength) break;            }                                            }    } else {            //create new session        $_SESSION['cart'][$get_product_id] = array(                                        "qty" => $get_product_quantity,                                        "price" => $get_product_price                                        ); echo "new item  <br />";    }

Edited by cinek
Link to comment
Share on other sites

You're only checking a single item in the cart, not all of them. The loop I posted checks all of them. I'm not sure if you understood the point of my code, because you're still trying to increase the quantity or add a new item inside the loop. My code loops through the entire cart first just to see if it exists, and after the loop it either increments the quantity of the existing item or adds a new one.

  • Like 1
Link to comment
Share on other sites

thanks, I'm using your loop now, this is what I have:

 if(isset($_SESSION['cart'])){			if($get_product_id == ""){				echo "nothing here <br />";		} else {			$idx = -1;			foreach($_SESSION['cart'] as $i => $cartItems)			{			  if($cartItems['productID'] == $get_product_id)			  {		   	 $idx = $i;		   	 break;			  }			}						if ($idx == -1)			{			  // not found			  $_SESSION['cart'][] = array(									  "qty" => $get_product_quantity,									  "price" => $get_product_price,									  "productID" => $get_product_id									  ); echo "new item <br />";			}			else			{			  // found at $_SESSION[$idx]			  $_SESSION['cart']['qty'][$idx] =+1;			}					} 	} else {		//create new session		$_SESSION['cart'][] = array(							"qty" => $get_product_quantity,							"price" => $get_product_price,							"productID" => $get_product_id							); echo "new session <br />";	}

but the quantity still doesn't increase adding a new product:

Array ( [0] => Array ( [qty] => [price] => [productID] => ) [1] => Array ( [qty] => 1 [price] => 10 [productID] => 2 ) )

adding the same product again:

Array ( [0] => Array ( [qty] => [price] => [productID] => ) [1] => Array ( [qty] => 1 [price] => 10 [productID] => 2 ) [qty] => Array ( [1] => 1 ) )

and when I add another product to the basket I get this error:

[b]Notice[/b]: Undefined index: productID on line 126

which is:

  if($cartItems['productID'] == $get_product_id) 

nvm fixed it:

if(isset($_SESSION['cart'])){            if($get_product_id == ""){                echo "nothing here <br />";        } else {            $idx = -1;            foreach($_SESSION['cart'] as $i => $cartItems)            {              if($cartItems['productID'] == $get_product_id)              {           	 $idx = $i;           	 break;              }            }                        if ($idx == -1)            {              // not found              $_SESSION['cart'][] = array(                                      "qty" => $get_product_quantity,                                      "price" => $get_product_price,                                      "productID" => $get_product_id                                      ); echo "new item <br />";            }            else            {              // found at $_SESSION[$idx]              $_SESSION['cart'][$idx]['qty']++; //$_SESSION['cart']['qty'] + 1;;              echo $idx . "<br />";              echo "trying to increase value <br />";            }                    }    } else {        //create new session        $_SESSION['cart'][] = array(                            "qty" => $get_product_quantity,                            "price" => $get_product_price,                            "productID" => $get_product_id                            ); echo "new session <br />";    }

Edited by cinek
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...